Home > php教程 > php手册 > body text

ThinkPHP使用PHPMailer实现邮件发送

WBOY
Release: 2016-06-13 11:32:05
Original
810 people have browsed it

本文所使用的是ThinkPHP 2.1版和 PHPMailer 5.1版。(后者建议您直接从本博下载,因为我们不能保证下面的代码在所有版本的PHPMailer中都能正常运行)
下面是具体步骤:
第一步、添加PHPMailer类库

PHPMailer邮件发送类V5.1下载地址

将下载后的文件解压,将PHPMail目录移动至ThinkPHP目录中的Vendor内。(请确保class.phpmailer.php文件就在ThinkPHPVendorPHPMailerclass.phpmailer.php)
第二步、添加发送邮件函数
在项目目录中的Common文件夹中的common.php文件(如果没有请创建)添加如下代码:

<?<span php
</span><span /*</span><span *********
* 发送邮件 *</span>
Copy after login
<span * by www.jbxue.com</span>
Copy after login
<span *********</span><span */</span>
Copy after login
<em id="__mceDel"><span function</span> SendMail(<span $address</span>,<span $title</span>,<span $message</span><span )
{
vendor(</span>'PHPMailer.class#PHPMailer'<span );
</span><span $mail</span>=<span new</span><span  PHPMailer();
</span><span //</span><span  设置PHPMailer使用SMTP服务器发送Email</span>
<span $mail</span>-><span IsSMTP();
</span><span //</span><span  设置邮件的字符编码,若不指定,则为'UTF-8'</span>
<span $mail</span>->CharSet='UTF-8'<span ;
</span><span //</span><span  添加收件人地址,可以多次使用来添加多个收件人</span>
<span $mail</span>->AddAddress(<span $address</span><span );
</span><span //</span><span  设置邮件正文</span>
<span $mail</span>->Body=<span $message</span><span ;
</span><span //</span><span  设置邮件头的From字段。</span>
<span $mail</span>->From=C('MAIL_ADDRESS'<span );
</span><span //</span><span  设置发件人名字</span>
<span $mail</span>->FromName='LilyRecruit'<span ;
</span><span //</span><span  设置邮件标题</span>
<span $mail</span>->Subject=<span $title</span><span ;
</span><span //</span><span  设置SMTP服务器。</span>
<span $mail</span>->Host=C('MAIL_SMTP'<span );
</span><span //</span><span  设置为&ldquo;需要验证&rdquo;</span>
<span $mail</span>->SMTPAuth=<span true</span><span ;
</span><span //</span><span  设置用户名和密码。</span>
<span $mail</span>->Username=C('MAIL_LOGINNAME'<span );
</span><span $mail</span>->Password=C('MAIL_PASSWORD'<span );
</span><span //</span><span  发送邮件。</span>
<span return</span>(<span $mail</span>-><span Send());
}
</span>?></em>
Copy after login

编辑Conf目录下的config.php,在return array添加如下内容

<?<span php
</span>'MAIL_ADDRESS'=>'xxx@126.com', <span //</span><span  邮箱地址</span>
'MAIL_SMTP'=>'smtp.126.com', <span //</span><span  邮箱SMTP服务器</span>
'MAIL_LOGINNAME'=>'xxx', <span //</span><span  邮箱登录帐号</span>
'MAIL_PASSWORD'=>'xxx', <span //</span><span  邮箱密码</span>
Copy after login

第四步、在Action中发送邮件
由于ThinkPHP会自动加载common.php中的函数,所以在需要发送邮件的时候,只需要使用如下代码即可。
SendMail("xxx@xxx.com","邮件标题","邮件正文");
至此,本教程已经结束。撒花~~~
欢迎成功配置的同学SendMail("dreamrunner@foxmail.com","我也会用ThinkPHP发送邮件啦","哇啦哇啦~~");
还有一些说明
如果邮件正文中需要换行怎么办呢?其实,最简单的方法是——
SendMail("xxx@xxx.com","邮件标题","邮件正文
我换行啦~~!");
QQ邮箱(含foxmail)和网易的126、163都测试成功。因为近期访问GMail比较吃力,所以没有测试。听说GMail要求必须使用SSL,有这方面需要的同学自行谷歌百度PHPMailer的方法。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template