Can we set email format priority?

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
FrankHe
Forum Newbie
Posts: 5
Joined: Thu Jan 22, 2009 9:36 am

Can we set email format priority?

Post by FrankHe »

If I set the multi format messages like this one:

$message = new Swift_Message('Mail\'s subject', "<p>this is paragrapah</p>", 'text/html');
$message->attach( new Swift_Message_Part( "This is a pure text msg" , 'text/plain'));

I just found pure text format is always in my gmail or outlook client.

are there any way to set priority of email to HTML, and text email is only used when html is turned off.

Thanks
machinehead933
Forum Newbie
Posts: 7
Joined: Tue Jan 20, 2009 10:48 pm

Re: Can we set email format priority?

Post by machinehead933 »

You cannot set the priority per se. If you include both an HTML and plain text version of the email to your recipient, it will be up to the mail client settings to choose how the mail will be displayed. There is no way to let one format take precedence over the other.
FrankHe
Forum Newbie
Posts: 5
Joined: Thu Jan 22, 2009 9:36 am

Re: Can we set email format priority?

Post by FrankHe »

if I only send html format, will that email be received if client set the plain text?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Can we set email format priority?

Post by Chris Corbyn »

You're actually doing this wrong. Plain text will always be in your inbox and there will be no way to change it. It will work correctly if you do this:

Code: Select all

$message = new Swift_Message('Mail\'s subject');
$message->attach(new Swift_Message_Part("<p>this is paragrapah</p>", 'text/html'));
$message->attach( new Swift_Message_Part( "This is a pure text msg" , 'text/plain'));
Basically both alternatives must be attached as a "part". The standard body is ignored once you've added any parts. This behavior has been changed in version 4 (beta available at the top of this forum).
FrankHe
Forum Newbie
Posts: 5
Joined: Thu Jan 22, 2009 9:36 am

Re: Can we set email format priority?

Post by FrankHe »

Thank you for your reply.

I am using symfony 1.2, and want to use swiftMailer in it, the error is if I use this one:

$message = new Swift_Message('Account Activation', $this->getPartial('sendActivationBody', array("user"=>$users,"html"=>true)), 'text/html');
$message->attach( new Swift_Message_Part($this->getPartial('sendActivationBody', array("user"=>$users,"html"=>false))), 'text/plain');

I will only have text email, but if I use this one only
$message = new Swift_Message('Account Activation', $this->getPartial('sendActivationBody', array("user"=>$users,"html"=>true)), 'text/html');
I will have html email.

Based on your introduciton, if I use:

$message = new Swift_Message("Account Activation");
$message->attach( new Swift_Message_Part($this->getPartial('sendActivationBody', array("user"=>$users,"html"=>true))), 'text/html');
$message->attach( new Swift_Message_Part($this->getPartial('sendActivationBody', array("user"=>$users,"html"=>false))), 'text/plain');

Then the email I sent is this kind:

<p>Dear frank,</p>
<p>Thank you for registration on line to use our <b>free love matching service</b>.</p>
<p>Please click the following link to activate your account:</p>

You see, all the html tags are preserved!

I know your guys aer not symfony guy, but if you can point out reason, that will be great.

Thanks.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Can we set email format priority?

Post by Chris Corbyn »

You seem to have your closing parentheses in the wrong place ;)

The "text/html" argument must be passed to Swift_Message_Part ;) Same goes for the "text/plain" argument.

Code: Select all

 
$message = new Swift_Message("Account Activation");
$message->attach( new Swift_Message_Part($this->getPartial('sendActivationBody', array("user"=>$users,"html"=>true)), 'text/html'));
$message->attach( new Swift_Message_Part($this->getPartial('sendActivationBody', array("user"=>$users,"html"=>false)), 'text/plain'));
FrankHe
Forum Newbie
Posts: 5
Joined: Thu Jan 22, 2009 9:36 am

Re: Can we set email format priority?

Post by FrankHe »

Definitely correct!
Thanks for your pointing out, this has wasted me two days!
What a stupid error.
Thanks again for your help. :D
Post Reply