我尝试使用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.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$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: base64\n\n" .
$data .= "\n\n" .
"--{$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
有人能给我提供一些如何解决这个问题的更多信息吗?
发布于 2012-10-15 10:12:28
这可能是由于您尝试上载的文件过大。在您的服务器配置中增加'Max_upload_filesize‘。
发布于 2012-10-15 09:58:33
我认为最简单的解决方案是使用像PHPMailer或SwiftMailer这样的脚本。这就是大多数人所做的事情,并消除了使用PHP发送电子邮件的困难。在不是专家的情况下尝试在PHP语言中使用mail()
函数是一种杀手锏!
发布于 2012-10-15 10:07:38
你试过的附件有多大?也许您应该增加上传文件的大小限制,请参阅http://drupal.org/node/97193
https://stackoverflow.com/questions/12893218
复制