Home  >  Article  >  Backend Development  >  Share a php mail library-swiftmailer

Share a php mail library-swiftmailer

WBOY
WBOYOriginal
2016-07-29 08:58:211156browse

I recently saw a good php mail library, which has the same function as phpmailer, but its performance is better than phpmailer, especially in terms of its ability to process attachments, and the probability of sending emails successfully is also high. Here is a usage:

<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>);

The above introduces the sharing of a PHP mail library - swiftmailer, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn