Php mail with attachment function.

Source: Internet
Author: User
Tags base64
The code is as follows: Copy code

$ Pipeline name = "testpipeline ";
$ Email = "test@test.com ";
$ Text = "Hello, the attachment contains the PDF file you need. Click to download. <Br> <a href = http://www.111cn.net> www.111cn.net </a> "; // text content

$ Text = base64_encode ($ text); // encode it using base64
$ Text = chunk_split ($ text); // cut the long string into 76 characters in each line.

$ Subject = $ subtitle name; // title
$ From = "admin@111cn.net"; // sender
$ To = $ email; // recipient

// Attachment
// Define the demarcation line
$ Boundary = "nextpart _". uniqid ("");
$ Boundary2 = "nextpart _". uniqid ("");
$ Headers = "to: $ torn ";
$ Headers. = "from: $ fromrn ";
$ Headers. = "mime-version: 1.0rn ";
$ Headers. = "content-type: multipart/mixed;
Boundary = "---- = _ $ boundary" rn ";

$ Read = file_get_contents ($ External name );

$ Read = base64_encode ($ read); // encode it using base64
$ Read = chunk_split ($ read); // cut the long string into 76 characters in each line.

// Now we can create the subject of the email
$ Body = "this is a multi-part message in mime format.

------ =_$ Boundary
Content-type: multipart/alternative;
Boundary = "---- = _ $ boundary2 ";

------ =_$ Boundary2
Content-type: text/html;
Charset = "gbk"
Content-transfer-encoding: base64

$ Text

------ = _ $ Boundary2 --

------ =_$ Boundary
Content-type: application/octet-stream;
Charset = "gbk ";
Name = "$ Your name"
Content-disposition: attachment; filename = "$ parameter name"
Content-transfer-encoding: base64

$ Read

------- = _ $ Boundary --";

If (mail ($ to, $ subject, $ body, $ headers ))
Echo "the PDF file (". $ your name. ") you need has been sent to your email:". $ .". <Br> check. ";
Else
Echo "Sorry, failed to send. <Br> ";


?>

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.