Home Backend Development PHP Tutorial Use php to send emails with attachments-(Analysis of examples using PHPMailer)_PHP tutorial

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

Jul 21, 2016 pm 03:12 PM
php phpmailer code use send copy Case Analysis have use e-mail of appendix

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...
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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles