Home > Backend Development > PHP Tutorial > Pear Mail Send email with attachments_PHP tutorial

Pear Mail Send email with attachments_PHP tutorial

WBOY
Release: 2016-07-20 11:07:00
Original
972 people have browsed it

Add attachment
Add an attachment
Add one or Multiple attachments are very simple. To add attachments, you call the addAttachment method. This method can be called multiple times to add multiple attachments.

boolean addAttachment($file string, string[$c_type='application/octetstream'], string[$name= ], boolean [$isfile = true], string [$encoding = 'a base64'])
Variables:

$file: Either the 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, app/pdf
$name: the name of the file you Want it to appear in the email, this should be unique
$ isFile: Whether the variable $file is the path to the file or the contents of the file
$encoding: This should usually be left as the default unless you know what you are doing
Attachments can be stored in a variable, or in a file on the server File system. In this first example, I will create a small text file named 'Hello text.txt' and change it to 'Hello world! Also.

  1.         include('Mail.php');
  2.         include('Mail/mime.php');
  3.  
  4.         // Constructing the email
  5.         $sender = "Leigh ";                              // Who your name and email address
  6.         $recipient = "Leigh ";                           // The Recipients name and email address
  7.         $subject = "Test Email";                                            // Subject for the email
  8.         $text = 'This is a text message.';                                  // Text version of the email
  9.         $html = '

    This is a html message

    '
    ;  // HTML version of the email
  10.         $crlf = "n";
  11.         $headers = array(
  12.                         'From'          => $sender,
  13.                         'Return-Path'   => $sender,
  14.                         'Subject'       => $subject
  15.                         );
  16.  
  17.         // Creating the Mime message
  18.         $mime = new Mail_mime($crlf);
  19.  
  20.         // Setting the body of the email
  21.         $mime->setTXTBody($text);
  22.         $mime->setHTMLBody($html);
  23.  
  24.         // Add an attachment
  25.         $file = "Hello World!";                                      // Content of the file
  26.         $file_name = "Hello text.txt";                               // Name of the Attachment
  27.         $content_type = "text/plain";                                // Content type of the file
  28.         $mime->addAttachment ($file, $content_type, $file_name, 0);  // Add the attachment to the email
  29.  
  30.         $body = $mime->get();
  31.         $headers = $mime->headers($headers);
  32.  
  33.         // Sending the email
  34.         $mail =& Mail::factory('mail');
  35.         $mail->send($recipient, $headers, $body);
  36. ?>

    添加多个附件
    正如上一节,添加多个附件是rasy与调用addAttachment了。在这个例子中,我会发送一个带有两个文本附件的电子邮件。

            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);
    ?>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444991.htmlTechArticle添加附件 添加一个附件 添加一或多个附件很简单,添加附件,是通过调用addAttachment方法,这种方法可以多次调用添加多个attachemnts。 布尔...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template