分享一個php郵件庫-swiftmailer

WBOY
發布: 2016-07-29 08:58:21
原創
1121 人瀏覽過

最近看到一個好的php郵件庫,與phpmailer作用一樣,但效能比phpmailer好,尤其是在處理附件的能力上,發送郵件成功的幾率也高。以下介紹一個用法:

<span> 1</span><span>require_once</span> ("lib/swift_required.php"<span>);
</span><span> 2</span><span> 3</span><span>//</span><span> 创建Transport对象,设置邮件服务器和端口号,并设置用户名和密码以供验证</span><span> 4</span><span>$transport</span> = Swift_SmtpTransport::newInstance('smtp.163.com', 25<span>)
</span><span> 5</span> ->setUsername('username@163.com'<span>)
</span><span> 6</span> ->setPassword('password'<span>);
</span><span> 7</span><span> 8</span><span>//</span><span> 创建mailer对象</span><span> 9</span><span>$mailer</span> = Swift_Mailer::newInstance(<span>$transport</span><span>);
</span><span>10</span><span>11</span><span>//</span><span> 创建message对象</span><span>12</span><span>$message</span> = Swift_Message::<span>newInstance();
</span><span>13</span><span>14</span><span>//</span><span> 设置邮件主题</span><span>15</span><span>$message</span>->setSubject('这是一份测试邮件'<span>)
</span><span>16</span><span>17</span><span>//</span><span> 设置邮件内容,可以省略content-type</span><span>18</span> -><span>setBody(
</span><span>19</span>     '<html>' .
<span>20</span>     ' <head></head>' .
<span>21</span>     ' <body>' .
<span>22</span>     ' Here is an image <img src="' . <span>//</span><span> 内嵌文件</span><span>23</span><span>$message</span>->embed(Swift_Image::fromPath('image.jpg')) .
<span>24</span>     '" alt="分享一个php邮件库——swiftmailer" />' .
<span>25</span>     ' Rest of message' .
<span>26</span>     '<a href="http://www.baidu.com">百度</a>'.
<span>27</span>     ' </body>' .
<span>28</span>     '</html>',
<span>29</span>     'text/html'
<span>30</span><span>);
</span><span>31</span><span>32</span><span>//</span><span> 创建attachment对象,content-type这个参数可以省略</span><span>33</span><span>$attachment</span> = Swift_Attachment::fromPath('image.jpg', 'image/jpeg'<span>)
</span><span>34</span> ->setFilename('cool.jpg'<span>);
</span><span>35</span><span>36</span><span>//</span><span> 添加附件</span><span>37</span><span>$message</span>->attach(<span>$attachment</span><span>);
</span><span>38</span><span>39</span><span>//</span><span> 用关联数组设置收件人地址,可以设置多个收件人</span><span>40</span><span>$message</span>->setTo(<span>array</span>('to@qq.com' => 'toName'<span>));
</span><span>41</span><span>42</span><span>//</span><span> 用关联数组设置发件人地址,可以设置多个发件人</span><span>43</span><span>$message</span>->setFrom(<span>array</span><span>(
</span><span>44</span>     'from@163.com' => 'fromName',
<span>45</span><span>));
</span><span>46</span><span>47</span><span>//</span><span> 添加抄送人</span><span>48</span><span>$message</span>->setCc(<span>array</span><span>(
</span><span>49</span>       'Cc@qq.com' => 'Cc'
<span>50</span><span> ));
</span><span>51</span><span>52</span><span>//</span><span> 添加密送人</span><span>53</span><span>$message</span>->setBcc(<span>array</span><span>(
</span><span>54</span>       'Bcc@qq.com' => 'Bcc'
<span>55</span><span>));
</span><span>56</span><span>57</span><span>//</span><span> 设置邮件回执</span><span>58</span><span>$message</span>->setReadReceiptTo('receipt@163.com'<span>);
</span><span>59</span><span>60</span><span>//</span><span> 发送邮件</span><span>61</span><span>$result</span> = <span>$mailer</span>->send(<span>$message</span>);
登入後複製

以上就介紹了分享一個php郵件庫-swiftmailer,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板