sending an html message with a plain text attachment

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

sending an html message with a plain text attachment

Post by s.dot »

I'm trying to send an HTML message with a plain text attachment for those email clients that are not HTML capable. My code looks like this:

Code: Select all

<?php

require_once 'lib/Swift.php';
require_once 'lib/Swift/Connection/SMTP.php';

$log = Swift_LogContainer::getLog();
$log->setLogLevel(4);

$smtp = new Swift_Connection_SMTP('smtp.1and1.com', 25);
$smtp->setUsername('noreply@example.com');
$smtp->setpassword('password');

$swift		= new Swift($smtp);
$message	= new Swift_Message('A simple subject', '<b>A</b> <strong><small>Simple</small> Body</strong>', 'text/html');
$attachment = new Swift_Message_Attachment('A Simple Body', 'email.txt', 'text/plain');
$message->attach($attachment);

if ($swift->send($message, 'me@anotherdomain.com', 'noreply@example.com'))
{
	echo 'sent email';
} else
{
	echo 'did not send email';
	$log = Swift_LogContainer::getLog();
	echo $log->dump(true);
}
The email sends successfully, however the original email is not in HTML format. I do get the plain text message, though.

I thought I might need to make it multi-part, but it's not a multi-part message (at least I don't think). It's just a message with an attachment.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Indeed you do want to send a multi-part message and let the email client sort out which part it wants read.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Even if I'm sending as an attachment?

Or is the preferred method to send it both HTML and text versions of the email, and let the client do it's thing, like you mentioned? Or, are these the same things?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

scottayy wrote:Even if I'm sending as an attachment?

Or is the preferred method to send it both HTML and text versions of the email, and let the client do it's thing, like you mentioned? Or, are these the same things?
You can't send an attachment like that ;) You need to "attach" your HTML part if you want that to appear as the body because by the very nature of attachements, the email will have more than one document inside it (i.e. the body and the attachment).

Why are you trying to do this using attachments rather than using multipart messages? Either way, this will work:

Code: Select all

$swift      = new Swift($smtp);
$message = new Swift_Message('A simple subject');

$body = new Swift_Message_Part('<b>A</b> <strong><small>Simple</small> Body</strong>', 'text/html');
$message->attach($body);

$attachment = new Swift_Message_Attachment('A Simple Body', 'email.txt', 'text/plain');
$message->attach($attachment);
The documentation regarding sending attachments does mention the fact you need to set your body differently ;)
Post Reply