Home > Article > Backend Development > An in-depth discussion of the PHP email sending class PHPMailer_PHP Tutorial
PHPMailer is an email sending class specially used in PHP language. It is very powerful and enriches the single mail() function of PHP itself. Supports SMTP and more even attachments.
PHPMailer complies with the LGPL license and can be downloaded for free. The current version is PHPMailer v2.2.1, which was updated on November 16, 2007.
Download address: http://phpmailer.codeworxtech.com/index.php?pg=sf&p=dl
Note: When downloading, the suffixes are .tar.gz and .zip. In fact, they contain The files are all the same, but the compression format is different. .tar.gz has been double-compressed and the file is smaller. WinRAR software can decompress these two formats correctly.
◆How to use:
☆ Previous note: After decompression, there will be an examples folder in the file package, which contains "pop3_before_smtp_test.php" and "test1. php" two files. The first file teaches basic usage (see below), while "test1.php" mainly introduces some new features, which are detailed in "index.html" in the same folder. , and another file "contents.html" is the email content, which can be replaced with an email template in the future to increase the standardization and enrichment of the email.
☆Basic method: pop3_before_smtp_test.php
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>require ‘class.phpmailer.php’; </span></li><li class="alt"><span>require ‘class.pop3.php’; </span></li><li><span>$</span><span class="attribute">pop</span><span> = </span><span class="attribute-value">new</span><span> POP3(); </span></li><li class="alt"><span>// 建立对象,收邮件 </span></li><li><span>$pop-</span><span class="tag">></span><span>Authorise(’pop3.example.com’<br>, 110, 30, ‘mailer’, ‘password’, 1); </span></span></li> <li class="alt"><span>// 设置收邮件的用户信息 </span></li> <li><span>// pop3.example.com:改写成邮箱的 pop3 服务器 </span></li> <li class="alt"><span>// 例如,163 为 pop3.163.com </span></li> <li><span>// mailer:用户名 </span></li> <li class="alt"><span>// password:密码 </span></li> <li> <span>$</span><span class="attribute">mail</span><span> = </span><span class="attribute-value">new</span><span> PHPMailer(); </span> </li> <li class="alt"><span>// 建立对象,发邮件,如果仅仅是发邮件可以去掉上面 </span></li> <li><span>// pop3 部份以及对 class.pop3.php 的包含 </span></li> <li class="alt"> <span>$mail-</span><span class="tag">></span><span>IsSMTP(); </span> </li> <li><span>// 不变 </span></li> <li class="alt"> <span>$mail-</span><span class="tag">></span><span class="attribute">SMTPDebug</span><span> = </span><span class="attribute-value">2</span><span>; </span> </li> <li> <span>$mail-</span><span class="tag">></span><span>IsHTML(true); </span> </li> <li class="alt"><span>// 是否使支持 HTML 邮件的发送,默认为 false , </span></li> <li><span>// 为了方便后面使用“邮件模版”,我们把它改为 true </span></li> <li class="alt"> <span>$mail-</span><span class="tag">></span><span class="attribute">Host</span><span> = ‘relay.example.com’; </span> </li> <li><span>// 发邮件的服务器 </span></li> <li class="alt"><span>// 例如,163邮箱 为 smtp.163.com </span></li> <li> <span>$mail-</span><span class="tag">></span><span class="attribute">From</span><span> = ‘mailer@example.com’; </span> </li> <li class="alt"><span>// 你的邮箱 </span></li> <li> <span>$mail-</span><span class="tag">></span><span class="attribute">FromName</span><span> = ‘Example Mailer’; </span> </li> <li class="alt"><span>// 你的姓名 </span></li> <li> <span>$mail-</span><span class="tag">></span><span class="attribute">Subject</span><span> = ‘My subject’; </span> </li> <li class="alt"><span>// 邮件标题 </span></li> <li> <span>$mail-</span><span class="tag">></span><span class="attribute">Body</span><span> = ‘Hello world’; </span> </li> <li class="alt"><span>// 邮件内容,这里可以使用新特性调用邮件<br>模版,具体详情看后面。 </span></li> <li> <span>$mail-</span><span class="tag">></span><span>AddAddress(’name@anydomain.<br>com’, ‘First Last’); </span> </li> <li class="alt"><span>// 收件人邮箱和姓名 </span></li> <li> <span>if (!$mail-</span><span class="tag">></span><span>Send()) </span> </li> <li class="alt"><span>{ </span></li> <li> <span>echo $mail-</span><span class="tag">></span><span>ErrorInfo; </span> </li> <li class="alt"><span>} </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
I hope the knowledge of PHPMailer introduced above can be used as a reference learning material for everyone.