By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

how to Sending Email in PHP with special characters using Swift Mailer

100+
P: 245
hey geeks,

I have a small mail script which will cause a PHP script to send a receipt upon clicking the submit button, by an HTML mail. This mail contains special characters, namely 'å', 'ä' and 'ö'. Whatever I try, I can't get these characters to display properly everywhere. In some email clients, these characters show up just fine, whereas in others they show up as question marks or kinda boxes. I've tried sending the mail as UTF-8, as ISO-8859- but I'm seriously stumped.

How do I send a mail from PHP using any characters while guaranteeing that the mail shows up the same everywhere?

I am using Wift Mailer library in order to send email.

Here is my swift mailer script settings

Expand|Select|Wrap|Line Numbers
  1. require_once 'lib/swift_required.php';
  2. $transport = Swift_MailTransport::newInstance();
  3.  
  4. //Create the Mailer using your created Transport
  5. $mailer = Swift_Mailer::newInstance($transport);
  6. $message = Swift_Message::newInstance()
  7.  
  8. ->setSubject($subject)
  9. ->setTo(array($cust_email => $name))
  10. ->setFrom(array('info@mohsinrafique.com' => 'Mohsin Rafique'))
  11. ->setSender('noreply@mohsinrafique.com')
  12. ->setReturnPath('bounce@mohsinrafique.com')
  13. ->setPriority(3)
  14. ->setCharset('utf-8')
  15. ->setBody($messagebody, 'text/html');
  16.  

Kindly help me out to sort out my problem.
Dec 10 '09 #1
Share this Question
Share on Google+
9 Replies


Dormilich
Expert Mod 5K+
P: 8,639
does the HTML mail itself contain any information about the used encoding?

testing myself (Swift 4.0.3 on Linux, Thunderbird 3.0 on Mac, XHTML/UTF-8), I had no problems.
Dec 10 '09 #2

100+
P: 245
Hmm yes and no too. I mean some static information like subject which is hard coded means

$subject="Bestätigungsmail";

and some dynamic information came from database but that information is an entity which is encoded by this function

Expand|Select|Wrap|Line Numbers
  1. function charset_decode_utf_8($string) {
  2. /* Only do the slow convert if there are 8-bit characters */
  3. /* avoid using 0xA0 (\240) in ereg ranges. RH73 does not like that */
  4. if (! ereg("[\200-\237]", $string) and ! ereg("[\241-\377]", $string))
  5. return $string;
  6. // decode three byte unicode characters
  7. $string = preg_replace("/([\340-\357])([\200-\277])([\200-\277])/e","'&#'.((ord('\\1')-224)*4096 + (ord('\\2')-128)*64 + (ord('\\3')-128)).';'",$string);
  8. // decode two byte unicode characters
  9. $string = preg_replace("/([\300-\337])([\200-\277])/e","'&#'.((ord('\\1')-192)*64+(ord('\\2')-128)).';'",$string);
  10. return $string;
  11. }
  12.  
so this is whole senerio about my html mail content. so how can i fixed that the mail shows up the same everywhere?
Dec 10 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
what I mean is, does your HTML contains a line as this?
Expand|Select|Wrap|Line Numbers
  1. <meta http-equiv="content-type" content="text/html;charset=utf-8">
Dec 10 '09 #4

100+
P: 245
Yes but even then it is not working:(
Dec 10 '09 #5

Dormilich
Expert Mod 5K+
P: 8,639
as some mail clients display the data correctly, it seems that the others don’t support HTML email (good enough) or the encoding. what about a multipart message (html + text)?
Dec 10 '09 #6

100+
P: 245
Yes i am doing this too. here it go.....!
Expand|Select|Wrap|Line Numbers
  1. ->setBody($messagebody, 'text/html');
Dec 10 '09 #7

Dormilich
Expert Mod 5K+
P: 8,639
that’s not multipart, that’s the mime type of the message body.
Dec 10 '09 #8

100+
P: 245
Then:(
Will you ellaborate it more
Dec 10 '09 #9

Dormilich
Expert Mod 5K+
P: 8,639
ref. SwiftMailer Documentation: Setting the Body – and adding alternative body content (example in the wiki)
Dec 10 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.