Thinkphp结合composer实现smtp发送邮件

发送邮件也算是网站的常用功能之一,相信很多人已经在网上找到相应的源码(我以前也是用的那套源码,代码很老了),为了避免重复造轮子和节约时间,使用composer上的nette/mail包。

版本要求:

php:5.3.1+ nette/mail:2.3(更高的版本要php5.6+) thinkphp:5.0+

关于composer的安装百度上有很多,这里不讲述

安装nette/mail

composer require nette/mail

使用

所有参数编码使用UTF-8

邮件参数

use Nette\Mail\Message;

$mail = new Message;
$mail->setFrom('John <john@example.com>')//发件人
    ->addTo('peter@example.com') // 收件人
    ->addTo('jack@example.com') // 添加收件人
    ->setSubject('Order Confirmation') // 邮件主题
    ->setBody("Hello, Your order has been accepted."); // 邮件内容

html格式的邮件:

$mail->setHtmlBody('<h1>内容</h1>');

发送邮件

use Nette\Mail\SendmailMailer;
$mailer = new SendmailMailer;
$mailer->send($mail);

先使用Message类填写邮件信息,使用SendmailMailer类发送邮件

邮件配置

$mailer = new Nette\Mail\SmtpMailer([
        'host' => 'smtp.gmail.com', // smtp主机地址
        'username' => 'john@gmail.com', // smtp账户
        'password' => '*****', // smtp密码
        // ssl配置信息,如果没有使用ssl形式发送只要上面三个参数
        'secure' => 'ssl',
        'context' =>  [
            'ssl' => [
                'capath' => '/path/to/my/trusted/ca/folder',
             ],
        ],
]);
$mailer->send($mail);

下面是在我项目中结合使用的,可以结合自己的项目修改

namespace library\service;
use app\admin\logic\ConfigLogic;
use Nette\Mail\Message;
use Nette\Mail\SmtpMailer;
use Nette\Utils\AssertionException;
use think\Exception;

class EmailService {

    // SMTP服务器
    private $server;

    // SMTP服务器的端口
    private $port = 25;

    // SMTP服务器的用户邮箱
    private $email;

    //SMTP服务器的用户帐号
    private $username;

    //SMTP服务器的用户密码
    private $password;

    // 发件人姓名
    private $name = '';

    public function __construct($config = []) {
        // ConfigLogic是数据库操作,也就是从数据库中读取配置
        if (empty($config)) {
            // 加载默认配置
            $model  = new ConfigLogic();
            $config = $model->getSmtpConfig();
        }
        if (isset($config['server'])) $this->server = $config['server'];
        if (isset($config['port'])) $this->port = $config['port'];
        if (isset($config['email'])) $this->email = $config['email'];
        if (isset($config['username'])) $this->username = $config['username'];
        if (isset($config['password'])) $this->password = $config['password'];
        if (isset($config['name'])) $this->name = $config['name'];
    }


    /**
     * send: 发送邮件
     * @param string $accept 接收人
     * @param string $title 邮件标题
     * @param string $content 邮件内容
     * @return bool 发送成功返回true
     */
    public function send($accept, $title, $content) {
        $mail = new Message();
        try {
            $mail->setFrom($this->name . ' <' . $this->email . '>')
                ->addTo($accept)
                ->setSubject($title)
                ->setBody($content);

            $mailer = new SmtpMailer([
                'host'     => $this->server,
                'username' => $this->username,
                'password' => $this->password,
            ]);
            $mailer->send($mail);
            return true;
        } catch (AssertionException $e) {
            return false;
        }
    }
}
Comments: 0

「人生在世,留句话给我吧」

提交评论