thinkphp 整合 swiftmailer 实现邮件发送

thinkphp swiftmailer(phpmailer)

文件夹结构

这里写图片描写叙述
图 1 swiftmailer-phpmailer

将swiftmailer整合到thinkphp中。如上图 1
我下载的版本号是 swiftmailer-5.x, 将文件夹里面的lib文件夹重命名为swiftmailer,并复制到ThinkPHP/Library/Vendor文件夹下,如上图 1

配置

这里写图片描写叙述
图 2 phpmailer swiftmailer 配置对照

// thinkphp config.php
// 配置swiftmailer邮件发送server
'SWIFT_HOST'     => 'smtp.qq.com',
'SWIFT_USERNAME' => '1071766043@qq.com',
'SWIFT_PASSWORD' => 'your-password',
从上面的对照能够看出,swiftmailer相比較于phpmailer来说配置简洁

使用

// 在须要使用的时候直接调用以下(图)的send_email函数就可以,
// 可是须要注意函数的返回值。由于能够依据返回值来确定是否发送成功
send_email('2577792479@qq.com', 'your-email-subject', 'your-email-content');

这里写图片描写叙述
图 3

相同的,在自己定义的**全局**function.php文件里,
定义一个通过swiftmailer发送邮件的**全局**函数,
方便直接调用,代码例如以下:
<?

php // Application/Common/Common/function.php /** * send email by swiftmailer * * @param string|array $to 收件人 * @param string $subject 主题 * @param string $content 内容 * @return int 发送的邮件数目 */ function send_email($to, $subject, $content) { vendor('swiftmailer.swift_required'); $transport = Swift_SmtpTransport::newInstance(C('SWIFT_HOST'), 25) ->setUsername(C('SWIFT_USERNAME')) ->setPassword(C('SWIFT_PASSWORD')); $mailer = Swift_Mailer::newInstance($transport); $message = Swift_Message::newInstance() ->setSubject($subject) ->setFrom(array(C('SWIFT_USERNAME') => 'safari_shi')) ->setTo($to) ->setBody($content, 'text/html', 'utf-8'); return $mailer->send($message); }

原文地址:https://www.cnblogs.com/mfmdaoyou/p/6962410.html