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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • neovantage
    New Member
    • Aug 2008
    • 245

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

    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

    Code:
    require_once 'lib/swift_required.php';
    $transport = Swift_MailTransport::newInstance();
    		
    //Create the Mailer using your created Transport
    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance()
    		
    ->setSubject($subject)
    ->setTo(array($cust_email => $name))
    ->setFrom(array('info@mohsinrafique.com' => 'Mohsin Rafique'))
    ->setSender('noreply@mohsinrafique.com')
    ->setReturnPath('bounce@mohsinrafique.com')
    ->setPriority(3)
    ->setCharset('utf-8')
    ->setBody($messagebody, 'text/html');

    Kindly help me out to sort out my problem.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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.
    Last edited by Dormilich; Dec 10 '09, 01:20 PM. Reason: adding test result

    Comment

    • neovantage
      New Member
      • Aug 2008
      • 245

      #3
      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

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

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        what I mean is, does your HTML contains a line as this?
        Code:
        <meta http-equiv="content-type" content="text/html;charset=utf-8">

        Comment

        • neovantage
          New Member
          • Aug 2008
          • 245

          #5
          Yes but even then it is not working:(

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            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)?
            Last edited by Dormilich; Dec 10 '09, 02:24 PM. Reason: rephrasing

            Comment

            • neovantage
              New Member
              • Aug 2008
              • 245

              #7
              Yes i am doing this too. here it go.....!
              Code:
              ->setBody($messagebody, 'text/html');

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                that’s not multipart, that’s the mime type of the message body.

                Comment

                • neovantage
                  New Member
                  • Aug 2008
                  • 245

                  #9
                  Then:(
                  Will you ellaborate it more

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    ref. SwiftMailer Documentation: Setting the Body – and adding alternative body content (example in the wiki)

                    Comment

                    Working...