我尝试使用PHP在我的网站上发送自动创建的电子邮件。经过一番努力,我最终得到了这个脚本,但由于某种原因,我得到了一个错误,我不知道为什么。
function sendMail($_POST) {
$fileatt = "http://www.mysite.com/contract.pdf";
$fileatt_type = "application/pdf";
$fileatt_name = "contract.pdf";
$email_from = "info@mysite.com";
$email_subject = "Your Contract";
$email_message = "Thanks for your booking! Here is your contract";
$email_to = $_POST['mail']; // Who the email is to
$headers = "From: ".$email_from;
$file = fopen($fileatt,'r');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "nMIME-Version: 1.0n" .
"Content-Type: multipart/mixed;n" .
" boundary="{$mime_boundary}"";
$email_message .= "This is a multi-part message in MIME format.nn" .
"--{$mime_boundary}n" .
"Content-Type:text/html; charset="iso-8859-1"n" .
"Content-Transfer-Encoding: 7bitnn" .
$email_message .= "nn";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}n" .
"Content-Type: {$fileatt_type};n" .
" name="{$fileatt_name}"n" .
//"Content-Disposition: attachment;n" .
//" filename="{$fileatt_name}"n" .
"Content-Transfer-Encoding: base64nn" .
$data .= "nn" .
"--{$mime_boundary}--n";
$ok = @mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
echo "You file has been sent to the email address you specified";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
}
并且该脚本确实发送电子邮件并包含附件,但附件的大小为 0kb。如果我看到我的错误,这是有道理的:
Warning: filesize(): stat failed for http://www.mysite.com/test.pdf in /customers/9/7/5/mysite.com/functions.php on line 712
Warning: fread(): Length parameter must be greater than 0 in /customers/9/7/5/mysite.com/functions.php on line 712
有人可以向我提供更多有关如何解决此问题的信息吗?
这可能是由于您尝试上传的文件过大。在服务器配置中增加"Max_upload_filesize"。
我认为最简单的解决方案是使用像PHPMailer或SwiftMailer这样的脚本。这是大多数人所做的,并消除了使用PHP发送电子邮件的困难。尝试在 PHP 中使用 mail()
函数而不成为专家是一个杀手锏!
您尝试过的附件有多大?也许您应该增加上传文件大小限制,请参阅 http://drupal.org/node/97193