Use php to send emails with attachments-(Analysis of examples using PHPMailer)_PHP tutorial

WBOY
Release: 2016-07-21 15:12:03
Original
1144 people have browsed it

Copy code The code is as follows:

/*PHPMailer is a PHP function package for sending emails. The functions it provides include:
*. Specify multiple recipients, CC addresses, BCC addresses and reply addresses when sending emails
*. Supports multiple email encodings including: 8bit, base64, binary and quoted-printable
*. Support SMTP authentication
*. Support redundant SMTP server
*. Support emails with attachments and emails in Html format
*. Customize email headers
*. Support embedding images in emails
*. Flexible debugging
*. Tested and compatible SMTP servers include: Sendmail, qmail, Postfix, Imail, Exchange, etc.
*. Can run on any platform

phpMailer is a very powerful php email class. You can set the sending email address, reply address, email subject, rich text content, upload attachments,...
Official website: http:// phpmailer.worxware.com/
Download address: http://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list
*/
require_once('include/ PHPMailer/class.phpmailer.php'); //Import PHPMAILER class
$mail = new PHPMailer(); //Create instance
$mail -> CharSet='utf-8'; //Set characters Set
$mail -> SetLanguage('ch','include/PHPMailer/language/'); //Set the language type and the directory where the language file is located    
$mail -> IsSMTP(); //Use Send via SMTP
$mail -> SMTPAuth = true; //Set whether the server requires SMTP authentication
$mail -> Host = SMTP_SERVER; //SMTP host address
$mail -> Port = SMTP_SERVER_PORT; //SMTP host port
$mail -> From = SMTP_USER_MAIL; //Sender's EMAIL address
$mail -> FromName = 'jasonxu'; //The sender is in the SMTP host Username
$mail -> Username = SMTP_USER_NAME; //Sender’s name
$mail -> Password = SMTP_USER_PASS; //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 = 'Content of test email'; //Mail content is made
$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'); //Reply when the recipient replies The given address and name
$mail -> AddAttachment('include/id.csv','att.csv');//The path and name of the attachment
if(!$mail -> Send ()) //Send mail
var_dump($mail -> ErrorInfo); //View the error message sent

Note: If phpmailer adds an attachment, it must be in the attachment name Please specify the attachment suffix. If you do not specify the attachment suffix, 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 attachment you finally receive may It is 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 probably several encoding methods for attachments: 8bit, base64, binary, and quoted-printable encoding is supported

The MIME Types accepted by CSV
· application/octet-stream
· text/comma-separated-values ​​(recommended)
· text/csv
So, the attachment type of csv format file can It is any one of the above three

An example of email sending in a previous project, compiled into an abbreviated version for easy application:

Copy the code The code is as follows:

$body=$_smtp_body;
$mail=new PHPMailer();//Get a PHPMailer instance
//$mail->SMTPSecure='tls';
$mail ->CharSet="utf-8"; //Set encoding
$mail->IsSMTP();//Set to use SMTP to send emails
$mail->Host=$_smtp_server;// Set the address of the SMTP mail server
$mail->Port=$_smtp_port; //Set the port of the mail server, the default is 25
$mail->From=$_smtp_from_mail; //Set the sender's port Email address
$mail->FromName=$_smtp_from_name;//Set the sender’s name
$mail->Username=$_smtp_username;
$mail->Password=$_smtp_password;
$mail->AddAddress("$email","");//Set the recipient's address (parameter 1) and name (parameter 2)
$mail->SMTPAuth=true;//Enable SMTP authentication
$mail->Subject=$_smtp_subject;//Set the title of the email
//$mail->AltBody="text/html";
$mail->Body=$ body;//Email content
$mail->IsHTML(true);//Set whether the content is html type
//$mail->WordWrap=50; 🎜>//$mail->AddReplyTo("samzhang@tencent.com","samzhang"); //Set the address of the reply recipient
$mail->SMTPDebug=0;
if ($mail->Send()){//Send mail
exit 'ok';
}else{
exit 'fail';
}

I probably remember that when I first used PHPMailer, I had an inexplicable problem. I spent a lot of time looking for information online before finally solving it. At present, I remember that the fsockopen function cannot be disabled in the server PHP environment, otherwise the email cannot be sent, but there are solutions. In short, there are always difficulties when using it at the beginning. Because it has been a long time, now that I think about it, I don’t know what specific changes were made. Therefore, package and upload the PHPMailer directory files currently being used to CSDN for the convenience of future use, and also for the convenience of friends who are troubled by this matter. PHPMailer download: http://xiazai.jb51.net/201304/yuanma/PHPMailer_jb51net.rar
In addition, the contents of the problems that occurred at that time are organized as follows:

1. Error: Could not connect to SMTP host
Reason 1: The SMTP requests required by the mail system are different, but all allow uppercase letters, and some do not support lowercase letters, such as NetEase and Tencent mailboxes. (As for whether this is the case, I have not tested it. Anyway, it will not affect if it is changed to uppercase)
Solution:


Copy code The code is as follows:
public function IsSMTP() {
$this- >Mailer ='SMTP'; // Change smtp ->SMTP; that is, it was originally lowercase and now it is uppercase.
}
// Choose the mailer and send through it

switch($this->Mailer) {
case 'sendmail':
return $this->SendmailSend($header, $body) ;
case 'SMTP':// also changes smtp ->SMTP ; that is, it was originally lowercase, but now it is uppercase.
return $this->SmtpSend($header, $body);
case 'mail':
default:
return $this->MailSend($header, $body);
}

2. SMTP Error: Could not connect to SMTP host
Cause: Some virtual hosts or servers have blocked the "fsockopen() function" for security reasons, resulting in failure to send Email
Solution:

Enable fsockopen function

First, remove the two semicolons below in php.ini

;extension=php_sockets.dll

;extension=php_openssl.dll

Replace fsockopen function

You can replace the fsockopen function in the class.smtp.php file with the pfsockopen function

3. Could not instantiate mail function
Reason:

The parameters set are incorrect. I used gmail to do some basic tests and need to set other parameters again.

Solution:

$mail->SMTPSecure = ‘tls’; //Just add this sentence

Note: I have never encountered this kind of error, so in the above example, I added comments to this content. If you encounter this kind of error, you can try this sentence.

http://www.bkjia.com/PHPjc/326777.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326777.htmlTechArticleCopy the code The code is as follows: /*PHPMailer is a PHP function package for sending emails. The functions it provides include: *. Specify multiple recipients, CC addresses, BCC addresses when sending emails...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!