<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Last Instinct &#187; UTF-8</title>
	<atom:link href="http://last.instinct.se/tag/utf-8/feed" rel="self" type="application/rss+xml" />
	<link>http://last.instinct.se</link>
	<description>Web, design and coolness</description>
	<lastBuildDate>Tue, 22 Mar 2011 19:29:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Mailing function for PHP with UTF-8, HTML and attachments</title>
		<link>http://last.instinct.se/check-this-out/mailing-function-for-php-with-utf-8-html-and-attachments/751</link>
		<comments>http://last.instinct.se/check-this-out/mailing-function-for-php-with-utf-8-html-and-attachments/751#comments</comments>
		<pubDate>Sun, 13 Jun 2010 20:51:07 +0000</pubDate>
		<dc:creator>Klas Lundberg</dc:creator>
				<category><![CDATA[Check this out]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[UTF-8]]></category>

		<guid isPermaLink="false">http://last.instinct.se/?p=751</guid>
		<description><![CDATA[I found a function a couple a years ago which I&#8217;ve been using for mailing in my projects. The function was rather basic from the start and then I&#8217;ve added functionality as needed. Nowadays it is changed into only having UTF8 support (since I almost always use UTF8 encoding) and also support for HTML. the [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://theinvisibleagent.wordpress.com/2009/05/24/mailmen-and-women-of-the-past/" target="_blank" title="Mailman"><img src="http://last.instinct.se/wp-content/uploads/mailmanbest002.jpg" alt="" title="Mailman" width="338" height="348" class="alignleft size-full wp-image-756" /></a>I found a function a couple a years ago which I&#8217;ve been using for mailing in my projects. The function was rather basic from the start and then I&#8217;ve added functionality as needed. Nowadays it is changed into only having UTF8 support (since I almost always use UTF8 encoding) and also support for HTML. the HTML encoding is detected automatically if the <code>&lt;html&gt;</code> tag is present in the text. You can attach images and files by adding them into an array. Feel free to use it however you like.</p>
<p>To get UTF-8 to work correctly in your PHP pages, see my post about <a href="http://last.instinct.se/check-this-out/setting-up-php-for-utf8-and-xhtml-compatibility/737">Setting up PHP for UTF8 and XHTML compatibility</a>.</p>
<p>The nice mailman image is from <a href="http://theinvisibleagent.wordpress.com/2009/05/24/mailmen-and-women-of-the-past/" target="_blank">The Invisible Agent</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">////////////////////////////////////////////</span>
<span style="color: #666666; font-style: italic;">// Function: send_mail()</span>
<span style="color: #666666; font-style: italic;">// UTF-8 Mail sending with html and attachments</span>
<span style="color: #666666; font-style: italic;">////////////////////////////////////////////</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/** Sends a mail.
 * 
 * @code {
	# To Email Address
	$to_address = &quot;to@address.com&quot;;
&nbsp;
	# From Email Address
	$from_address = &quot;from@address.com&quot;;
&nbsp;
	# From Name
	$from_name = &quot;Send Mail&quot;;
&nbsp;
	# Message Subject
	$email_subject = &quot;This is a test mail with some attachments&quot;;
&nbsp;
	# Use relative paths to the attachments
	$attachments = Array(
	  Array(&quot;file&quot;=&gt;&quot;../../test.doc&quot;, &quot;content_type&quot;=&gt;&quot;application/msword&quot;), 
	  Array(&quot;file&quot;=&gt;&quot;../../123.pdf&quot;, &quot;content_type&quot;=&gt;&quot;application/pdf&quot;)
	);
&nbsp;
	# Message Body
	$email_body = &quot;&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;This is a message with &lt;b&gt;&quot;.count($attachments).&quot;&lt;/b&gt; attachments and maybe some &lt;i&gt;HTML&lt;/i&gt;!&lt;/body&gt;&lt;/html&gt;&quot;;
&nbsp;
	send_mail($to_address, $from_address, $from_name, $email_subject, $email_body, $attachments);
 } 
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> send_mail<span style="color: #009900;">&#40;</span><span style="color: #000088;">$to_address</span><span style="color: #339933;">,</span> <span style="color: #000088;">$from_address</span><span style="color: #339933;">,</span> <span style="color: #000088;">$from_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email_subject</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email_body</span><span style="color: #339933;">,</span> <span style="color: #000088;">$attachments</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$eol</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$mime_boundary</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$headers</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$msg</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mb_strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email_body</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;html&gt;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$mail_site</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;send_mail@&quot;</span> <span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SERVER_NAME'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Common Headers
</span>  <span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'Sender: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$mail_site</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'From: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mb_encode_mimeheader</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$from_name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; &lt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$mail_site</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'Reply-To: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mb_encode_mimeheader</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$from_name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; &lt;<span style="color: #006699; font-weight: bold;">$from_address</span>&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'Return-Path: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mb_encode_mimeheader</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$from_name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; &lt;<span style="color: #006699; font-weight: bold;">$from_address</span>&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">// these two to set reply address</span>
  <span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Message-ID: &lt;&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #000088;">$mime_boundary</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;@&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SERVER_NAME'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;X-Mailer: PHP v&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">phpversion</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>          <span style="color: #666666; font-style: italic;">// These two to help avoid spam-filters</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Boundry for marking the split &amp; Multitype Headers
</span>  <span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'MIME-Version: 1.0'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$attachments</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Content-Type: multipart/mixed; boundary=<span style="color: #000099; font-weight: bold;">\&quot;</span>1<span style="color: #006699; font-weight: bold;">$mime_boundary</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;--1&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$mime_boundary</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Content-Type: multipart/alternative; boundary=<span style="color: #000099; font-weight: bold;">\&quot;</span>2<span style="color: #006699; font-weight: bold;">$mime_boundary</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$eol</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Content-Type: multipart/alternative; boundary=<span style="color: #000099; font-weight: bold;">\&quot;</span>2<span style="color: #006699; font-weight: bold;">$mime_boundary</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Setup for text OR html
</span>  
  <span style="color: #666666; font-style: italic;"># Text Version
</span>  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;--2&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$mime_boundary</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Content-Type: text/plain; charset=UTF-8&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Content-Transfer-Encoding: 8bit&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Content-Type: text/plain; charset=UTF-8&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Content-Transfer-Encoding: 8bit&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$eol</span><span style="color: #339933;">,</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$eol</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email_body</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	  <span style="color: #666666; font-style: italic;"># HTML Version
</span>	  <span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;--2&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$mime_boundary</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
	  <span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Content-Type: text/html; charset=UTF-8&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
	  <span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Content-Transfer-Encoding: 8bit&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
	  <span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$email_body</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
&nbsp;
	  <span style="color: #666666; font-style: italic;"># Finished
</span>	  <span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;--2&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$mime_boundary</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;--&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// finish with two eol's for better security. see Injection.</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$attachments</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attachments</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attachments</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
	   <span style="color: #666666; font-style: italic;"># File for Attachment
</span>	   <span style="color: #000088;">$file_name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attachments</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attachments</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	   <span style="color: #000088;">$handle</span><span style="color: #339933;">=</span><span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attachments</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	   <span style="color: #000088;">$f_contents</span><span style="color: #339933;">=</span><span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attachments</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	   <span style="color: #000088;">$f_contents</span><span style="color: #339933;">=</span><span style="color: #990000;">chunk_split</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">base64_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$f_contents</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//Encode The Data For Transition using base64_encode();</span>
	   <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	   <span style="color: #666666; font-style: italic;"># Attachment
</span>	   <span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;--1&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$mime_boundary</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
	   <span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Content-Type: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$attachments</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;content_type&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;; name=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$file_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
	   <span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Content-Transfer-Encoding: base64&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
	   <span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Content-Disposition: attachment; filename=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$file_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// !! This line needs TWO end of lines !! IMPORTANT !!</span>
	   <span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$f_contents</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
&nbsp;
	 <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000088;">$msg</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;--1&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$mime_boundary</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;--&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">.</span><span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// finish with two eol's for better security. see Injection.</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># SEND THE EMAIL
</span>  <span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sendmail_from'</span><span style="color: #339933;">,</span><span style="color: #000088;">$from_address</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// the INI lines are to force the From Address to be used !</span>
  <span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$to_address</span><span style="color: #339933;">,</span> <span style="color: #990000;">mb_encode_mimeheader</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email_subject</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$msg</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">ini_restore</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sendmail_from'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flast.instinct.se%252Fcheck-this-out%252Fmailing-function-for-php-with-utf-8-html-and-attachments%252F751%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fis.gd%2FcO1fq%22%2C%20%22style%22%3A%20%22small%22%2C%20%22title%22%3A%20%22Mailing%20function%20for%20PHP%20with%20UTF-8%2C%20HTML%20and%20attachments%22%20%7D);"><script type="text/javascript">topsyWidgetPreload({ "url": "http%3A%2F%2Flast.instinct.se%2Fcheck-this-out%2Fmailing-function-for-php-with-utf-8-html-and-attachments%2F751", "shorturl": "http://is.gd/cO1fq", "style": "small", "title": "Mailing function for PHP with UTF-8, HTML and attachments" });</script></div>

<div align="right" style="float: right; padding: 0px 0px 1px 5px;"><a name="fb_share" type="button_count" share_url="http://last.instinct.se/check-this-out/mailing-function-for-php-with-utf-8-html-and-attachments/751"></a></div>]]></content:encoded>
			<wfw:commentRss>http://last.instinct.se/check-this-out/mailing-function-for-php-with-utf-8-html-and-attachments/751/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting up PHP for UTF8 and XHTML compatibility</title>
		<link>http://last.instinct.se/check-this-out/setting-up-php-for-utf8-and-xhtml-compatibility/737</link>
		<comments>http://last.instinct.se/check-this-out/setting-up-php-for-utf8-and-xhtml-compatibility/737#comments</comments>
		<pubDate>Sun, 13 Jun 2010 20:00:21 +0000</pubDate>
		<dc:creator>Klas Lundberg</dc:creator>
				<category><![CDATA[Check this out]]></category>
		<category><![CDATA[Multilanguage]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[UTF-8]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://last.instinct.se/?p=737</guid>
		<description><![CDATA[PHP is usually not configured for working well with UTF8 multilingual support and XHTML from start. To get it working correctly, you need to adjust a few settings. UTF-8 There are two things to think about. First, make sure your php files are saved using UTF8 encoding. Even notepad can achieve this in the file [...]]]></description>
			<content:encoded><![CDATA[
<p><img src="http://last.instinct.se/wp-content/uploads/languages.png" alt="" title="Language" width="200" height="200" class="alignleft size-full wp-image-747" />PHP is usually not configured for working well with UTF8 multilingual support and XHTML from start. To get it working correctly, you need to adjust a few settings. </p>
<h3>UTF-8</h3>
<p>There are two things to think about. <b>First, make sure your php files are saved using UTF8 encoding</b>. Even notepad can achieve this in the file save dialog. Then you have to set the php.ini settings to UTF8. This can be done with the <a href="http://php.net/manual/en/function.ini-set.php" target="_blank">ini_set()</a> function. The <em>ini_set()</em> have to be called before outputting any text or HTML/XHTML in the document.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// UTF8 settings</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mbstring.language'</span><span style="color: #339933;">,</span> 			<span style="color: #0000ff;">'Neutral'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mbstring.internal_encoding'</span><span style="color: #339933;">,</span> 		<span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mbstring.http_input'</span><span style="color: #339933;">,</span> 			<span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mbstring.http_output'</span><span style="color: #339933;">,</span> 		<span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mbstring.encoding_translation'</span><span style="color: #339933;">,</span>	<span style="color: #0000ff;">'On'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mbstring.detect_order'</span><span style="color: #339933;">,</span> 		<span style="color: #0000ff;">'auto'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mbstring.substitute_character'</span><span style="color: #339933;">,</span> 	<span style="color: #0000ff;">'long'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Normally, the server sets the content type correctly if the file is coded in UTF8, but sometimes you need to force the content-type to have UTF8 charset. The <a href="http://php.net/manual/en/function.header.php" target="_blank">header()</a> function also have to be called before outputting any text or HTML/XHTML in the document.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Set XHTML content type and character encoding of the document</span>
<span style="color: #666666; font-style: italic;">// You may also use text/html as mime type instead of application/xhtml+xml</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: application/xhtml+xml; charset=utf-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Sometimes you may have problems with getting data from MySQL encoded correctly as UTF8. Most of the time this isn&#8217;t a problem, but you can also force some UTF8 settings on the MySQL server. Use the <a href="http://php.net/manual/en/function.mysql-set-charset.php" target="_blank">mysql_set_charset</a> if you have PHP version 5.2 or higher and <em>SET NAMES</em>/<em>SET CHARACTER SET</em> if you have an older version of PHP.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$conn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'user'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// PHP 5.2 and above</span>
<span style="color: #990000;">mysql_set_charset</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'utf8'</span><span style="color: #339933;">,</span><span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #666666; font-style: italic;">// PHP below v 5.2</span>
<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SET NAMES utf8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SET CHARACTER SET utf8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>At last, do not forget to add the Content-Type &lt;meta&gt; tag in the &lt;head&gt; section of your HTML/XHTML. Otherwise the web browsers will display your page incorrectly anyway.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=utf-8&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<h3>XHTML</h3>
<p>To get correct XHTML compatibility you need to adjust the session settings of PHP so that sessions is integrated the right way into your code. The server is often configured to insert session id:s into url:s on your PHP page.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// XHTML compatibility</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'arg_separator.output'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&amp;amp;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'url_rewriter.tags'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'a=href,area=href,frame=src,input=src,form=action,fieldset='</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>For security reasons, I recommend you to disable the url rewriting, since it makes it possible to hijack another user&#8217;s session under certain circumstances. This makes the previous XHTML settings unnecessary, but keep them if you intend to use sessions by URL.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Disable session by URL</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'session.use_only_cookies'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Still having problems?</h3>
<p>To see your PHP version and how the settings are affected, simply call <code>php_info()</code> after changing the settings. <em>Master value</em> is the original server setting and <em>Local value</em> is the value after changing it using <code>ini_set()</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">php_info<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>These UTF8 settings works most of the time, but many servers may have other configuration issues. If you still have issues with UTF8 check the php_info() output for any settings set to <code>ISO-8859-1</code> and change them to <code>UTF-8</code> using the <code>ini_set()</code> function. If you still have problems, check that your document files really are saved using correct UTF-8 character encoding.</p>
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flast.instinct.se%252Fcheck-this-out%252Fsetting-up-php-for-utf8-and-xhtml-compatibility%252F737%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fis.gd%2FcNYBX%22%2C%20%22style%22%3A%20%22small%22%2C%20%22title%22%3A%20%22Setting%20up%20PHP%20for%20UTF8%20and%20XHTML%20compatibility%22%20%7D);"><script type="text/javascript">topsyWidgetPreload({ "url": "http%3A%2F%2Flast.instinct.se%2Fcheck-this-out%2Fsetting-up-php-for-utf8-and-xhtml-compatibility%2F737", "shorturl": "http://is.gd/cNYBX", "style": "small", "title": "Setting up PHP for UTF8 and XHTML compatibility" });</script></div>

<div align="right" style="float: right; padding: 0px 0px 1px 5px;"><a name="fb_share" type="button_count" share_url="http://last.instinct.se/check-this-out/setting-up-php-for-utf8-and-xhtml-compatibility/737"></a></div>]]></content:encoded>
			<wfw:commentRss>http://last.instinct.se/check-this-out/setting-up-php-for-utf8-and-xhtml-compatibility/737/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

