Home  >  Article  >  php教程  >  [Consolidate the foundation of PHP] Send emails with PHP (PHPMailer)

[Consolidate the foundation of PHP] Send emails with PHP (PHPMailer)

WBOY
WBOYOriginal
2016-12-05 13:26:241834browse

This article address

Reference address

Share the outline:

 1. Overview

 2. Write code to send emails

 3. Reference documents

1. Overview


This article talks about how to use the email class library PHPMailer to send emails.

 When we are doing projects, we often need the email function. In fact, the PHP language itself already has a method for sending emails (mail() method), not to mention that this method implements very few functions. If you want to use the mail() method to send emails, you must configure the SMTP server yourself. Here is I won’t talk about how to use mail() (the function call is indeed very simple). Therefore, we recommend using the second method: PHPMailer.

2. Write code to send email


 1)【Download PHPMailer】

                                                   First, go to http://phpmailer.sourceforge.net/ to download the latest PHPMailer package (PHPMailer method must use this package, currently based on gitHub).

You can also download the compressed package directly: https://github.com/wozhuzaisi/PHPMailer/archive/master.zip

 2)【Code Implementation】

After downloading, extract it to the corresponding directory. You can see class.phpmailer.php in the decompressed folder (you need to include this file when calling PHPMailer)

 Sample code:

 <?<span style="color: #000000;">php 
 </span><span style="color: #008000;">//</span><span style="color: #008000;">1.【下载地址】PHPMailer下载地址:https://github.com/wozhuzaisi/PHPMailer/archive/master.zip
 //2.【邮箱配置SMTP】本文实现的是 smtp协议的163邮箱发送。其他邮箱和协议请参考: http://blog.wpjam.com/m/gmail-qmail-163mail-imap-smtp-pop3/
 //3.【文本编码】请保证 utf-8的文本编码,否则邮件会出现乱码
 //4.【运行方式】 直接调用 smtp_mail()函数即可
  
   
//测试邮件
// 参数说明(收件人邮箱地址, 收件人姓名, 邮件主题, 邮件内容, 附加信息, 发送人用户名)  </span>
smtp_mail("receiveUser@haodf.com", 'receiveUserName', "【标题】12.01 测试邮件", "【内容】测试邮件", "", <span style="color: #800080;">$fromUsername</span>="邮件发送人"<span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> "<br>end<br>"<span style="color: #000000;"> ;
 
</span><span style="color: #0000ff;">function</span> smtp_mail( <span style="color: #800080;">$receiveEmailAddress</span>, <span style="color: #800080;">$receiveUserName</span>, <span style="color: #800080;">$subject</span>, <span style="color: #800080;">$body</span>, <span style="color: #800080;">$extraHdrs</span>='', <span style="color: #800080;">$fromUsername</span><span style="color: #000000;">){ 
    </span><span style="color: #800080;">$path</span> = 'PHPMailer-master/'<span style="color: #000000;">; 
    </span><span style="color: #0000ff;">require_once</span>(<span style="color: #800080;">$path</span>."class.smtp.php"<span style="color: #000000;">);    
    </span><span style="color: #0000ff;">require</span>(<span style="color: #800080;">$path</span>."class.phpmailer.php"<span style="color: #000000;">);
       
    </span><span style="color: #800080;">$mail</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> PHPMailer();    
    </span><span style="color: #800080;">$mail</span>->IsSMTP();                  <span style="color: #008000;">//</span><span style="color: #008000;"> send via SMTP
    //这里使用 163邮箱</span>
    <span style="color: #800080;">$mail</span>->Host = "smtp.163.com";   <span style="color: #008000;">//</span><span style="color: #008000;"> SMTP servers    </span>
    <span style="color: #800080;">$mail</span>->SMTPAuth = <span style="color: #0000ff;">true</span>;           <span style="color: #008000;">//</span><span style="color: #008000;"> turn on SMTP authentication </span>
    
    <span style="color: #800080;">$mail</span>->Username = "yourEmailUserName";     <span style="color: #008000;">//</span><span style="color: #008000;"> SMTP username  注意:普通邮件认证不需要加 @域名  这里是我的163邮箱</span>
    <span style="color: #800080;">$mail</span>->Password = "yourEmailPassWord"; <span style="color: #008000;">//</span><span style="color: #008000;"> SMTP password    在这里输入邮箱的密码</span>
    
    <span style="color: #800080;">$mail</span>->From = <span style="color: #800080;">$fromMailAddress</span> = "yourName@163.com";      <span style="color: #008000;">//</span><span style="color: #008000;"> 发件人邮箱    </span>
    <span style="color: #800080;">$mail</span>->FromName =  <span style="color: #800080;">$fromUsername</span>;  <span style="color: #008000;">//</span><span style="color: #008000;"> 发件人    </span>
    <span style="color: #800080;">$mail</span>->CharSet = "UTF-8";   <span style="color: #008000;">//</span><span style="color: #008000;"> 这里指定字符集!    指定UTF-8后邮件的标题和发件人等等不会乱码,如果是GB2312标题会乱码</span>
    <span style="color: #800080;">$mail</span>->Encoding = "base64"<span style="color: #000000;">;    
    </span><span style="color: #800080;">$mail</span>->AddAddress(<span style="color: #800080;">$receiveEmailAddress</span>, <span style="color: #800080;">$receiveUserName</span>);  <span style="color: #008000;">//</span><span style="color: #008000;"> 收件人邮箱和姓名    </span>
    <span style="color: #800080;">$mail</span>->AddReplyTo(<span style="color: #800080;">$fromMailAddress</span>, <span style="color: #800080;">$fromUsername</span><span style="color: #000000;">);    
    </span><span style="color: #008000;">//</span><span style="color: #008000;">$mail->WordWrap = 50; // set word wrap 换行字数    
    //$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment 附件    
    //$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    
    //$mail->IsHTML(true);  // send as HTML    
    // 邮件主题    </span>
    <span style="color: #800080;">$mail</span>->Subject = <span style="color: #800080;">$subject</span><span style="color: #000000;">;    
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 邮件内容    </span>
    <span style="color: #800080;">$mail</span>->Body = <span style="color: #800080;">$body</span><span style="color: #000000;">;                                                                          
    </span><span style="color: #008000;">//</span><span style="color: #008000;">$mail->AltBody ="text/html";    </span>
    <span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$mail</span>-><span style="color: #000000;">Send())    
    {    
        </span><span style="color: #0000ff;">echo</span> "error <p>"<span style="color: #000000;">;    
        </span><span style="color: #0000ff;">echo</span> "error: " . <span style="color: #800080;">$mail</span>-><span style="color: #000000;">ErrorInfo;    
        </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">;    
    }    
    </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {    
        </span><span style="color: #0000ff;">echo</span>"success!"<span style="color: #000000;">; 
    }    
}

  </span>

That’s it, criticisms and corrections are welcome

3. Reference documents


 1) Use PHPMailer to send emails

 2) PHP sends mail (PHPMailer) - FTD2012 - Blog Park

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