PearMail email with attachment _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PearMail sends an email with an attachment. It is easy to add one or more attachments by adding an attachment. you can call the addAttachment method multiple times to add multiple attachemets. Boolean add attachment
Add an attachment
It is easy to add one or more attachments. to add attachments, you can call the addAttachment method multiple times to add multiple attachemets.

Boolean addAttachment ($ File string, string [$ c_type = 'application/eight-byte stream'], string [$ name =], Boolean [$ isfile = true], string [$ encoding = 'base64'])
Variable:

$ File: either the variable contains the content of a file or the path of the file.
$ C_type: content type, which means, for example, the MIME type of a file. Text/plain, text/CSV format, application/PDF format
$ Name: name of the file, which must be unique if you want it to appear in an email.
$ IsFile: whether the variable $ File is the path to the file or file content
$ Encoding: this should usually be left by default unless you know what you are doing
An attachment can be a file system stored in a variable or file on the server. In the first example, I changed the name of a small text file 'Your text.txt 'to 'Hello World! Also.

  1. Include ('mail. php ');
  2. Include ('mail/mime. php ');
  3. // Constructing the email
  4. $ Sender = "Leigh "; // Who your name and email address
  5. $ Recipient = "Leigh "; // The Recipients name and email address
  6. $ Subject = "Test Email ";// Subject for the email
  7. $ Text = 'This is a text message .';// Text version of the email
  8. $ Html ='

    This is a html message

    ';// HTML version of the email
  9. $ Crlf ="N";
  10. $ Headers = array (
  11. 'From' => $ sender,
  12. 'Return-path' => $ sender,
  13. 'Subobject' => $ Subject
  14. );
  15. // Creating the Mime message
  16. $ Mime =NewMail_mime ($ crlf );
  17. // Setting the body of the email
  18. $ Mime-> setTXTBody ($ text );
  19. $ Mime-> setHTMLBody ($ html );
  20. // Add an attachment
  21. $ File = "Hello World! ";// Content of the file
  22. $ File_name = "Hello text.txt ";// Name of the Attachment
  23. $ Content_type = "text/plain ";// Content type of the file
  24. $ Mime-> addAttachment ($ file, $ content_type, $ file_name, 0 );// Add the attachment to the email
  25. $ Body = $ mime-> get ();
  26. $ Headers = $ mime-> headers ($ headers );
  27. // Sending the email
  28. $ Mail = & Mail: factory ('mail ');
  29. $ Mail-> send ($ recipient, $ headers, $ body );
  30. ?>

    Add multiple attachments
    As in the previous section, adding multiple attachments is rasy and calling addAttachment. In this example, I will send an email with two text attachments.

    Include ('mail. php ');
    Include ('mail/mime. php ');

    // Constructing the email
    $ Sender = "Leigh "; // Who your name and email address
    $ Recipient = "Leigh "; // The Recipients name and email address
    $ Subject = "Test Email"; // Subject for the email
    $ Text = 'This is a text message. '; // Text version of the email
    $ Html ='

    This is a html message

    '; // HTML version of the email
    $ Crlf = "n ";
    $ Headers = array (
    'From' => $ sender,
    'Return-path' => $ sender,
    'Subobject' => $ Subject
    );

    // Creating the Mime message
    $ Mime = new Mail_mime ($ crlf );

    // Setting the body of the email
    $ Mime-> setTXTBody ($ text );
    $ Mime-> setHTMLBody ($ html );

    // Add an attachment
    $ File = "Hello World! "; // Content of the file
    $ File_name = "Hello text.txt"; // Name of the Attachment
    $ Content_type = "text/plain"; // Content type of the file
    $ Mime-> addAttachment ($ file, $ content_type, $ file_name, 0); // Add the attachment to the email

    // Add a second attachment
    $ File = "Hello World! Again :) "; // Content of the file
    $ File_name = "Hello text 2.txt"; // Name of the Attachment
    $ Content_type = "text/plain"; // Content type of the file
    $ Mime-> addAttachment ($ file, $ content_type, $ file_name, 0); // Add the attachment to the email

    $ Body = $ mime-> get ();
    $ Headers = $ mime-> headers ($ headers );

    // Sending the email
    $ Mail = & Mail: factory ('mail ');
    $ Mail-> send ($ recipient, $ headers, $ body );
    ?>


Adding one or more attachments to an attachment is simple. adding an attachment is done by calling the addAttachment method. this method can be called multiple times to add multiple attachemets. Boolean...

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.