I'm trying to send an email with a pdf attachment using Pear. The email sends, but it seems to be getting sent in the wrong format. It appears the email is displaying in text format. This is what I receive:
Content-Type: multipart/alternative;
boundary="=_5a78298c81e9b7e60dee1049b9239270"
--=_5a78298c81e9b7e60dee1049b9239270
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1
Hello world
--=_5a78298c81e9b7e60dee1049b9239270
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=ISO-8859-1
Hello world
--=_5a78298c81e9b7e60dee1049b9239270--
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream;
name=phpk0AQHD
Content-Disposition: attachment;
filename=phpk0AQHD
This is followed by a massive amount of random characters which I can only assume is the attached file in text format. I've tried sending it to Gmail, Hotmail and an Microsoft Exchange address that uses Outlook, and I get the same results.
This is my code:
<?php
include('../includes/Mail.php');
include('../includes/Mail/mime.php');
$newsletterName = $_POST['newsletterName'];
$newsletterDate = $_POST['newsletterDate'];
$_SESSION['newsletterName'] = $newsletterName;
$_SESSION['newsletterDate'] = $newsletterDate;
$uploadDir = "newsletters/";
$tempLocation = $_FILES['newsletter']['tmp_name'];
$newsletterPath = $uploadDir . str_replace(" ", "-", $_FILES['newsletter']['name']);
$newsletterFile = $_FILES['newsletter'];
if ($_FILES["newsletter"]["type"] == "application/pdf")
{
include '../includes/db-connection-wp.php';
$query = "INSERT INTO newsletters (newsletter_title, newsletter_path, newsletter_date) VALUES ('" . mysql_real_escape_string($newsletterName) . "', '" . mysql_real_escape_string($newsletterPath) . "', '$newsletterDate')";
if (!mysql_query($query,$connection))
{
die('Error: ' . mysql_error() . ' Please go back and try again.');
}
//copy pdf to the right location
if (copy($tempLocation, "../" . $newsletterPath))
{
include '../includes/db-connection.php';
$queryContact = "SELECT users.email_address FROM form_pages LEFT JOIN users ON form_pages.user_id = users.user_id WHERE form_pages.page_name = 'add-newsletter'";
$resultContact = mysql_query($queryContact);
while ($rowContact = mysql_fetch_assoc($resultContact))
{
$from = $rowContact["email_address"];
}
$query = "SELECT * FROM newsletter_recipients";
$result = mysql_query($query);
$subject = 'Newsletter';
$message = new Mail_mime();
$text = file_get_contents("mail_text.txt");
$html = file_get_contents("mail_html.html");
$message->setTXTBody("Hello world");
$message->setHTMLBody("<b>Hello world</b>");
$message->addAttachment($tempLocation);
$body = $message->get();
$extraheaders = array("From"=>"[email protected]", "Subject"=>"My Subject 7");
$headers = $message->headers($extraheaders);
$mail = Mail::factory("mail");
$mail->send("[email protected]", $headers, $body);
$_SESSION['pdf'] = true;
$_SESSION['newsletterName'] = null;
$_SESSION['newsletterDate'] = null;
header("location:success/");
}
else
{
$_SESSION['upload'] = false;
header("location:add-newsletter/");
}
}
else
{
$_SESSION['pdf'] = false;
echo "hello " . $_FILES["newsletter"]["type"];
header("location:add-newsletter/");
}
?>
Any tips on what I'm doing wrong would be really appreciated.