Keep in touch

you can keep in touch with my all blogs and sites by install this Toolbar...
http://rworldtoolbar.ourtoolbar.com/

Showing posts with label PHP mail function. Show all posts
Showing posts with label PHP mail function. Show all posts

Friday, June 27, 2008

UTF8 encoded php mail function

create a php mail function that can handle UTF8 characters across most email readers.

The problem with the php mail is that it does not encode the names and subjects and they could get lost in the transport or be misinterpreted from the email readers. This function actually does the proper encoding and overcomes the php mail deficiency.
—————————–
function UTF8_mail($from,$to,$subject,$message,$cc=”",$bcc=”"){
$from = explode(”<”,$from );
$headers =“From: =?UTF-8?B?”.base64_encode($from[0]).”?= <”. $from[1] . “\r\n”;
$to = explode(”<”,$to );$to = “=?UTF-8?B?”.base64_encode($to[0]).”?= <”. $to[1] ;
$subject=”=?UTF-8?B?”.base64_encode($subject).”?=\n”;
if($cc!=”"){$cc = explode(”<”,$cc );$headers .= “Cc: =?UTF-8?B?”.base64_encode($cc[0]).”?= <”. $cc[1] . “\r\n”;}
if($bcc!=”"){$bcc = explode(”<”,$bcc );$headers .= “Bcc: =?UTF-8?B?”.base64_encode($bcc[0]).”?= <”. $bcc[1] . “\r\n”;}
$headers .=“Content-Type: text/plain; ”. “charset=UTF-8; format=flowed\n”. “MIME-Version: 1.0\n”. “Content-Transfer-Encoding: 8bit\n”. “X-Mailer: PHP\n”;
return mail($to, $subject, $message, $headers);
}
UTF8_mail(“Γιω�?γος Κοντοπουλος ”,“First Last ”,“Θέμα Subject”,“Κείμενο Text”,“”,“Κ�?υφός Φίλοςhidden_friend@email.com”);
—————————–
All this function is accomplishing is to encode each
Name
to
=?UTF-8?B?zpzOuc+HzrHOu863z4I=?= email@domain.com

The emails themselves don’t need to be encoded since an email conventionally can only consist of of latin characters but, we could also confuse the mail server if we did encode them.

Sending E-Mail(s) in PHP

PHP mail() function
E-mails in PHP can be easily sent using the library function 'mail'. This function takes four arguments to send E-mails from a PHP web page and returns 'true' upon successful delivery of Email. The parameters of this function are as follows:
-Recipient E-mail address
-E-mail Subject
-E-mail message (body)
-Headers and additional parameters like the Sender E-mail address
Syntax:
mail( string to, string subject, string message [, string additional_headers [, string additional_parameters]] )

This function returns the boolean value 'True' if the mail is sent successfully, otherwise it returns 'False'.

Example:
To : textbox
From : textbox
Subject : textbox
Message(body) : textbox or textarea
Send : button
Above forms shows how to send email through PHP. And code for the above examples given below, use this code and try it.

Sample PHP Code
//Check whether the submission is made
if(isset($hidSubmit)){
//Declarate the necessary variables
$mail_to=$txtEmailto;
$mail_from=$txtEmailfrm;
$mail_sub=$txtSub;
$mail_mesg=$txtMsg;
//Check for success/failure of delivery
if(mail($mail_to,$mail_sub,$mail_mesg,"From:$mail_from/r/nReply-to:$mail_from"))
echo "E-mail has been sent successfully from $mail_sub to $mail_to";
else
echo "Failed to send the E-mail from $mail_sub to $mail_to";
}
?>