亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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

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

Dec 05, 2016 pm 01:26 PM

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】本文實現(xiàn)的是 smtp協(xié)議的163郵箱發(fā)送。其他郵箱和協(xié)議請參考: http://blog.wpjam.com/m/gmail-qmail-163mail-imap-smtp-pop3/
 //3.【文本編碼】請保證 utf-8的文本編碼,否則郵件會出現(xiàn)亂碼
 //4.【運行方式】 直接調(diào)用 smtp_mail()函數(shù)即可
  
   
//測試郵件
// 參數(shù)說明(收件人郵箱地址, 收件人姓名, 郵件主題, 郵件內(nèi)容, 附加信息, 發(fā)送人用戶名)  </span>
smtp_mail("receiveUser@haodf.com", 'receiveUserName', "【標題】12.01 測試郵件", "【內(nèi)容】測試郵件", "", <span style="color: #800080;">$fromUsername</span>="郵件發(fā)送人"<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;"> 發(fā)件人郵箱    </span>
    <span style="color: #800080;">$mail</span>->FromName =  <span style="color: #800080;">$fromUsername</span>;  <span style="color: #008000;">//</span><span style="color: #008000;"> 發(fā)件人    </span>
    <span style="color: #800080;">$mail</span>->CharSet = "UTF-8";   <span style="color: #008000;">//</span><span style="color: #008000;"> 這里指定字符集!    指定UTF-8后郵件的標題和發(fā)件人等等不會亂碼,如果是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 換行字數(shù)    
    //$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;"> 郵件內(nèi)容    </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 of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72