IngmarBoddington
6/1/2014 - 3:45 PM

Basic function for sending html / txt mixed content email via native php mail() function

Basic function for sending html / txt mixed content email via native php mail() function

function sendMail($toEmail, $fromEmail, $fromName, $replyEmail, $subject, $html = '', $text = '') {

  $boundary = "nextPart";
  $headers = 'From: '.$fromName.' <'.$fromEmail.'>'."\r\n";
  $headers .= 'Reply-To: '.$replyEmail."\r\n";
  $headers .= 'X-Mailer: PHP/'.phpversion()."\r\n";
  $headers .= "MIME-Version: 1.0\r\n";
  $headers .= "Content-Type: multipart/alternative; boundary = $boundary\r\n";
                
  //text version
  $message = "\n--$boundary\n"; 
  $message .= "Content-type: text/plain; charset=iso-8859-1\r\n";
  $message .= $text;
                
  //html version
  $message .= "\n--$boundary\n";
  $message .= "Content-type: text/html; charset=iso-8859-1\r\n";
  $message .= $html;
                
  if (mail($toEmail, $subject, $message, $headers)) {
    return true;
  } else {
    return false;
  }
}