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


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.