>php教程 >php手册 >Pear Mail 发送邮件带附件

Pear Mail 发送邮件带附件

WBOY
WBOY원래의
2016-06-13 11:23:001083검색

添加附件
添加一个附件
添加一或多个附件很简单,添加附件,是通过调用addAttachment方法,这种方法可以多次调用添加多个attachemnts。

布尔addAttachment($文件的字符串,字符串[$ c_type ='应用程序/八位字节流'],串[$名称=],布尔[$ isfile =真],字符串[$编码='一个base64'])
变量:

$文件:要么变量包含一个文件的内容,或文件本身的路径
$ c_type:内容类型,这意味着,例如文件的MIME类型。
text / plain的,文字/ CSV格式,应用/ PDF格式
$名称:该文件的名称,您希望它出现在电子邮件,这应该是唯一的
$ isFile:是否变量$文件是对文件或文件的内容的路径
$编码:这通常应为默认离开,除非你知道你在做什么
附件可以是在一个变量,或在服务器上的文件中存储的文件系统。在这第一个例子中,我将建立一个小型文本文件名为'你好text.txt'改为'你好世界!也。

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


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:php 发送sms实例代码다음 기사:php实现多线程