分享一個php郵件庫——swiftmailer


博客搬家了,歡迎大家關注,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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM