How does PHPMailer send emails with attachments in php?

Source: Internet
Author: User
Tags php file php and port number server port

First go to http://phpmailer.worxware.com/download the latest version of the package
2. After the download is complete, find the class. phpmailer. php and class. smtp. php classes in your own directory!
3. Create a new php file named phpmail_jiucool.php.
4. phpmail_jiucool.php content is as follows:


Instance

The code is as follows: Copy code

Function postmail_jiucool_com ($ to, $ subject = "", $ body = ""){
// Author: Jiucool WebSite: http://www.jiucool.com
// $ To indicates the recipient address $ subject indicates the Mail Title $ body indicates the mail body
// Error_reporting (E_ALL );
Error_reporting (E_STRICT );
Date_default_timezone_set ("Asia/Shanghai"); // Set the time zone UTC + 8.
Require_once ('class. phpmailer. Php ');
Include ("class. smtp. php ");
$ Mail = new PHPMailer (); // A new PHPMailer object is displayed.
$ Body = eregi_replace ("[]", '', $ body); // filter the email content.
$ Mail-> CharSet = "UTF-8"; // Set the mail encoding, the default ISO-8859-1, this must be set if you send Chinese, otherwise garbled
$ Mail-> IsSMTP (); // you can specify the SMTP service.
$ Mail-> SMTPDebug = 1; // enable the SMTP debugging function
// 1 = errors and messages
// 2 = messages only
$ Mail-> SMTPAuth = true; // enable SMTP verification
$ Mail-> SMTPSecure = "ssl"; // security protocol
$ Mail-> Host = "smtp.googlemail.com"; // SMTP server
$ Mail-> Port = 465; // SMTP server Port number
$ Mail-> Username = "SMTP server Username"; // SMTP server Username
$ Mail-> Password = "SMTP server Password"; // SMTP server Password
$ Mail-> SetFrom ('sender address, for example, admin # jiucool.com # Replace with @ ', 'sender name ');
$ Mail-> AddReplyTo ("email reply address, such as admin # jiucool.com # changed to @", "email reply recipient name ");
$ 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 = $;
$ Mail-> AddAddress ($ address, "recipient name ");
// $ 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! Congratulations, the email is sent successfully! ";
        }
    }

Note:

.
For example

The code is as follows: Copy code
$ Mail-> AddAttachment ('include/id.csv ', 'Att ');//

Attachment path and name
If an attachment is sent to a similar attachment, the final attachment can be att.txt.
AddAttachment can be used to set the attachment encoding method and attachment type. For example, the attachment can also be set

The code is as follows: Copy code
$ Mail-> AddAttachment ('include/id.csv', 'att.csv ', "binary", "text/comma-separated-values ");//

Path and name of the attachment,
The attachment encoding methods include:Supports 8-bit, base64, binary, and quoted-printable encoding.
 
The MIME Type acceptable to CSV
· Application/octet-stream
· Text/comma-separated-values (recommended)
· Text/csv
Therefore, the attachment type of csv files can be any of the above three types.

Instance

The code is as follows: Copy code

Require_once ('include/PHPMailer/class. phpmailer. Php'); // import the PHPMAILER class
$ Mail = new PHPMailer (); // Create an instance
$ Mail-> CharSet = 'utf-8'; // sets the character set.
$ Mail-> SetLanguage ('ch', 'include/PHPMailer/language/'); // sets the Directory of the language type and language file
$ Mail-> IsSMTP (); // send using SMTP
$ Mail-> SMTPAuth = true; // Sets 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; // The sender's EMAIL address.
$ Mail-> FromName = 'jasonxu '; // The user name of the sender in the SMTP host
$ Mail-> Username = SMTP_USER_NAME; // sender's name
$ Mail-> Password = SMTP_USER_PASS; // The sender's Password on the SMTP host
$ Mail-> Subject = 'Test mail title'; // mail Subject
$ Mail-> AltBody = 'text/html '; // sets the alternate display when the email body does not support HTML.
$ Mail-> Body = 'Test mail content'; // Make the mail content
$ Mail-> IsHTML (true); // whether it is an HTML email
$ Mail-> AddAddress ('chinajason2008 # gmail.com ', 'jasonxu'); // recipient's address and name
$ Mail-> AddReplyTo ('chinajason2008 # gmail.com ', 'jasonxu'); // address and name replied to by the recipient
$ Mail-> AddAttachment ('include/id.csv', 'att.csv '); // The path and name of the attachment
If (! $ Mail-> Send () // Send an email
Var_dump ($ mail-> ErrorInfo); // view the error message sent


If you add this item, you can send an attachment to the instance mailbox.

$ Mail-> AddAttachment ('include/id.csv', 'att.csv ');//Attachment path and name

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.