Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Send UTF8 email with attachment

Here is the function to send email, that can contain UTF8 characters and file attachments. It takes 5 parameters: $to - where to send email, $from - from whom email was sent, $subject - email subject, $message -  message of email, $file - path to file.

<?php
//$to = "abc@abc.com";
//$from = "admin@web.com";
//$subject = "Welcome to website";
//$message = "Thank you for registering in website";
//$file = "./folder/filename.ext";
function send_utf8_mail($to, $from, $subject, $message, $file = "")
{
    if($file != "")
    {
        $fn = explode("/", $file);
        $filename = $fn[sizeof($fn)-1];
        $attachment = chunk_split(base64_encode(file_get_contents($file))); 
    }
    $uid = md5(uniqid(time()));
    $header = "From: ".$from."rn";
    $header .= "Reply-To: ".$from."rn";
    $header .= "MIME-Version: 1.0rn";
    $header .= "Content-Type: multipart/mixed; boundary="".$uid.""rnrn";
    $header .= "This is a multi-part message in MIME format.rn";
    $header .= "--".$uid."rn";
    $header .= "Content-type:text/plain; charset="UTF-8"rn";
    $header .= "Content-Transfer-Encoding: 7bitrnrn";
    $header .= $message."rnrn";
    if($file != "")
    {
        $header .= "--".$uid."rn";
        $header .= "Content-Type: application/octet-stream; name="".$filename.""rn";
        $header .= "Content-Transfer-Encoding: base64rn";
        $header .= "Content-Disposition: attachment; filename="".$filename.""rnrn";
        $header .= $attachment."rnrn";
    }
    $header .= "--".$uid."--";
    if(mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header))
    {
        return true;
    }
    else
    {
        return false;
    }
}
?>

You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus