thinkphp5使用swiftmailer

来源:互联网 发布:php仿qq聊天系统 编辑:程序博客网 时间:2024/06/06 10:01

https://github.com/swiftmailer/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方法引入

thinphp5完全手册上引入自定义类库的方法-Loader

use think\Loader;Loader::import('swiftmailer.swift_required');use Swift_SmtpTransport;use Swift_Mailer;use Swift_Message;use Swift_Attachment;

然后新建一个方法如下:

    public function sendMail(){        $transport = Swift_SmtpTransport::newInstance('smtp.163.com', 25);        $transport->setUsername('13521146683@163.com');        $transport->setPassword('163的授权码');        $transport->setEncryption('tls');        $mailer = Swift_Mailer::newInstance($transport);        $message = Swift_Message::newInstance();        $message->setFrom(array('13521146683@163.com' => 'w'));        $message->setTo(array('810169879@qq.com' => 'Mr.Right', '13521146683@163.com' => '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目录下

0 0
原创粉丝点击