I am trying to use SwiftMailer to send out an email with an attachment (from a contact form), and everything works great except it is only sending plain text emails. I would like it to send it as an HTML email. As you can see I tried setting the content-type to text/html, but no luck. Tried a few other things from the docs with no luck as well. Any guidance would be greatly appreciated. Thanks.
//Create the sender from the details we've been given
$sender =& new Swift_Address($email, $name);
//Create the message to send
$message =& new Swift_Message("Message From: " . $name);
$message->setContentType("text/html");
$message->attach(new Swift_Message_Part($body));
# message sent time:
$clean['email_sent'] = date('Y:m:d h:i:s');
//If an attachment was uploaded, attach it
if ($upload_valid && isset($file_path) && isset($file_name) && isset($file_type))
{
$message->attach(new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
$clean['email_sent'] = date('Y:m:d h:i:s');
}
//Try sending the email
$sent = $swift->send($message, "info@example.com", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
//Create the sender from the details we've been given
$sender =& new Swift_Address($email, $name);
//Create the message to send
$message =& new Swift_Message("Message From: " . $name);
$message->attach(new Swift_Message_Part($body, "text/html"));
# message sent time:
$clean['email_sent'] = date('Y:m:d h:i:s');
//If an attachment was uploaded, attach it
if ($upload_valid && isset($file_path) && isset($file_name) && isset($file_type))
{
$message->attach(new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
$clean['email_sent'] = date('Y:m:d h:i:s');
}
//Try sending the email
$sent = $swift->send($message, "info@example.com", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
As soon as you attach a "part" that becomes a mutlipart message, even if there's only one part for the client to use. The other "part" is actually the attachment in this case. You need to set the content-type on the part, not the message.