【问题标题】:Send File Attachment Thought PHP's mail() function发送文件附件思想 PHP 的 mail() 函数
【发布时间】:2011-05-18 18:48:57
【问题描述】:

我正在使用 PHP 的邮件功能来发送电子邮件,但我需要在邮件中附加一个文件。

在大多数情况下,我相信要附加的项目将是一个文本文件,如果必须,我可以将内容回显到电子邮件中,但我担心最终文件类型将变成 PDF、Word 文档等,因为上传文件的人不知道纯文本和他们自己的格式之间的区别。

【问题讨论】:

标签: php email


【解决方案1】:

其中有很多内容,我强烈建议使用可以为您完成所有工作的软件包,例如 PHPMailer

您可以花时间做一些更重要的事情,其他人已经完成了这项工作。

【讨论】:

    【解决方案2】:
    <?php
    $to = 'destination-address@somewhere.com';
    $from = 'source@somewhere.com';
    $subject = 'See Attachments';
    $message = 'Please review the following attachments.';
    
    // Define a list of FILES to send along with the e-mail. Key = File to be sent. Value = Name of file as seen in the e-mail.
    $attachments = array(
        '/tmp/WEDFRTS' => 'first-attachment.png',
        '/tmp/some-other-file' => 'second-attachment.png'
    );
    
    // Define any additional headers you may want to include
    $headers = array(
        'Reply-to' => 'source@somewhere.com',
        'Some-Other-Header-Name' => 'Header Value'
    );
    
    $status = mailAttachments($to, $from, $subject, $message, $attachments, $headers);
    if($status === True) {
        print 'Successfully mailed!';
    } else {
        print 'Unable to send e-mail.';
    }
    
    
    
    
    function mailAttachments($to, $from, $subject, $message, $attachments = array(), $headers = array(), $additional_parameters = '') {
        $headers['From'] = $from;
    
        // Define the boundray we're going to use to separate our data with.
        $mime_boundary = '==MIME_BOUNDARY_' . md5(time());
    
        // Define attachment-specific headers
        $headers['MIME-Version'] = '1.0';
        $headers['Content-Type'] = 'multipart/mixed; boundary="' . $mime_boundary . '"';
    
        // Convert the array of header data into a single string.
        $headers_string = '';
        foreach($headers as $header_name => $header_value) {
            if(!empty($headers_string)) {
                $headers_string .= "\r\n";
            }
            $headers_string .= $header_name . ': ' . $header_value;
        }
    
        // Message Body
        $message_string  = '--' . $mime_boundary;
        $message_string .= "\r\n";
        $message_string .= 'Content-Type: text/plain; charset="iso-8859-1"';
        $message_string .= "\r\n";
        $message_string .= 'Content-Transfer-Encoding: 7bit';
        $message_string .= "\r\n";
        $message_string .= "\r\n";
        $message_string .= $message;
        $message_string .= "\r\n";
        $message_string .= "\r\n";
    
        // Add attachments to message body
        foreach($attachments as $local_filename => $attachment_filename) {
            if(is_file($local_filename)) {
                $message_string .= '--' . $mime_boundary;
                $message_string .= "\r\n";
                $message_string .= 'Content-Type: application/octet-stream; name="' . $attachment_filename . '"';
                $message_string .= "\r\n";
                $message_string .= 'Content-Description: ' . $attachment_filename;
                $message_string .= "\r\n";
    
                $fp = @fopen($local_filename, 'rb'); // Create pointer to file
                $file_size = filesize($local_filename); // Read size of file
                $data = @fread($fp, $file_size); // Read file contents
                $data = chunk_split(base64_encode($data)); // Encode file contents for plain text sending
    
                $message_string .= 'Content-Disposition: attachment; filename="' . $attachment_filename . '"; size=' . $file_size.  ';';
                $message_string .= "\r\n";
                $message_string .= 'Content-Transfer-Encoding: base64';
                $message_string .= "\r\n\r\n";
                $message_string .= $data;
                $message_string .= "\r\n\r\n";
            }
        }
    
        // Signal end of message
        $message_string .= '--' . $mime_boundary . '--';
    
        // Send the e-mail.
        return mail($to, $subject, $message_string, $headers_string, $additional _parameters); }
    

    reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 2017-08-08
      • 2013-04-13
      相关资源
      最近更新 更多