【问题标题】:How to add attachment to this PHP mail() code? [duplicate]如何在这个 PHP mail() 代码中添加附件? [复制]
【发布时间】:2013-12-16 22:57:22
【问题描述】:

我有一些我喜欢的代码。在我安装的任何服务器上,它都可以完美运行多年。我喜欢它的地方在于它只有几行代码,我可以将电子邮件发送给任何人,他们总能收到,而且我不必为 SMTP 用户/密码而烦恼。

出于我的目的,我不需要复杂的脚本。我已经下载并安装了所有邮件程序(PHPmailer),但与我的约 10 行代码相比,这对我来说是同样的事情,100,000 行代码尝试做但不起作用,因为网站上的 zip 文件丢失了文件,文件位于错误的文件夹中,或者出于安全原因“禁用”某些功能,这些示例都不起作用,这意味着我必须深入研究,浏览所有代码并修复示例等...

我希望有一个更简单的解决方案。我的代码如下所示:

<?
$to = "to@mail.com";
$from = "from@mail.com";
$email_subject = "Test";
$email_message .= "Hello, \n\n";

$headers = 'From: '.$from."\r\n". 'Reply-To: '.$to."\r\n" . 'X-Mailer: PHP/' . phpversion();
@mail($to, $email_subject, $email_message, $headers);
?>

那是六行代码。

我知道我可以在接下来的 18 个小时里在 Google 上搜索这个答案,但我想如果来这里问这个问题不会太麻烦:

假设我有完全相同的 6 行代码,但我还需要附加一个与 PHP 文件位于同一文件夹中的文件以发送电子邮件。

$file = dirname(__FILE__).'doc.pdf';

是否有一种快速、经济的方法来修改用于发送消息的原始六行代码,以作为附件添加到 PDF 文件中?

【问题讨论】:

标签: php


【解决方案1】:

我检查了这里发布的关于这个问题的所有答案,但没有一个很好。

这对你有用:

<?

// fill in the basics here

$to = 'anyone@gmail.com';
$from = 'nobody@gmail.com';
$subject = 'Top Secret';
$message = 'Emails R Us';

// THIS is where you reference the file(s) to be attached: the first part is the     location and file name, the next part is the title in the email for the attachment; you can have more than one file (separate by comma). avoid large files (over 1MB)

$attachments = array(
'basic.pdf' => 'The File Title', // can be any extension
);

$headers = array(
'Reply-to' => 'rto@x.com',
'CC' => 'cc@y.com'
'BCC' => 'bcc@z.com'
    );

// end basics

// now the function that assembles the email and sends it


function mailAttachments($to, $from, $subject, $message, $attachments = array(), $headers = array(), $additional_parameters = '') {
$headers['From'] = $from;
$mime_boundary = '==MIME_BOUNDARY_' . md5(time());
$headers['MIME-Version'] = '1.0';
$headers['Content-Type'] = 'multipart/mixed; boundary="' . $mime_boundary . '"';
$headers_string = '';
foreach($headers as $header_name => $header_value) {
    if(!empty($headers_string)) {
        $headers_string .= "\r\n";
    }
    $headers_string .= $header_name . ': ' . $header_value;
}

$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";

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'); 
        $file_size = filesize($local_filename); 
        $data = @fread($fp, $file_size); 
        $data = chunk_split(base64_encode($data)); 

        $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";
    }
}


$message_string .= '--' . $mime_boundary . '--';


return mail($to, $subject, $message_string, $headers_string, $additional_parameters);
}

// end function

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 2013-03-20
    • 1970-01-01
    • 2010-10-28
    • 2021-01-06
    • 2014-07-18
    • 2017-03-04
    相关资源
    最近更新 更多