1

我使用代码发送电子邮件:

<?php
$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

但我得到了:

sendmail:致命:chdir /Library/Server/Mail/Data/spool:没有这样的文件或目录

而且我不知道如何添加附件,重要的是我需要为附件指定 Content-Type,有人可以帮助我吗?

谢谢!

4

1 回答 1

2
<?php 

$to = "someone@example.com";
$subject = "Test mail";
$from = "someonelse@example.com <someonelse@example.com>";
$message = "Hello! This is a simple email message.";

// let's say you want to email a comma-separated-values
$tofile = "col1,col2,col3\n";
$tofile .= "val1,val1,val1\n";


$filename  = "filename.csv";
$random_hash = md5(date('r', time())); 
$attachment = chunk_split(base64_encode($tofile)); 

$headers = "From: " . $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"\r\n"; 
$headers .= "X-Priority: 3\r\n";
$headers .= "X-MSMail-Priority: Normal\r\n";    
$headers .= "X-Mailer: PHP/" . phpversion();

$body = "--PHP-mixed-".$random_hash."\r\n";
$body .= "Content-Type: text/plain; charset=\"UTF-8\"\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n";
$body .= $message;
$body .= "\r\n\r\n";
$body .= "\r\n--PHP-mixed-" . $random_hash . "\r\n\r\n";

$body .= "--PHP-mixed-".$random_hash."\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Type: text/comma-separated-values; name=\"" . $filename . "\" charset=\"UTF-8\"\r\n"; 
$body .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n";
$body .= $attachment;
$body .= "\r\n--PHP-mixed-".$random_hash."--";

mail($to, $subject, $body, $headers);

?>
于 2012-08-17T07:55:17.313 回答