Home> php教程> php手册> body text

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

WBOY
Release: 2016-12-05 13:26:24
Original
1749 people have browsed it

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:

php //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()函数即可 //测试邮件 // 参数说明(收件人邮箱地址, 收件人姓名, 邮件主题, 邮件内容, 附加信息, 发送人用户名)  smtp_mail("receiveUser@haodf.com", 'receiveUserName', "【标题】12.01 测试邮件", "【内容】测试邮件", "", $fromUsername="邮件发送人"); echo "
end
" ; function smtp_mail( $receiveEmailAddress, $receiveUserName, $subject, $body, $extraHdrs='', $fromUsername){ $path = 'PHPMailer-master/'; require_once($path."class.smtp.php"); require($path."class.phpmailer.php"); $mail = new PHPMailer(); $mail->IsSMTP(); // send via SMTP //这里使用 163邮箱 $mail->Host = "smtp.163.com"; // SMTP servers $mail->SMTPAuth = true; // turn on SMTP authentication $mail->Username = "yourEmailUserName"; // SMTP username 注意:普通邮件认证不需要加 @域名 这里是我的163邮箱 $mail->Password = "yourEmailPassWord"; // SMTP password 在这里输入邮箱的密码 $mail->From = $fromMailAddress = "yourName@163.com"; // 发件人邮箱 $mail->FromName = $fromUsername; // 发件人 $mail->CharSet = "UTF-8"; // 这里指定字符集! 指定UTF-8后邮件的标题和发件人等等不会乱码,如果是GB2312标题会乱码 $mail->Encoding = "base64"; $mail->AddAddress($receiveEmailAddress, $receiveUserName); // 收件人邮箱和姓名 $mail->AddReplyTo($fromMailAddress, $fromUsername); //$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 // 邮件主题 $mail->Subject = $subject; // 邮件内容 $mail->Body = $body; //$mail->AltBody ="text/html"; if(!$mail->Send()) { echo "error

"; echo "error: " . $mail->ErrorInfo; exit; } else { echo"success!"; } }

Copy after login

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

source:php.cn
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
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!