问题
Essentially what I'm trying to do is attach a file to an email I'm sending out. Simple enough, right? For some reason or another it does not like the following code (presumably because of the headers). Can anyone help?
Thanks in advance!!
$subject = "File ".date("Ymd");
$message = "NONE";
$filename = "test.csv";
$content = chunk_split(base64_encode(file_get_contents($filename)));
$uid = md5(uniqid(time()));
$name = basename($file);
$header .= "MIME-Version: 1.0\r\n";
$header .= "From: noreply@x.com\r\n";
$header .= "Reply-To: noreply@x.com\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: text/csv; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."\r\n";
//echo $header;
if (mail($to_email, $subject, $message, $header)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR!";
}
And the error:
Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent.
回答1:
Please please please don't build your own MIME emails. Use PHPMailer or Swiftmailer, which do almost everything for you. You can replace you entire script with about 5 or 6 lines of code.
And best of all, they'll give you far better error messages/diagnostics than the pathetically stupid mail()
function ever will.
回答2:
If you insist on building your own header, I would suggest doing so with the aid of your output buffer - also I noticed that you were failing to close up your content boundaries. Pasted below is how I would edit the header generating part of your script.
ob_start();
?>
MIME-Version: 1.0
From: noreply@x.com
Reply-To: noreply@x.com
Content-Type: multipart/mixed; boundary="<?php echo $uid; ?>"
This is a multi-part message in MIME format.
--<?php echo $uid; ?>
Content-Type:text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 7bit
<?php echo $message; ?>
--<?php echo $uid; ?>--
--<?php echo $uid; ?>
Content-Type: text/csv; name="<?php echo $filename; ?>"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="<?php echo $filename; ?>"
<?php echo $content; ?>
--<?php echo $uid; ?>--
<?php
$header = trim(ob_get_clean());
回答3:
The Geekmail PHP library makes it easy to add attachments to emails (and to send emails in general):
$geekMail = new geekMail();
$geekMail->setMailType('text');
$geekMail->from("noreply@x.com");
$geekMail->to($to_email);
$geekMail->subject($subject);
$geekMail->message($message);
$geekMail->attach($filename);
if (!$geekMail->send()){
//an error occurred sending the email
$errors = $geekMail->getDebugger();
}
回答4:
You don't seem to populate destination address (in the code sample) and you have your message both in headers (that definitely extends further than headers) and in body…
来源:https://stackoverflow.com/questions/5626574/php-mail-headers