2012-08-17 49 views
1

我用代码来发送电子邮件:简单的脚本发送电子邮件?

<?php 
$to = "[email protected]"; 
$subject = "Test mail"; 
$message = "Hello! This is a simple email message."; 
$from = "[email protected]"; 
$headers = "From:" . $from; 
mail($to,$subject,$message,$headers); 
echo "Mail Sent."; 
?> 

但我得到:

的sendmail:致命的:CHDIR /库/服务器/邮件/数据/阀芯:没有这样的文件或目录

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

谢谢!

+0

您使用的是Linux还是OSX? – mittmemo 2012-08-17 03:40:36

+2

您的服务器(Mac?)配置错误,您必须先解决该问题,然后才能发送邮件。然后使用PHPmailer或Swiftmailer查看电子邮件 - 使用mail()发送附件非常痛苦且容易出错。 – 2012-08-17 03:46:19

+0

@MarcB它绝对是一个有趣的体验,处理附件和各种mime组合自己虽然;-) – 2012-08-17 04:04:06

回答

2
<?php 

$to = "[email protected]"; 
$subject = "Test mail"; 
$from = "[email protected] <[email protected]>"; 
$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); 

?> 
相关问题