Pear Mail send mail with attachments _php tutorial

Source: Internet
Author: User
Add an attachment
Add an attachment
Adding One or more attachments is simple, adding an attachment is by calling the AddAttachment method, which can be called multiple times to add more than one attachemnts.

Boolean addattachment ($ file string, string [$ c_type = ' application/eight-bit byte stream '], string [$ name =], Boolean [$ isfile = True], string [$ encoded = ' one base64 '])
Variable:

$ file: Either a variable contains the contents of a file, or the path to the file itself
$ c_type: Content type, which means, for example, the MIME type of the file. text/plain, TEXT/CSV format, application/PDF format
$ name: The name of the file that you want it to appear in the email, which should be unique
$ isfile: Whether the variable $ file is the path to the contents of the file or file
$ code: This usually should be left by default unless you know what you're doing
An attachment can be a file system stored in a variable, or in a file on the server. in this first example, I will create a small text file named ' Hello Text.txt ' instead of ' 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. ' Subject ' = $subject
  14. );
  15. //Creating the Mime message
  16. $mime = New Mail_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 with calling AddAttachment. In this example, I'll send an e-mail 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,
    ' Subject ' = $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);
    ?>


http://www.bkjia.com/phpjc/444991.html www.bkjia.com true http://www.bkjia.com/phpjc/444991.html techarticle add an attachment add one or more attachments it's simple to add an attachment, By calling the AddAttachment method, this method can be called multiple times to add more than one attachemnts. 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.