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 (presu
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();
}
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…
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());
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.