0
我正在使用PHP的郵件功能來發送電子郵件,但我將需要附加一個文件與該消息。發送文件附件思考PHP的郵件()函數
大多數情況下,我相信將要附加的項目將成爲一個文本文件,如果必須的話,我可以將內容複製到電子郵件中,但我擔心最終文件類型將變成PDF,Word文檔等,因爲要上傳文件的人不知道純文本和他們自己的格式之間的區別。
我正在使用PHP的郵件功能來發送電子郵件,但我將需要附加一個文件與該消息。發送文件附件思考PHP的郵件()函數
大多數情況下,我相信將要附加的項目將成爲一個文本文件,如果必須的話,我可以將內容複製到電子郵件中,但我擔心最終文件類型將變成PDF,Word文檔等,因爲要上傳文件的人不知道純文本和他們自己的格式之間的區別。
有這麼進入了很多,我會強烈建議有一個包,爲您完成所有如PHPMailer
有您能花時間做更重要的事情工作的工作,別人有已經完成了這項工作。
<?php
$to = '[email protected]';
$from = '[email protected]';
$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' => '[email protected]',
'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); }
的可能的複製[如何發送使用附件PHP電子郵件(http://stackoverflow.com/questions/2027069)。看看[這個答案](http://stackoverflow.com/questions/2027069/how-to-send-email-with-attachment-using-php/2027112#2027112)。 – netcoder 2010-12-01 21:45:49
你可能想看看SwiftMailer。 http://swiftmailer.org – Jonah 2010-12-01 21:47:37