首页 新闻动态 如何用php结合phpmailer发送邮件

如何用php结合phpmailer发送邮件

来源:网企运营网站建设 | 时间:2020/10/30 | 浏览:17175

php本身提供了一个邮件发送函数mail,可以在程序中直接发送电子邮件,不过该函数要求服务器支持sendmail或者必须设置一台不需要中继的邮件发送服务器,但现在要找到一台不需要身份验证的邮件发送中继几乎不可能。

所以使用mail函数往往无法成功发送email。

自己如果熟悉SMTP协议,结合socket功能就可以编写高效稳定的邮件发送程序,但对一般用户来说太困难.好在互联网上已经有很多别人编写好的邮件发送模块,我们只需要下载后简单调用即可,十分方便。

在此我们给用户推荐一个php下功能十分强大,易用且免费的SMTP类模块-PHPMailer,(下载地址http://downinfo.myhostadmin.net/mail/PHPMailer-BMH_v5.0.0.zip)PHPMailer是一个用于发送电子邮件的PHP函数包。它提供的功能包括:

*.在发送邮时指定多个收件人,抄送地址,暗送地址和回复地址
*.支持多种邮件编码包括:8bit,base64,binary和quoted-printable
*.支持SMTP验证
*.支持冗余SMTP服务器
*.支持带附件的邮件和Html格式的邮件
*.自定义邮件头
*.支持在邮件中嵌入图片
*.调试灵活
*.经测试兼容的SMTP服务器包括:Sendmail,Postfix,Imail,Exchange等
*.可运行在任何平台之上

下载该组件后,按如下示例编写代码即可实现php在线发送邮件.
 

一:前台表单


<html>
<body>
<h3>phpmailer Unit Test</h3>
请你输入<font color="#FF6666">收信</font>的邮箱地址:
<form name="phpmailer" action="send.php" method="post">
<input type="hidden" name="submitted" value="1"/>
邮箱地址: <input type="text" size="50" name="address" />
<br/>
<input type="submit" value="发送"/>
</form>
</body>
</html>


二:后台PHP程序


 <?php
require("class.phpmailer.php"); //下载的文件必须放在该文件所在目录
$mail = new PHPMailer(); //建立邮件发送类
$address = $_POST['address'];
$mail->IsSMTP(); // 使用SMTP方式发送
$mail->Host = "mail.xxxxx.com"; // 您的企业邮局域名
$mail->SMTPAuth = true; // 启用SMTP验证功能
$mail->Username = "user@xxxx.com"; // 邮局用户名(请填写完整的email地址)
$mail->Password = "******"; // 邮局密码

$mail->From = "user@xxxx.com"; //邮件发送者email地址
$mail->FromName = "您的名称";
$mail->AddAddress("$address", "");//收件人地址,可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")
//$mail->AddReplyTo("", "");

//$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件
//$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式

$mail->Subject = "PHPMailer测试邮件"; //邮件标题
$mail->Body = "Hello,这是测试邮件"; //邮件内容
$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //附加信息,可以省略

if(!$mail->Send())
{
 echo "邮件发送失败. <p>";
 echo "错误原因: " . $mail->ErrorInfo;
 exit;
}

echo "邮件发送成功";
?>


TAG:深圳龙岗外贸邮箱
相关案例
Shenzhen Bente Industrial Co., Ltd BenteIC was founded in May 2011 and has been deeply engaged in the field of electronic technology for over a decade. Focusing on the integrated supporting services of "global channels for memory ics + burning and testing", it is recognized as a technology-driven and practical technology enterprise in the industry. We adhere to the principles of "concentration and professionalism; With the core values of "co-creation and win-win", we have been deeply engaged in the storage niche market. Through diversified brand layout, we precisely match market demands, focus on the core pain points of small and medium-sized batch customers, and have established a high-quality reputation as a "storage professional" within the industry. The core competitiveness of the company stems from a professional technical team composed of senior semiconductor practitioners. It closely follows the technological iteration rhythm of the industry and, with solid professional capabilities and rich practical experience, provides customers with all-round technical support and customized supporting services.
深圳市奔特实业有限公司 BenteIC 创立于2011年5月,深耕电子科技领域十余载,聚焦“存储IC全球渠道+烧录测试”一体化配套服务,是业内公认的技术实干型科技企业。我们以“专注、专业;共创、共赢”为核心价值观,深耕存储细分领域,通过多元化品牌布局精准匹配市场需求,聚焦中小批量客户核心痛点,在行业内树立起“存储专业户”的优质口碑。公司核心竞争力源于半导体资深从业者组成的专业技术团队,紧跟行业技术迭代节奏,以扎实专业能力与丰富实战经验,为客户提供全维度技术支持与定制化配套服务。
中科华恒制冷技术(惠州)有限公司 中科华恒制冷技术(惠州)有限公司是一家集研发和销售为一体的超临界冷热精控洁净高端设备设计企业,专做冷热环境洁净控制行业国产替代进口产品,研发半导体开发及封测控温Chiller、超低温冷冻机、新能源用控温控流量系统等专用设备,广泛应用于半导体、生物医疗、新能源及大功率激光等冷却领域,特种车辆、各类风洞、卫星测试等领域,处于国际领先水平。
佛山琪昊机械有限公司 佛山琪昊机械有限公司座落于珠三角经济区的中心顺德,水、陆、空交通十分便利。是集科、工、贸一体化的专业公司,公司设计经营制造全自动龙门线,卷对卷连续自动线,化学镀自动线,阳极氧化自动线,垂直升降线,单一镀种环形线及各式爬坡线,附属经营铁氟龙电加热器、氟塑料换热器、贵金属阳极、化学镀镍设备,电镀滚筒及电镀生产线辅助设备、制冷机系统等工业专用产品。