博客搬家了,欢迎大家关注,https://bobjin.com
最近看到一个好的php邮件库,与phpmailer作用一样,但性能比phpmailer好,尤其是在处理附件的能力上,发送邮件成功的几率也高。
github地址:https://github.com/swiftmailer/swiftmailer.git
下面介绍一个用法:
1 require_once ("lib/swift_required.php"); 2 3 // 创建Transport对象,设置邮件服务器和端口号,并设置用户名和密码以供验证 4 $transport = Swift_SmtpTransport::newInstance('smtp.163.com', 25) 5 ->setUsername('username@163.com') 6 ->setPassword('password'); 7 8 // 创建mailer对象 9 $mailer = Swift_Mailer::newInstance($transport); 10 11 // 创建message对象 12 $message = Swift_Message::newInstance(); 13 14 // 设置邮件主题 15 $message->setSubject('这是一份测试邮件') 16 17 // 设置邮件内容,可以省略content-type 18 ->setBody( 19 '<html>' . 20 ' <head></head>' . 21 ' <body>' . 22 ' Here is an image <img src="' . // 内嵌文件 23 $message->embed(Swift_Image::fromPath('image.jpg')) . 24 '" alt="Image" />' . 25 ' Rest of message' . 26 '<a href="http://www.baidu.com">百度</a>'. 27 ' </body>' . 28 '</html>', 29 'text/html' 30 ); 31 32 // 创建attachment对象,content-type这个参数可以省略 33 $attachment = Swift_Attachment::fromPath('image.jpg', 'image/jpeg') 34 ->setFilename('cool.jpg'); 35 36 // 添加附件 37 $message->attach($attachment); 38 39 // 用关联数组设置收件人地址,可以设置多个收件人 40 $message->setTo(array('to@qq.com' => 'toName')); 41 42 // 用关联数组设置发件人地址,可以设置多个发件人 43 $message->setFrom(array( 44 'from@163.com' => 'fromName', 45 )); 46 47 // 添加抄送人 48 $message->setCc(array( 49 'Cc@qq.com' => 'Cc' 50 )); 51 52 // 添加密送人 53 $message->setBcc(array( 54 'Bcc@qq.com' => 'Bcc' 55 )); 56 57 // 设置邮件回执 58 $message->setReadReceiptTo('receipt@163.com'); 59 60 // 发送邮件 61 $result = $mailer->send($message);
博客搬家了,欢迎大家关注,https://bobjin.com