我有一些我喜欢的代码。多年来,它在我所戴的任何服务器上运行良好。我喜欢它只是几行代码,我可以发送电子邮件给任何人,他们总是得到它,我不必打扰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文件中?
答案 0 :(得分:0)
我在这个问题中检查了所有答案,但没有一个是那么好。
这对你有用:
<?
// 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
?>