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

How to send emails with attachments using PHPMailer in php_PHP Tutorial

WBOY
Release: 2016-07-13 16:56:15
Original
1201 people have browsed it

It is a common practice to use PHPMailer to send emails in PHP development. This method is much easier to use than PHP mail. Let’s take a look at how PHPMailer sends emails with attachments.

. First go to http://phpmailer.worxware.com/ to download the latest version of the package
2. After the download is completed, find the two classes class.phpmailer.php and class.smtp.php and put them in your own directory!
3. Then create a new php file and name it: phpmail_jiucool.php
4.phpmail_jiucool.php content is as follows:


Example

 代码如下 复制代码

function postmail_jiucool_com($to,$subject = "",$body = ""){
    //Author:Jiucool WebSite: http://www.jiucool.com
    //$to 表示收件人地址 $subject 表示邮件标题 $body表示邮件正文
    //error_reporting(E_ALL);
    error_reporting(E_STRICT);
    date_default_timezone_set("Asia/Shanghai");//设定时区东八区
    require_once('class.phpmailer.php');
    include("class.smtp.php");
    $mail             = new PHPMailer(); //new一个PHPMailer对象出来
    $body             = eregi_replace("[]",'',$body); //对邮件内容进行必要的过滤
    $mail->CharSet ="UTF-8";//设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
    $mail->IsSMTP(); // 设定使用SMTP服务
    $mail->SMTPDebug  = 1;                     // 启用SMTP调试功能
                                           // 1 = errors and messages
                                           // 2 = messages only
    $mail->SMTPAuth   = true;                  // 启用 SMTP 验证功能
    $mail->SMTPSecure = "ssl";                 // 安全协议
    $mail->Host       = "smtp.googlemail.com";      // SMTP 服务器
    $mail->Port       = 465;                   // SMTP服务器的端口号
    $mail->Username   = "SMTP服务器用户名";  // SMTP服务器用户名
    $mail->Password   = "SMTP服务器密码";            // SMTP服务器密码
    $mail->SetFrom('发件人地址,如admin#jiucool.com #换成@', '发件人名称');
    $mail->AddReplyTo("邮件回复地址,如admin#jiucool.com #换成@","邮件回复人的名称");
    $mail->Subject    = $subject;
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer! - From www.jiucool.com"; // optional, comment out and test
    $mail->MsgHTML($body);
    $address = $to;
    $mail->AddAddress($address, "收件人名称");
    //$mail->AddAttachment("images/phpmailer.gif");      // attachment
    //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
    if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!恭喜,邮件发送成功!";
        }
    }

Note:

When adding an attachment to phpmailer, the attachment suffix must be specified in the attachment name. If the attachment suffix is ​​not specified, the default attachment suffix will be .txt.
For example

 代码如下 复制代码
$mail -> AddAttachment('include/id.csv','att');//

The path and name of the attachment
If you add an attachment and send it as above, the final attachment you receive may be att.txt.
AddAttachment can set the attachment encoding method and attachment type. For example, the above attachment addition can also be set to

 代码如下 复制代码
$mail -> AddAttachment('include/id.csv','att.csv',"binary","text/comma-separated-values");//

The path and name of the attachment,
There are several encoding methods for attachments: Supports 8bit, base64, binary, and quoted-printable encoding

And the MIME Type accepted by CSV
· application/octet-stream
· text/comma-separated-values ​​(recommended)
· text/csv
Therefore, the attachment type of csv format file can be any of the above three

Example

$mail -> SMTPAuth = true; //Set whether the server requires SMTP authentication $mail -> Host = SMTP_SERVER; //SMTP host address
The code is as follows
 代码如下 复制代码

require_once('include/PHPMailer/class.phpmailer.php'); //导入PHPMAILER类
$mail = new PHPMailer(); //创建实例
$mail -> CharSet='utf-8'; //设置字符集
$mail -> SetLanguage('ch','include/PHPMailer/language/');  //设置语言类型和语言文件所在目录         
$mail -> IsSMTP(); //使用SMTP方式发送
$mail -> SMTPAuth = true; //设置服务器是否需要SMTP身份验证 
$mail -> Host = SMTP_SERVER; //SMTP 主机地址 
$mail -> Port = SMTP_SERVER_PORT; //SMTP 主机端口
$mail -> From = SMTP_USER_MAIL; //发件人EMAIL地址
$mail -> FromName = 'jasonxu'; //发件人在SMTP主机中的用户名 
$mail -> Username = SMTP_USER_NAME; //发件人的姓名 
$mail -> Password = SMTP_USER_PASS; //发件人在SMTP主机中的密码 
$mail -> Subject = '测试邮件的标题'; //邮件主题 
$mail -> AltBody = 'text/html'; //设置在邮件正文不支持HTML时的备用显示
$mail -> Body = '测试邮件的内容';//邮件内容做成
$mail -> IsHTML(true);  //是否是HTML邮件
$mail -> AddAddress('chinajason2008#gmail.com','jasonxu'); //收件人的地址和姓名 
$mail -> AddReplyTo('chinajason2008#gmail.com','jasonxu'); //收件人回复时回复给的地址和姓名
$mail -> AddAttachment('include/id.csv','att.csv');//附件的路径和附件名称
if(!$mail -> Send()) //发送邮件 
var_dump($mail -> ErrorInfo);  //查看发送的错误信息

Copy code


require_once('include/PHPMailer/class.phpmailer.php'); //Import PHPMAILER class

$mail = new PHPMailer(); //Create instance $mail -> CharSet='utf-8'; //Set character set $mail -> SetLanguage('ch','include/PHPMailer/language/'); //Set the language type and the directory where the language file is located    

$mail -> IsSMTP(); //Send using SMTP
$mail -> Port = SMTP_SERVER_PORT; //SMTP host port $mail -> From = SMTP_USER_MAIL; //Sender’s EMAIL address $mail -> FromName = 'jasonxu'; //The sender’s username in the SMTP host $mail -> Username = SMTP_USER_NAME; //Sender’s name $mail -> Password = SMTP_USER_PASS; //The sender’s password in the SMTP host $mail -> Subject = 'Test email title'; //Email subject $mail -> AltBody = 'text/html'; //Set the backup display when the email body does not support HTML $mail -> Body = 'Test email content';//Email content is created $mail -> IsHTML(true); //Is it an HTML email $mail -> AddAddress('chinajason2008#gmail.com','jasonxu'); //Recipient's address and name $mail -> AddReplyTo('chinajason2008#gmail.com','jasonxu'); //The address and name to which the recipient replies when replying $mail -> AddAttachment('include/id.csv','att.csv');//The path and name of the attachment if(!$mail -> Send()) //Send email var_dump($mail -> ErrorInfo); //View the sent error message Just add this message and you can send an email with attachments $mail -> AddAttachment('include/id.csv','att.csv');//The path and name of the attachment

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631600.htmlTechArticleIt is a common practice to use PHPMailer to send emails in PHP development. This method is much easier to use than PHP mail. , let’s take a look at how PHPMailer sends emails with attachments. .First...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template