Use PHP to send an email with attachments--(Phpmailer used for instance analysis) _php instance

Source: Internet
Author: User
Tags base64 php send mail

Copy Code code as follows:

/*phpmailer is a PHP function package that is used to send e-mail. The features it provides include:
*. Specify multiple recipients, CC addresses, dark addresses, and reply-to addresses when sending mail
*. Support for multiple message codes including: 8bit,base64,binary and Quoted-printable
*. Support SMTP Authentication
*. Support for redundant SMTP servers
*. Support for messages with attachments and HTML-formatted messages
*. Custom message headers
*. Support for embedding pictures in messages
*. Flexible Commissioning
*. tested and compatible SMTP servers include: Sendmail,qmail,postfix,imail,exchange, etc.
*. can be run on any platform

Phpmailer is a very powerful php send mail class, you can set the sending email address, reply address, message 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 an instance
$mail-> charset= ' utf-8 '; Setting the character set
$mail-> setlanguage (' ch ', ' include/phpmailer/language/'); Set the directory where language types and language files are located
$mail-> issmtp (); Send using SMTP method
$mail-> Smtpauth = true; To 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 email Address
$mail-> fromname = ' Jasonxu '; The sender's user name 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 message title '; Message subject
$mail-> altbody = ' text/html '; Set up an alternate display when the message body does not support HTML
$mail-> BODY = ' Test the content of the message ';//the contents of the message are made
$mail-> ishtml (true); Whether it is an HTML message
$mail-> addaddress (' chinajason2008#gmail.com ', ' Jasonxu '); The address and name of the recipient
$mail-> addreplyto (' chinajason2008#gmail.com ', ' Jasonxu '); Address and name replied to when the recipient replies
$mail-> addattachment (' include/id.csv ', ' att.csv ');//attachment path and attachment name
if (! $mail-> Send ())//Send mail
Var_dump ($mail-> errorinfo); To view sent error messages

Note: Phpmailer If you add an attachment, you must specify the suffix of the attachment in the name of the attachment, if you do not specify the attachment suffix, the default attachment suffix will be. txt.
such as $mail-> addattachment (' include/id.csv ', ' att '); the path and attachment name of the attachment
If you add an attachment to the above, you may end up receiving an attachment that is att.txt.
AddAttachment can set the attachment encoding and attachment type, such as the attachment added above can also be set to
$mail-> addattachment (' include/id.csv ', ' att.csv ', ' binary ', ' text/comma-separated-values '); the path and attachment name of the attachment,
There are probably several ways to encode attachments: 8bit, base64, binary, and quoted-printable encodings.

and CSV acceptable MIME Type
· Application/octet-stream
· Text/comma-separated-values (recommended)
· Text/csv
Therefore, the attachment type of the CSV format file can be any of the above three types

An example of a message sent in a previous project, organize a thumbnail version for easy application:

Copy Code code 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 send mail by SMTP
To set the address of an SMTP mail server $mail->host=$_smtp_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 mailbox address
$mail->fromname=$_smtp_from_name;//to set the sender's name
$mail->username=$_smtp_username;
$mail->password=$_smtp_password;
$mail->addaddress ("$email", "")//Set the address of the Inbox (parameter 1) and name (parameter 2)
$mail->smtpauth=true;//to open SMTP authentication
$mail->subject=$_smtp_subject;//to set the title of the message
$mail->altbody= "text/html";
$mail->body= $body;//message contents
$mail->ishtml (TRUE);//Set whether the content is HTML type
$mail->wordwrap=50; Set the number of characters per line
$mail->addreplyto ("samzhang@tencent.com", "Samzhang"); Set the address of the recipient of the reply
$mail->smtpdebug=0;
if ($mail->send ()) {//Send mail
Exit ' OK ';
}else{
Exit ' fail ';
}

Probably remember the first time used Phpmailer, there have been inexplicable wonderful problems, but also spent a lot of time on the Internet to find information, the final solution. Currently remember that the server PHP environment can not prohibit the Fsockopen function, or mail can not send, but there are solutions. In short, a beginning to use, there is always a bad, because the time is long, now want to, also do not know what the specific changes. Therefore, will now use the Phpmailer directory file packaging uploaded to Csdn, also for the convenience of later use it, but also convenient for the trouble of friends. Phpmailer Download: Http://xiazai.jb51.net/201304/yuanma/PHPMailer_jb51net.rar
In addition, the contents of the problem included at that time were sorted as follows:

1, error:could not connect to SMTP host
Reason 1: Do not mail system requirements of the SMTP request is not the same, but all allow uppercase, some do not support lowercase, such as NetEase, Tencent's mailbox. (As for this, I have not tested, anyway, to uppercase, also does not affect)

Workaround:

Copy Code code as follows:

Public Function issmtp () {
$this->mailer = ' SMTP '; ->smtp SMTP, which is lowercase and now uppercase.
}

Choose the mailer and send through it
Switch ($this->mailer) {
Case ' SendMail ':
return $this->sendmailsend ($header, $body);
Case ' SMTP '://The same SMTP->smtp, that is, lowercase, now capitalized.
return $this->smtpsend ($header, $body);
Case ' mail ':
Default
return $this->mailsend ($header, $body);
}


2, SMTP error:could not connect to SMTP host
Reason: Some virtual host, or server, for security, shielding the "fsockopen () function" caused the message could not be sent

Workaround:

Enable Fsockopen function

First, remove the following two semicolon from the 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:

Incorrect settings, I used Gmail to do some basic testing, need to set other parameters at the time.

Workaround:

$mail->smtpsecure = ' TLS '; I just need to add this sentence.

Note: I have not encountered this error, so in the above example, this content is added to the annotation. If you encounter this error, you can use this sentence to try.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.