Posted By

stz184 on 10/08/10


Tagged

mail utf8


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

jplr


Send UTF-8 encoded mail with custom to and reply-to


 / Published in: PHP
 

Parameters:\r\n- $to - email address to send the mail\r\n- $subject - subject\r\n- $message - message body (HTML allowed)\r\n- $fromname - sender\'s name\r\n- $fromemail - sender\'s email\r\n- $replytoname = custom, different from $fromname name to be used for replying to this message\r\n- $replytoemail = custom, different from $fromemail email to be used for replying to this message

  1. function send_mail($to, $subject, $message, $from_name, $from_email, $reply_to_name=false, $reply_to_email=false) {
  2. if($reply_to_name == false) $reply_to_name = $from_name;
  3. if($reply_to_email == false) $reply_to_email = $from_email;
  4. if(!preg_match("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,6}^", $to)){
  5. return false;
  6. }
  7. if(!preg_match("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,6}^", $from_email)){
  8. return false;
  9. }
  10. if(!preg_match("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,6}^", $reply_to_email)){
  11. return false;
  12. }
  13. $headers = 'MIME-Version: 1.0' . PHP_EOL;
  14. $headers .= 'From: '.$from_name.' <'.$from_email.'>' . PHP_EOL;
  15. $headers .= 'Reply-To: '.$reply_to_name.' <'.$reply_to_email.'>' . PHP_EOL;
  16. $headers .= 'Return-Path: '.$from_name.' <'.$from_email.'>' . PHP_EOL;
  17. $headers .= 'Content-Type: text/html; charset=UTF-8'.PHP_EOL;
  18. $headers .= 'Content-Transfer-Encoding: base64 '.PHP_EOL;
  19. $headers .= 'X-Mailer: PHP/' . phpversion();
  20.  
  21. $subject = '=?UTF-8?B?'.base64_encode($subject).'?=';
  22. $message = chunk_split(base64_encode($message));
  23.  
  24. if(@mail($to, $subject, $message, $headers)) {
  25. return true;
  26. }
  27. else {
  28. return false;
  29. }
  30. }

Report this snippet  

You need to login to post a comment.