Page 1 of 1

Send mail problem HTML works but text does not.

Posted: Wed Jul 14, 2004 7:55 pm
by hawleyjr
I'm using the below function to send mail in my apps. The mail sends and works fine if I'm viewing the HTML format but when I test it in a text only mail client I get an error saying I got mail but it can't read it??? any help would be great.

Code: Select all

<?php
/* SEND EMAIL */
function send_mail($to,$to_name,$cc,$cc_name,$bcc,$bcc_name,$from,$from_name,$subject,$plain_Text_message,$HTML_Message){
	
	//TO AND FROM ARE THE ONLY REQUIRED FIELDS
		$a_invalid_email_send_mail = array();
		
	if(trim($to)=='' or !isset($to) or is_null($to) or !is_email_valid($to,FALSE)){
		$a_invalid_email_send_mail[INVALID_EMAIL_1] = 'to';
	}
	if(trim($from)=='' or !isset($from) or is_null($from) or !is_email_valid($from,FALSE)){
		$a_invalid_email_send_mail[INVALID_EMAIL_2] = 'from';
	}
	if(!is_email_valid($cc,TRUE)){
		$a_invalid_email_send_mail[INVALID_EMAIL_3] = 'cc';
	}
	if(!is_email_valid($bcc,TRUE)){
		$a_invalid_email_send_mail[INVALID_EMAIL_4] = 'bcc';
	}
	
	
	if(count($a_invalid_email_send_mail)>0)
		return 	$a_invalid_email_send_mail;
	else
		unset($a_invalid_email_send_mail);	
	

	//add From: header 
	$headers = "From: $from\r\n"; 
	$headers .= "Cc: $cc\r\n";
	$headers .= "Bcc: $cc\r\n";
	$headers .= "Reply-To: $from\r\n";
	
	//specify MIME version 1.0 
	$headers .= "MIME-Version: 1.0\r\n"; 
	
	//unique boundary 
	$boundary = md5(uniqid(rand(), true)); 
	
	//tell e-mail client this e-mail contains//alternate versions 
	$headers .= "Content-Type: multipart/alternative" . 
	   "; boundary = $boundary\r\n\r\n"; 
	
	//message to people with clients who don't 
	//understand MIME 
	$headers .= "This is a MIME encoded message.\r\n\r\n"; 
	
	//plain text version of message 
	$headers .= "--$boundary\r\n" . 
	   "Content-Type: text/plain; charset=ISO-8859-1\r\n" . 
	   "Content-Transfer-Encoding: base64\r\n\r\n"; 
	$headers .= chunk_split(base64_encode($plain_Text_message)); 
	
	//HTML version of message 
	$headers .= "--$boundary\r\n" . 
	   "Content-Type: text/html; charset=ISO-8859-1\r\n" . 
	   "Content-Transfer-Encoding: base64\r\n\r\n"; 
	$headers .= chunk_split(base64_encode($HTML_Message)); 
			
	//send message 
	$mail_status = mail($to, $subject, "", $headers); 
		
	return $mail_status;
		
} 
?>