Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mail() not working properly

Status
Not open for further replies.

sysadmin42

Technical User
May 6, 2005
138
I have a script where you first select the users (from the db) that you want to send a message to, next page you write the message, and then click send and it goes out to everyone you selected. It only seems to be sending to one or two recipients and it seemed to work fine for a while.

The odd thing is that when it writes the log (function write_log() below) all the recipients are listed.

Any thoughts?

Here's the code:

Code:
//variables from previous pages
//these are used just in case the user hits the back button
$_SESSION['tmp_subject']=$_POST['subject'];
$_SESSION['tmp_message']=$_POST['message'];
$_SESSION['tmp_recipients']=$_POST['recipients'];
if ($_POST['subject']=="" || $_POST['message']=="") header("Location: emailer2.php?v2e=1");

unset($_SESSION['tmp_subject']);
unset($_SESSION['tmp_message']);
unset($_SESSION['tmp_recipients']);


//emailer
	$option['other_email']=1; //take this out later and add to user options
	$server_email_limit=999;
	$subject=str_replace(" ","",stripslashes(strip_tags(unhtmlspecialchars($_POST['subject']))));

	$msg=str_replace(" ","",stripslashes(strip_tags(unhtmlspecialchars($_POST['message']))));
	$msg.="\n\n\n--------This message sent courtesy of BNI Connection. To log on, visit [URL unfurl="true"]www.bniconnection.com";[/URL]

	$message="";
	$mailheaders="From: {$_SESSION['firstname']} {$_SESSION['lastname']} <{$_SESSION['my_email']}>\n";
	$mailheaders.="Reply-To: {$_SESSION['my_email']}";

//get receipient emails
	$sql_email="SELECT id,email FROM members WHERE id IN ({$_POST['recipients']}) AND active='1' LIMIT 0,$server_email_limit";
	$result_email=@mysql_query($sql_email,$conn) or die(mysql_error());

//send to each recipient
	while ($emailer=mysql_fetch_array($result_email)) {
		$option=get_profile_options($emailer['id'],$conn);
		$recipient=$emailer['email'];	
			mail($recipient,$subject,$msg,$mailheaders);
			$message.="user emailed: $recipient<br>";
	}
	write_log($_SERVER['PHP_SELF'],"Email Sent.<br>".$message,$conn);
 
The first thing after reading through your code is that there is no return value check for the actual mail function.
The mail() function returns true/false for successful/failed execution. Your code assumes that calling the mail function will succeed under all circumstances.
Instead of sending individual e-mails you could also just send one mail with everybody either in a CC header or BCC. That would hand the majority of work to the mail server and reduce the execution of the mail() function to a single instance.
 
should there be a limit in the number of addresses I can BCC? Also, that wouldn't help if I was sending out personalized emails- which might be a later expansion.

thanks for pointing out the return value. duh...

so I put code in like that and none of the while loops returned false, so now it looks like there's a problem with the mail server?
 
Is it possible that the recipients have filter software that weeds out your mails? Some free e-mail providers such as yahoo or gmail filter out messages with incomplete headers etc.
Make sure all headers required are present.
There were several threads in this forum about mail headers required to send successfully to hotmail etc. Use the search function in the top of this site.

You are absolutely right that personalized e-mails have to be sent one at a time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top