Home >Backend Development >PHP Tutorial >How to send emails and attachments with PHP using PHPMailer

How to send emails and attachments with PHP using PHPMailer

小云云
小云云Original
2018-03-27 09:52:406669browse

PHPMailer is an encapsulated PHP mail sending class that supports sending emails with HTML content and can add attachments for sending. Unlike PHP itself, the mail() function does not require server environment support. You only need to set up the mail server to relevant Information can be used to send emails.

First you need to download the PHPMailer class package: click to download

The php code is implemented as follows:

<?php
require(&#39;class.phpmailer.php&#39;);
$mail = new PHPMailer(); //实例化
$mail->IsSMTP(); // 启用SMTP
$mail->Host = "smtp.163.com"; //SMTP服务器 163邮箱例子
//$mail->Host = "smtp.126.com"; //SMTP服务器 126邮箱例子
//$mail->Host = "smtp.qq.com"; //SMTP服务器 qq邮箱例子
$mail->Port = 25;  //邮件发送端口
$mail->SMTPAuth   = true;  //启用SMTP认证
$mail->CharSet  = "UTF-8"; //字符集
$mail->Encoding = "base64"; //编码方式
$mail->Username = "abc@163.com";  //你的邮箱
$mail->Password = "xxx";  //你的密码
$mail->Subject = "xxx你好"; //邮件标题
$mail->From = "abc@163.com";  //发件人地址(也就是你的邮箱)
$mail->FromName = "xxx";   //发件人姓名
$address = "xxx@qq.com";//收件人email
$mail->AddAddress($address1, "xxx1");    //添加收件人1(地址,昵称)
$mail->AddAddress($address2, "xxx2");    //添加收件人2(地址,昵称)
$mail->AddAttachment(&#39;xx.xls&#39;,&#39;我的附件.xls&#39;); // 添加附件,并指定名称
$mail->AddAttachment(&#39;xx1.xls&#39;,&#39;我的附件1.xls&#39;); // 可以添加多个附件
$mail->AddAttachment(&#39;xx2.xls&#39;,&#39;我的附件2.xls&#39;); // 可以添加多个附件
$mail->IsHTML(true); //支持html格式内容
$mail->AddEmbeddedImage("logo.jpg", "my-attach", "logo.jpg"); //设置邮件中的图片
$mail->Body = &#39;你好, <b>朋友</b>! <br/>这是一封邮件!&#39;; //邮件主体内容
//发送
if(!$mail->Send()) {
  echo "发送失败: " . $mail->ErrorInfo;
} else {
  echo "成功";
}
?>

Attached: Commonly used email servers ( SMTP, POP3) address, port

sina.com: 
POP3服务器地址:pop3.sina.com.cn(端口:110) SMTP服务器地址:smtp.sina.com.cn(端口:25)   
sinaVIP: 
POP3服务器:pop3.vip.sina.com (端口:110) SMTP服务器:smtp.vip.sina.com (端口:25)  
sohu.com: 
POP3服务器地址:pop3.sohu.com(端口:110) SMTP服务器地址:smtp.sohu.com(端口:25)  
126邮箱: 
POP3服务器地址:pop.126.com(端口:110) SMTP服务器地址:smtp.126.com(端口:25)  
139邮箱: 
POP3服务器地址:POP.139.com(端口:110) SMTP服务器地址:SMTP.139.com(端口:25)  
163.com: 
POP3服务器地址:pop.163.com(端口:110) SMTP服务器地址:smtp.163.com(端口:25)  
QQ邮箱  
POP3服务器地址:pop.qq.com(端口:110) 
SMTP服务器地址:smtp.qq.com (端口:25)  
QQ企业邮箱 
POP3服务器地址:pop.exmail.qq.com (SSL启用 端口:995) SMTP服务器地址:smtp.exmail.qq.com(SSL启用 端口:587/465)  
yahoo.com: 
POP3服务器地址:pop.mail.yahoo.com SMTP服务器地址:smtp.mail.yahoo.com  
yahoo.com.cn: 
POP3服务器地址:pop.mail.yahoo.com.cn(端口:995) SMTP服务器地址:smtp.mail.yahoo.com.cn(端口:587

Related recommendations:

PHPMailer implements the function of sending emails using QQ mailbox

PHPMailer sends in PHP Email method analysis

phpmailer implements the function of sending emails

The above is the detailed content of How to send emails and attachments with PHP using PHPMailer. For more information, please follow other related articles on the PHP Chinese website!

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