• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Electric Toolbox Blog

The Electric Toolbox Blog

Linux, Apache, Nginx, MySQL, Javascript and PHP articles

  • Applications
  • FCKEditor
  • Apache
  • Windows
  • Contact Us
Home / Change the email body character set with PEAR Mail_Mime

Change the email body character set with PEAR Mail_Mime

The PHP PEAR Mail Mime library makes it easy to create multi part emails with attachments and alternate plain text and HTML messages. I recently needed to change the character set from the default iso-8859-1 to utf-8 to render some special unicode characters and show in this post how to do this.

The PHP PEAR Mail Mime Library

The Mail Mime library can be found here:
http://pear.php.net/package/Mail_Mime

The above link has information about the library including links to download it and to the documentation. Some useful documentation created from the source using phpDocumentor is here:
http://pear.php.net/package/Mail_Mime/docs/latest/Mail_Mime/Mail_mime.html

Sending a Mail_mime email

The following code example shows the basics of sending a message using Mail_mime:

$mime = new Mail_mime();
$mime->setTxtBody($message);
$body = $mime->get();
$headers = $mime->headers($headers);
$mail = Mail::factory('mail');
$mail->send($recipient_email_address, $headers, $body);

Changing the default character set

The Mail_mime::get() function can be passed an associative array of options including the ‘text_charset’ which determines the character set to use. Therefore to make the character set UTF-8 instead of ISO-8859-1 change the $mime->get line from the above code snippet to look like this:

$body = $mime->get(array('text_charset' => 'utf-8'));

The full revised snippet would now look like this, where I have highlighted the change in red:

$mime = new Mail_mime();
$mime->setTxtBody($message);
$body = $mime->get(array('text_charset' => 'utf-8'));
$headers = $mime->headers($headers);
$mail = Mail::factory('mail');
$mail->send($recipient_email_address, $headers, $body);

Raw email message – default

This snippet shows the raw email message with the default character set, which I have highlighted in red:

To: chris@electrictoolbox.com
Subject: Blah blah blah
MIME-Version: 1.0
From: "Chris Hope" <chris@electrictoolbox.com>
Content-Type: multipart/mixed;
    boundary="=_fa873f3720cf75d9105e98ea7c65e248"
Date: Tue, 05 Jan 2010 09:27:23 +1300

--=_fa873f3720cf75d9105e98ea7c65e248
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset="ISO-8859-1"
Body content here --=_fa873f3720cf75d9105e98ea7c65e248 Content-Transfer-Encoding: base64 Content-Type: text/plain; name="order-727.csv"; Content-Disposition: attachment; filename="order-727.csv"; Attachment content here --=_fa873f3720cf75d9105e98ea7c65e248--

Raw email message – after changing character set to UTF-8

This second snippet now shows the same email message but where the ‘text_charset’ has been set as ‘utf-8’, again with the relevent portion highlighted in red:

To: chris@electrictoolbox.com
Subject: Blah blah blah
MIME-Version: 1.0
From: "Chris Hope" <chris@electrictoolbox.com>
Content-Type: multipart/mixed;
    boundary="=_fa873f3720cf75d9105e98ea7c65e248"
Message-Id: <E1NRtWN-000121-EP@debian.debian.local>
Date: Tue, 05 Jan 2010 09:58:08 +1300

--=_fa873f3720cf75d9105e98ea7c65e248
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset="utf-8"
Body content here --=_fa873f3720cf75d9105e98ea7c65e248 Content-Transfer-Encoding: base64 Content-Type: text/plain;  name="order-727.csv"; Content-Disposition: attachment;  filename="order-727.csv"; Attachment content here --=_fa873f3720cf75d9105e98ea7c65e248--

Check Out These Related posts:

  1. String concatenation with MySQL – Part 1
  2. Make an entire table row clickable with jQuery
  3. String concatenation with MySQL – Part 2
  4. Changing exim4 settings with Debian 5 Lenny

Filed Under: PHP

Primary Sidebar

Categories

  • Apache
  • Applications
  • Article
  • Case Studies
  • Email Servers
  • FCKEditor
  • HTML And CSS
  • Javascript
  • Linux/Unix/BSD
  • Microsoft SQL Server
  • Miscellaneous Postings
  • MySql
  • Networking
  • Nginx Web Server
  • Offsite Articles
  • OSX
  • PHP
  • Quick Tips
  • RFC – Request for Comments
  • SilverStripe
  • VMWare
  • VPN
  • Windows
  • WordPress

Recent Posts

  • Vim Show Line Numbers
  • Add User To Group Linux
  • Chmod 777 Tutorial
  • How to Copy Directory Linux
  • Linux create user

Copyright © 2021. ElectricToolBox. All Rights Reserved.

  • Contact Us
  • Copyright Info
  • Privacy Policy
  • Sitemap