I am trying to send rich email to people on a mailing list and am running into trouble getting the rich email part to work. My code says all recipients have been sent an email, but the email is not received.

Here is what I have:

 #assemble mail headers############################# 
		   $headers  = "MIME-Version: 1.0\r\n";
     	   $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
		   $headers .= "From:$admin_email\r\n";
		   $headers .= "Reply-To:$admin_email\r\n"; 
		   $headers .= "X-Priority: 1\r\n";
		   $headers .= "X-MSMail-Priority: High\r\n";
		 ##################################################

	#connect to database and retreive email addresses
	$db = mysql_connect("localhost", "username", "password") or die("Could not connect to the database.");
	mysql_select_db("mydbname",$db) or die(mysql_error());

	$query = "SELECT * FROM mailing_list";
	$result = mysql_query($query,$db);
	#determine how many people are on the list for later comparison with successful emails
	$num_members = mysql_num_rows($query);	

	$num_success = 0;	#init variable to track the number of successfully sent emails
	$failure = "";		#var to be used to track all email failures

	do{
		$ok = mail($mailarray['email'], $subject,$letter,$headers);
		if($ok){
			sleep(5);	#pause for 5 seconds between emails to avoid flooding the mail server
			$num_success++;
		}else{
			$failure .= "Email could not be sent to ".$mailarray['email']."\n"; #record failures
		}
	} while ($myrow = mysql_fetch_array($result));

	#if all went well, store a success message
	if(empty($failure)) $failure = "There are no failure messages to report";

	#when loop is done, send email to administrator with results of process
	if(mail($admin_email,"Newsletter Send Results","Your newsletter has been sent. Below are the results of the send process:

		Number of Recipients: $num_members
		Number of Successful Emails Sent: $num_success
		Failure Messages: $failure")){
		echo "The newsletter sending porcess is complete!<br />
			  An email has been sent to ".$admin_email." with the results of the newsletter sending process.";
	}

Ideally I would like to be able to send a multipart email that will deliver plain text or HTMl versions of the message, but will settle for a working HMTL based email.

    where did you get $mail_array from?

      That is definately a mistake. $myrow should be $mailarray, like this:

      } while ($mailarray = mysql_fetch_array($result));
      

      I am making the change and testing now.

      Any suggestions on how to pull off the multipart email to be able to deliver plain text emial to users who can't receive rich email?

        That did fix it. The ehaders sent just fine and the HTML email arrived. Thanks for pointing that out. I just found out that CSS is not recognized by email clients.

        So how do I ge this thing to send plain text email as well. I have seen plenty of examples of what the headers should look like, but I am still unclear about how to assemble headers when teh message needs to be sandwiched in there multiple times. Won't that cause a problem with the mail function? Am I thinking abou this backwards?

          I just want to alter what I have to be able to send both an HTML version of the message and a text version.

            Does anyone have tips on how to convert the above code to send plain text and HTML email?

            Helloooo?

              10 days later

              If it's any help earthlingzed, a newsletter app that I've put together for a client of ours simply has the plain text before the html (the two are separated by --MIME_BOUNDRY-- tags), this way, subscribers whose email client can interpret html see this, and those that don't see the plain text version..something along the lines of....

              // Set the headers for the mail
              $headers = "From: sitename <someone@somewhere.co.uk>\r\n";
              $headers .= "X-Sender: someone@somewhere.co.uk\r\n";
              $headers .= "X-Mailer: sitename\r\n"; //mailer
              $headers .= "X-Priority: 3\r\n"; //1 UrgentMessage, 3 Normal
              $headers .= "Return-Path: <someone@somewhere.co.uk>\r\n";
              $headers .= "Reply-To: <someone@somewhere.co.uk>\r\n";

              $headers .= "MIME-Version: 1.0\r\n";
              $headers .= "Content-Type: multipart/alternative; boundary=\"MIME_BOUNDRY\"\r\n";

              $headers .= "This is a multi-part message in MIME format.\r\n";

              // add the plain text for non html mail readers.
              $mailbodytext = "--MIME_BOUNDRY\r\n";
              $mailbodytext .= "Content-Type: text/plain; \r\n";
              $mailbodytext .= "\tcharset=\"iso-8859-1\"\r\n";
              $mailbodytext .= "Content-Transfer-Encoding: quoted-printable\n\n\n";

              $mailbodytext .= "This e-mail is available in HTML format only. No text version is available.";
              $mailbodytext .= "\n\n\n";

              // now we add the html section of the message
              $mailbodytext .= "--MIME_BOUNDRY\n";
              $mailbodytext .= "Content-Type: text/html;\n";
              $mailbodytext .= "\tcharset=\"iso-8859-1\"\n";
              $mailbodytext .= "Content-Transfer-Encoding: quoted-printable\n";
              $mailbodytext .= "\n";
              $mailbodytext .= "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
              $mailbodytext .= "<html>\n";
              $mailbodytext .= " <head>\n";
              $mailbodytext .= " <link rel=3D\"stylesheet\" href=3D\"http://www.somesite.co.uk/assets/style_mozilla.css\" type=3D\"text/css\">\n";
              $mailbodytext .= " <title>Some title</title>\n";
              $mailbodytext .= " </head>\n";
              $mailbodytext .= " <body>\n";
              $mailbodytext .= " <table border=3D\"0\" cellpadding=3D\"0\" cellspacing=3D\"0\" width=3D\"650\">\n";

              etc etc etc

              $mailbodytext .= " </body>\n";
              $mailbodytext .= "</html>\n";

              // end message part
              $mailbodytext .= "\n";
              $mailbodytext .= "--MIME_BOUNDRY--\n";

              Where you see the <html element>=3D\"0\" stuff is for Outlook Express I believe, as I found that html wasn't displaying properly in these clients and needed the extra tags before the double quotes.

              There's a better explanation on php.net's site under the mail functions if it's any help?

              Kind regards

              Greg

                That is exactly what I needed to know. Thanks so much for the help! 😃

                  Write a Reply...