Maison > développement back-end > tutoriel php > 分享一个php邮件库——swiftmailer

分享一个php邮件库——swiftmailer

WBOY
Libérer: 2016-07-29 08:58:21
original
1161 Les gens l'ont consulté

最近看到一个好的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>     '' .
<span>20</span>     ' ' .
<span>21</span>     ' ' .
<span>22</span>     ' Here is an image <img src="'%20.%20<span>//</span><span>%20%E5%86%85%E5%B5%8C%E6%96%87%E4%BB%B6</span><span>23</span><span>%24message</span>->embed(Swift_Image::fromPath('image.jpg'))%20.%0A<span>24</span>%20%20%20%20%20'" alt="分享一个php邮件库——swiftmailer">' .
<span>25</span>     ' Rest of message' .
<span>26</span>     '<a href="http://www.baidu.com">百度</a>'.
<span>27</span>     ' ' .
<span>28</span>     '',
<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>);
Copier après la connexion

以上就介绍了分享一个php邮件库——swiftmailer,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal