HELP NEEDED - PHP Mail function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

HELP NEEDED - PHP Mail function

Post by RobertGonzalez »

I am using the following snippet of code in an application that grabs the item entered into the DB, then emails it to a selected admin. The problem is that when the email is received by the admin, the FROM header shows "unspecified" and the actual "From: so and so" shows up in the body just below the Content Type header, which also shows up inside the body. Can someone tell me what I am doing wrong?

Code: Select all

<?php
		//
		// Step 2, grab the question that was just added and email it
		//
		$sql = "SELECT f.faq_id, f.faq_passcode, f.faq_from_fname, f.faq_from_lname, f.faq_from_email, a.admin_id, a.admin_fname, a.admin_lname, a.admin_email
				FROM nummi_faqs f, nummi_admins a 
				WHERE a.admin_id = f.admin_id 
				ORDER BY f.faq_id DESC 
				LIMIT 0,1";
		if ( !($result = $db->sql_query($sql)) )
		{
			set_error('Could not find the last question.');
		}

		while ( $row = $db->sql_fetchrow($result) )
		{
			$faq_id = $row['faq_id'];
			$passcode = $row['faq_passcode'];
			$ffname = $row['faq_from_fname'];
			$flname = $row['faq_from_lname'];
			$femail = $row['faq_from_email'];
			$admin_id = $row['admin_id'];
			$afname = $row['admin_fname'];
			$alname = $row['admin_lname'];
			$aemail = $row['admin_email'];

			$to = "$afname $lname <$aemail>"; 
			$itsabout = "PEP Website Question";
			$mcontent = "Hi $afname, someone has just asked you a PEP question.\n";
			$mcontent .= "To view this question, answer it and add it to the database, visit\n ";
			$mcontent .= "http://nummi.purelyauto.com/questions_admin.php?mode=ans&admin=$admin_id&ques=$faq_id&check=$passcode \n";

			/* To send HTML mail, you can set the Content-type header. */
			$headers  = "MIME-Version: 1.0\r\n";
			$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
			/* additional headers */
			if ( ($femail !='') ) //if there is a from email
			{
				if ( ($ffname !='') && ($flname !='') ) //if there is a first AND last name
				{
					$headers .= "From: $ffname $flname <$femail>\r\n";
				}
				else
				{
					$headers .= "From: <$femail>\r\n";
				}
			}
			else
			{
				$headers .= "From: Site Admin <noreply@purelyauto.com>\r\n";
			}

			/* send it */
			mail($to, $itsabout, $mcontent, $headers);
		}// End While

?>
The body of the email the admin gets is:
Content-type: text/plain; charset=iso-8859-1
From: Robert Gonzalez <robert@everah.com>


Hi Robert, someone has just asked you a PEP question.
To view this question, answer it and add it to the database, visit
http://nummi.purelyauto.com/questions_a ... 1087565982
In this email, the Sender in the header is shown as Sender Unspecified.

Thanks in advance. As always, all help would be appreciated.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Code: Select all

<?php
function socketmail($toArray, $subject, $message) { 
	// $toArray format --> array("Name1" => "address1", "Name2" => "address2", ...) 	
	ini_set(sendmail_from, "debug@adventurkey.com"); 	
	$connect = fsockopen (ini_get("SMTP"), ini_get("smtp_port"), $errno, $errstr, 30) or die("Could not talk to the sendmail server!"); 
	$rcv = fgets($connect, 1024); 
		
	fputs($connect, "HELO {$_SERVER['SERVER_NAME']}\r\n"); 
	$rcv = fgets($connect, 1024); 

	while (list($toKey, $toValue) = each($toArray)) { 	
		fputs($connect, "MAIL FROM:debug@adventurkey.com\r\n"); 
		$rcv = fgets($connect, 1024); 
		fputs($connect, "RCPT TO:$toValue\r\n"); 
		$rcv = fgets($connect, 1024); 
		fputs($connect, "DATA\r\n"); 
		$rcv = fgets($connect, 1024); 
				
		fputs($connect, "Subject: $subject\r\n"); 
		fputs($connect, "From: Ad <debug@adventurkey.com>\r\n"); 
		fputs($connect, "To: $toValue  <$toValue>\r\n"); 
		// key array deki elemanlarin key value lari, kime gonderilecekse onun ismini iceriyor.
		//fputs($connect, "To: $toKey  <$toValue>\r\n"); 
		fputs($connect, "X-Sender: <debug@adventurkey.com>\r\n"); 
		fputs($connect, "Return-Path: <debug@adventurkey.com>\r\n"); 
		fputs($connect, "Errors-To: <debug@adventurkey.com>\r\n"); 
		fputs($connect, "X-Mailer: PHP\r\n"); 
		fputs($connect, "X-Priority: 2\r\n"); 
		fputs($connect, "Content-Type: text/html; charset=iso-8859-9\r\n"); 
		fputs($connect, "\r\n"); 
		fputs($connect, stripslashes($message)." \r\n"); 
				
		fputs($connect, ".\r\n"); 
		$rcv = fgets($connect, 1024); 
		fputs($connect, "RSET\r\n"); 
		$rcv = fgets($connect, 1024); 
} 	
	fputs ($connect, "QUIT\r\n"); 
	$rcv = fgets ($connect, 1024); 
	fclose($connect); 
	ini_restore(sendmail_from); 
} 
?>
Here is a function that I used for mailing, you can specify what you need.
Some comments in my native language, but I'm sure you have potential to understand them :)
I hope this helps...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

[SOLVED] - PHP Mail problem

Post by RobertGonzalez »

It seems that the extra '\r' were throwing my mail script out of whack. Removing those seemed to clear things up.
Post Reply