Tag: PHP
Mailing function for PHP with UTF-8, HTML and attachments
by Klas Lundberg on Jun.13, 2010, under Check this out
I found a function a couple a years ago which I’ve been using for mailing in my projects. The function was rather basic from the start and then I’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 <html> 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.
To get UTF-8 to work correctly in your PHP pages, see my post about Setting up PHP for UTF8 and XHTML compatibility.
The nice mailman image is from The Invisible Agent.
//////////////////////////////////////////// // Function: send_mail() // UTF-8 Mail sending with html and attachments //////////////////////////////////////////// /** Sends a mail. * * @code { # To Email Address $to_address = "to@address.com"; # From Email Address $from_address = "from@address.com"; # From Name $from_name = "Send Mail"; # Message Subject $email_subject = "This is a test mail with some attachments"; # Use relative paths to the attachments $attachments = Array( Array("file"=>"../../test.doc", "content_type"=>"application/msword"), Array("file"=>"../../123.pdf", "content_type"=>"application/pdf") ); # Message Body $email_body = "<html><head></head><body>This is a message with <b>".count($attachments)."</b> attachments and maybe some <i>HTML</i>!</body></html>"; send_mail($to_address, $from_address, $from_name, $email_subject, $email_body, $attachments); } */ function send_mail($to_address, $from_address, $from_name, $email_subject, $email_body, $attachments=false) { $eol="\r\n"; $mime_boundary = md5(time()); $headers = ''; $msg = ''; $html = (mb_strpos($email_body, "<html>") !== false); $mail_site = "send_mail@" .$_SERVER['SERVER_NAME']; # Common Headers $headers .= 'Sender: ' . $mail_site . $eol; $headers .= 'From: ' . mb_encode_mimeheader($from_name) . " <" . $mail_site . ">" . $eol; $headers .= 'Reply-To: ' . mb_encode_mimeheader($from_name) . " <$from_address>" . $eol; $headers .= 'Return-Path: ' . mb_encode_mimeheader($from_name) . " <$from_address>" . $eol; // these two to set reply address $headers .= "Message-ID: <".time().$mime_boundary."@".$_SERVER['SERVER_NAME'].">".$eol; $headers .= "X-Mailer: PHP v" . phpversion() . $eol; // These two to help avoid spam-filters # Boundry for marking the split & Multitype Headers $headers .= 'MIME-Version: 1.0' . $eol; if ($attachments !== false) { $headers .= "Content-Type: multipart/mixed; boundary=\"1$mime_boundary\"" . $eol; $msg .= "--1".$mime_boundary.$eol; if($html) { $msg .= "Content-Type: multipart/alternative; boundary=\"2$mime_boundary\"" . $eol.$eol; } } else { if($html) { $headers .= "Content-Type: multipart/alternative; boundary=\"2$mime_boundary\"" . $eol; } } # Setup for text OR html # Text Version if($html) { $msg .= "--2".$mime_boundary.$eol; $msg .= "Content-Type: text/plain; charset=UTF-8".$eol; $msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol; } else { $headers .= "Content-Type: text/plain; charset=UTF-8".$eol; $headers .= "Content-Transfer-Encoding: 8bit"; } $msg .= strip_tags(str_replace("<br>", $eol, str_replace("<br />", $eol, $email_body))).$eol.$eol; if($html) { # HTML Version $msg .= "--2".$mime_boundary.$eol; $msg .= "Content-Type: text/html; charset=UTF-8".$eol; $msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol; $msg .= $email_body.$eol.$eol; # Finished $msg .= "--2".$mime_boundary."--".$eol.$eol; // finish with two eol's for better security. see Injection. } if ($attachments !== false) { for($i=0; $i < count($attachments); $i++) { if (is_file($attachments[$i]["file"])) { # File for Attachment $file_name = substr($attachments[$i]["file"], (strrpos($attachments[$i]["file"], "/")+1)); $handle=fopen($attachments[$i]["file"], 'rb'); $f_contents=fread($handle, filesize($attachments[$i]["file"])); $f_contents=chunk_split(base64_encode($f_contents)); //Encode The Data For Transition using base64_encode(); fclose($handle); # Attachment $msg .= "--1".$mime_boundary.$eol; $msg .= "Content-Type: ".$attachments[$i]["content_type"]."; name=\"".$file_name."\"".$eol; $msg .= "Content-Transfer-Encoding: base64".$eol; $msg .= "Content-Disposition: attachment; filename=\"".$file_name."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !! $msg .= $f_contents.$eol.$eol; } } $msg .= "--1".$mime_boundary."--".$eol.$eol; // finish with two eol's for better security. see Injection. } # SEND THE EMAIL ini_set('sendmail_from',$from_address); // the INI lines are to force the From Address to be used ! mail($to_address, mb_encode_mimeheader($email_subject), $msg, $headers); ini_restore('sendmail_from'); }
Setting up PHP for UTF8 and XHTML compatibility
by Klas Lundberg on Jun.13, 2010, under Check this out
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 save dialog. Then you have to set the php.ini settings to UTF8. This can be done with the ini_set() function. The ini_set() have to be called before outputting any text or HTML/XHTML in the document.
// UTF8 settings ini_set('mbstring.language', 'Neutral'); ini_set('mbstring.internal_encoding', 'UTF-8'); ini_set('mbstring.http_input', 'UTF-8'); ini_set('mbstring.http_output', 'UTF-8'); ini_set('mbstring.encoding_translation', 'On'); ini_set('mbstring.detect_order', 'auto'); ini_set('mbstring.substitute_character', 'long');
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 header() function also have to be called before outputting any text or HTML/XHTML in the document.
// Set XHTML content type and character encoding of the document // You may also use text/html as mime type instead of application/xhtml+xml header('Content-Type: application/xhtml+xml; charset=utf-8');
Sometimes you may have problems with getting data from MySQL encoded correctly as UTF8. Most of the time this isn’t a problem, but you can also force some UTF8 settings on the MySQL server. Use the mysql_set_charset if you have PHP version 5.2 or higher and SET NAMES/SET CHARACTER SET if you have an older version of PHP.
$conn = mysql_connect('localhost', 'user', 'password'); // PHP 5.2 and above mysql_set_charset('utf8',$conn); // PHP below v 5.2 mysql_query("SET NAMES utf8"); mysql_query("SET CHARACTER SET utf8");
At last, do not forget to add the Content-Type <meta> tag in the <head> section of your HTML/XHTML. Otherwise the web browsers will display your page incorrectly anyway.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />XHTML
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.
// XHTML compatibility ini_set('arg_separator.output', '&'); ini_set('url_rewriter.tags', 'a=href,area=href,frame=src,input=src,form=action,fieldset=');
For security reasons, I recommend you to disable the url rewriting, since it makes it possible to hijack another user’s session under certain circumstances. This makes the previous XHTML settings unnecessary, but keep them if you intend to use sessions by URL.
// Disable session by URL ini_set('session.use_only_cookies', '1');
Still having problems?
To see your PHP version and how the settings are affected, simply call php_info() after changing the settings. Master value is the original server setting and Local value is the value after changing it using ini_set().
php_info();
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 ISO-8859-1 and change them to UTF-8 using the ini_set() function. If you still have problems, check that your document files really are saved using correct UTF-8 character encoding.
PHP 5.3.1 update problems
by Klas Lundberg on Jan.26, 2010, under CMS
One.com just upgraded their systems to PHP version 5.3.1 from 5.2.1. I think it’s great that they keep their systems updated, but it would be really nice if the could inform about these updates somewhere. On the other hand, their chat support was proved to be excellent once again, giving me the answers I needed in a couple of minutes or so.
The problem with the update was however with the new introduction of a very old syntax, used in Basic (and Visual Basic) back in the days when MC Hammer ruled the charts and AJAX was a household cleaner liquid or possibly a soccer team. The syntax is the goto command, used like this:
I would not recommend to use this syntax at all, since it mostly only makes things complicated!
1 2 3 4 5 6 7 | <?php goto a; echo 'Foo'; a: echo 'Bar'; ?> |
The above example will output: Bar
However, this little command caused a lot of trouble on Linn’s site, where all the scripts included a function called goto(). The timing for all this misshappening was really bad, since Linn have a big fashion show coming up and I was in London at the time the site went down. The function is used for redirecting the user to other URL:s with some nice informative graphics in between. With the new command introduced, the function now caused a fatal error on every page, meaning the whole site was down. The problem was easily solved, but I really cannot understand why they would want to introduce that command?
PHP.net has put together a guide with more info about migrating from PHP 5.2.x to PHP 5.3.x.
Linnn clothing
by Klas Lundberg on Jan.14, 2010, under CMS, Check this out
I just put up a website for my friend Linn Nydren and her label Linnn Clothing. She is into designing clothes and have a gorgeous new collection for spring 2010. She is originally from sweden but lives in London and have her business there too. You should check it out if yo’re into fashion, design and clothes. You can also follow her work on Linnn Clothing on facebook.
The site is made in flash with my own old CMS shout in the backend, still going strong. I also connected Google Analytics to it, which I by the way love! =)
Putting up my old CMS
by Klas Lundberg on Feb.04, 2009, under CMS
This will be the beginning of a hopefully long journey and I’m gonna start it by putting up my old Content Management System. It’s a rather flexible system built on PHP and MySQL. I used to make a little money on it until I got my web developer job at Basilicon. Now I’m gonna put it up on the web for free just for fun, so enjoy!
The first thing I will have to do is rename it. It was called Shout, but I discovered that there was another CMS called Shout, so I have to think of another name. Any suggestions? I think I have to do some fine tuning to get it to work properly on my hosting at one.com, but then it should probably work anywhere.
It is actually powering these two sites, check them out.

