0

如何在 PHP 中发送带有附件的邮件?

我想使用纯 php 而不是任何库...

这是我已经尝试过的:

<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. Each line should be separated with \n
$message = "Hello World!\n\nThis is my first mail.";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
4

3 回答 3

4

试试这个:

<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/zip; name="attachment.zip" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?> 

参考:http ://webcheatsheet.com/PHP/send_email_text_html_attachment.php

于 2013-08-14T10:35:06.433 回答
1

您可以尝试以下方法,但首先您需要对这些进行更改:

$name = 'image.jpg';
// The path to the image with the file name:
$fileatt = "images/".$name;

在下面的代码中:(预先测试)

<?php

// Set who this goes to:
$to = array('Your Name','email@example.com');
// Give the message a subject:
$subject = 'Some subject';
// Set who this message is from:
$from = array("My Company", "email@example.com");

// Create the header information:
$random_hash = md5(date('r', time())); 
$mime_boundary = "==Multipart_Boundary_x{$random_hash}x"; 
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: multipart/mixed; boundary="'.$mime_boundary.'"' . "\r\n";
$headers .= 'To: '.$to[0].' <'.$to[1].'>' . "\r\n";
$headers .= 'From: '.$from[0].' <'.$from[1].'>' . "\r\n";

// Build the message (can have HTML in it) message:
$message = 'This is an <b>HTML message</b> it comes with an attachment!'."\n\n".
// Do not edit this part of the message:
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// The image that you would like to send (filename only):
$name = 'image.jpg';
// The path to the image with the file name:
$fileatt = "images/".$name;

$fileatt_type = "application/octet-stream"; // File Type
// Filename that will be used for the file as the attachment
$fileatt_name = $name; 

// Read the file attachment:
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file); 

// Create sendable information
$data = chunk_split(base64_encode($data));

// Finalize the message with attachment
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}\n";
unset($data);
unset($file);
unset($fileatt);
unset($fileatt_type);
unset($fileatt_name);

// Send the message:
mail($to[1], $subject, $message, $headers);

echo "Email sent";

?> 
于 2013-08-14T10:44:11.277 回答
0

您需要使用base64编码才能附加文件。所以这里有一个更简单的代码。

<?php
$to = "someone@somewhere.com";
$subject = "Email with an Attachment with no Special libraries";
$random_hash = md5(date('r', time()));
$headers = "From: someid@yoursite.com\r\nReply-To: noreply@so.com";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents("pics.zip")));
$output = "somecontent";
echo mail($to, $subject, $output, $headers);
?>
于 2013-08-14T10:40:57.190 回答