thinkphp5使⽤swiftmailer
⽅式1:php引⼊swiftmailer
(我的php安装在/usr/local/php⽬录下)
swiftmailer下载下来后解压,把swiftmailer-5.x/⽬录下的ib⽂件夹改名为swiftmailer,然后把swiftmailer放在centos7
的/usr/local/php/lib/php⽬录下。
然后再thinkphp5的任意⼀个controller中,做如下引⽤:
require_once 'swiftmainler/swift_required.php';
use Swift_SmtpTransport;
use Swift_Mailer;
use Swift_Message;
use Swift_Attachment;
⽅式2:thinkphp5引⼊swiftmailer
swiftmailer下载下来后解压,把swiftmailer-5.x/⽬录下的ib⽂件夹改名为swiftmailer,然后把swiftmailer放在thinkphp5根⽬录下的extend⽬录下,然后使⽤Loader⽅法引⼊
use think\Loader;
Loader::import('swiftmailer.swift_required');
use Swift_SmtpTransport;
use Swift_Mailer;
use Swift_Message;
use Swift_Attachment;
thinkphp3然后新建⼀个⽅法如下:
public function sendMail(){
$transport = Swift_SmtpTransport::newInstance('smtp.163', 25);
$transport->setUsername('135********@163');
$transport->setPassword('163的授权码');
$transport->setEncryption('tls');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message->setFrom(array('135********@163' => 'w'));
$message->setTo(array('810169879@qq' => 'Mr.Right', '135********@163' => 'Mr.Wrong'));//发给两个邮箱
$message->setSubject("任务反馈");//邮件标题
$message->setBody('这是⼀个测试', 'text/html', 'utf-8');//邮件内容
// $mailer->send($message);
$message->attach(Swift_Attachment::fromPath('public/test.jpg', 'image/jpeg')->setFilename('rename_pic.jpg'));//附件图⽚
try{
$mailer->send($message);
}
catch (Swift_ConnectionException $e){
echo 'There was a problem communicating with SMTP: ' . $e->getMessage();
}
}
其中附件图⽚放在了public⽬录下
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论