Php 发送电子邮件的简单脚本?

Php 发送电子邮件的简单脚本?,php,sendmail,content-type,Php,Sendmail,Content Type,我使用代码发送电子邮件: <?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."; ?&g

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

<?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:fatal:chdir/Library/Server/Mail/Data/spool:没有这样的文件或目录

我不知道如何添加附件,重要的是我需要指定附件的内容类型,有人能帮我吗


谢谢

您的服务器(Mac?)配置错误,您必须先修复该错误,然后才能发送邮件。然后研究如何使用PHPmailer或Swiftmailer发送电子邮件-使用just mail()发送附件非常痛苦且容易出错。@MarcB处理附件和各种mime组无疑是一种有趣的体验;-)
<?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);

?>