Jump to content

First mail() statement works; second one intermittent?


bgoulette612

Recommended Posts

I feel bad posting this, because I'm sure it's probably simple, but I'm a relative PHP n00b, and I'm out of ideas...

 

I have a script that creates and sends an e-mail well enough: this first call to the mail() function works pretty much all the time.

 

What doesn't always work -- rarely, if ever, works! -- is a second mail() statement. Here's a pseudo-code version of what I have:

 

if( mail($to1 $subject1, $message1, $headers1) ) {
//Send another, different e-mail to a different address.
mail($to2, $subject2, $message2, $headers2);
}

 

For those into masochism, here's the actual code I'm trying:

 

...
if( constructEmail($aFormValues) ) {
sendAutoResponse($_SESSION['temp-fields'], $_SESSION['temp-request']);
}
...

function constructEmail($aFormValues) {
$to = "info@wgb.hyperology.com"; // $aFormValues['c-email'];
$subject = $aFormValues['c-int-address'] ? "Requesting information about {$aFormValues['c-int-address']}" : "Web comment/request";

//	Try to prevent injection...
$aFormValues['c-name'] = preg_replace('/(\\r|\\n)*/si', '', $aFormValues['c-name']);
$aFormValues['c-email'] = preg_replace('/(\\r|\\n)*/si', '', $aFormValues['c-email']);

$msg  = '<table cellspacing="5" cellpadding="0" border="0">'."\n";
$msg .= '<tr><td valign="top">'."<b>IP address</b></td><td>{$_SERVER['REMOTE_ADDR']}".'</td></tr>'."\n";
$msg .= '<tr><td valign="top">'."<b>Name</b></td><td>{$aFormValues['c-name']}".'</td></tr>'."\n";
$msg .= '<tr><td valign="top">'."<b>Telephone</b></td><td>{$aFormValues['c-phone']}".'</td></tr>'."\n";
$msg .= '<tr><td valign="top">'."<b>Mailing address</b></td><td>{$aFormValues['c-address']}".'</td></tr>'."\n";
$msg .= '<tr><td valign="top">'."<b>E-mail</b></td><td>{$aFormValues['c-email']}".'</td></tr>'."\n";
if( $aFormValues['c-int-address'] ) {
	$msg .= '<tr><td colspan="2"><hr /></td></tr>'."\n";
	$msg .= '<tr><td colspan="2" valign="top">'."Please send me information about <b>{$aFormValues['c-int-address']}</b>".'</td></tr>'."\n";
}

if( $aFormValues['c-comment'] != '' ) {
	$msg .= '<tr><td colspan="2"><hr /></td></tr>'."\n";
	$msg .= '<tr><td colspan="2">'."<b>Comment</b>".'</td><tr>'."\n";
	$msg .= '<tr><td valign="top" colspan="2">'.formatTextBlock($aFormValues['c-comment']).'</td></tr>'."\n";
}
$msg .= '</table>'."\n";

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "To: \"W.G. Blake's Agency\" <info@wgb.hyperology.com>\r\n";
$headers .= "From: \"{$aFormValues['c-name']}\" <{$aFormValues['c-email']}>\r\n";

$_SESSION['temp-fields'] = $aFormValues;
$_SESSION['temp-request'] = $aFormValues['c-int-address'] ? true : null;
return mail($to, $subject, $msg, $headers);
}

function sendAutoResponse($aFields, $bRequest = null) {
//	TODO: FIGURE OUT WHY THIS IS BROKEN?!
//	Create and send confirmation e-mail.
$to = $aFields['c-email'];
$subject = "Thank you for visiting W.G. Blake's Agency";

//	Try to prevent injection...
$aFields['c-name'] = preg_replace('/(\\r|\\n)*/si', '', $aFields['c-name']);
$aFields['c-email'] = preg_replace('/(\\r|\\n)*/si', '', $aFields['c-email']);

$msg  = "Dear {$aFields['c-name']},\n\n";
$msg .= "Thank you for your recent visit to and inquiries regarding W.G. Blake's Agency (http://wgb.hyperology.com). ";

if( $bRequest ) {
	$msg .= "We're preparing the information you requested (regarding our listing located at {$aFields['c-int-address']}), and we'll be in touch with you as soon as possible.";
} else {
	$msg .= "If you requested that we follow up with you, we'll do so accordingly as soon as possible.";
}

$msg .= "\n\n";
$msg .= "Sincerely,\nW.G. Blake's Agency\ninfo@wgb.hyperology.com\n\n";
$msg .= "(Note: This is an automated response. Please do not respond to this e-mail as no one will ever receive it.)\n";

$headers  = "To: \"{$aFields['c-name']}\" <{$aFields['c-email']}>\r\n";
$headers .= "From: \"W.G. Blake's Agency\" <auto-response@wgb.hyperology.com>\r\n";

mail($to, $subject, $msg, $headers);
}

 

I would greatly appreciate any insight anyone can provide! Thanks in advance!!!

Link to comment
Share on other sites

try ob_start() above everything, this allows output buffering, which means in short (or one of the meanings), that you can send headers more than once even after whitespace / text is made. Apparently your echoing text after every email you send (from what i can tell anyway), which isnt allowed with headers.. unless you declare ob_start() .

 

Sorry if im totally wrong, but i gave your code a quick scroll. But if the problem im assuming is right, then this should be it.

Link to comment
Share on other sites

No, the mail() function does not send HTTP headers, it sends SMTP headers which are entirely different. You get the "headers already sent" problem when you attempt to send HTTP headers after they've been sent once. I have many scripts that send more than one email message with no problems.

 

Ken

Link to comment
Share on other sites

No, the mail() function does not send HTTP headers, it sends SMTP headers which are entirely different. You get the "headers already sent" problem when you attempt to send HTTP headers after they've been sent once. I have many scripts that send more than one email message with no problems.

 

Ken

 

Alright, you got me here. Sorry for buttin' in with my lack of mail() experience.

 

Hope for the best bgoulette612

Link to comment
Share on other sites

Checking the return value of mail() would be a good start.

 

My next step would be to print out all the values you pass to mail() and do a manual inspection.

 

I've tried both testing and echoing the values of the second mail() statement with something like this:

 

if( mail($to2, $subject2, $message2, $headers2) ) {
echo $message2;
}

 

And that does in fact display the message contents, which tells me that the mail() statement is evaluating properly (returning true)...

 

One other thing: I'm running xajax, and the first mail() statement (and, actually, the second), all live within the $objResponse object, but my thought was that since I'm not actually echoing anything back to the response (in normal execution), it wouldn't affect things. If the first mail() statement failed, then maybe that'd be something, but I have no idea why it would cause a second statement to fail...

Link to comment
Share on other sites

I've tried the "staring at the code" approach, but it all looks fine to me.

 

Hmm.. do you have access to the mail logs?  If mail() returns true, that means php is telling you that the mail system accepted the message for delivery.  If it's not getting through, then the mail system must have done something with it.. maybe it tagged it as spam and ate it or something similar.

Link to comment
Share on other sites

btherl, thanks for at least staring  ;) !

 

Well, after having no real luck for most of the night, the e-mail that I thought was just never getting sent appeared in my inbox! Actually, about 10 of them did (probably all of them, but I stopped counting after the first few "failures.")

 

I'm not entirely sure how/where to look at any mail logs, so I haven't explored that yet. Thanks again for checking things out!

Link to comment
Share on other sites

Were they failed mail delivery notifications or were they the actual emails you sent?  If they failed it should give a reason.

 

Either way, it looks like your php script is fine :)  It's what happens to the email after php sends it that's the problem.

 

Normally mail logs will be in /var/log .. where exactly depends on which package you are using.  /var/log/mail.log and /var/log/postfix often have something useful in them.

Link to comment
Share on other sites

Were they failed mail delivery notifications or were they the actual emails you sent?  If they failed it should give a reason.

 

Either way, it looks like your php script is fine :)  It's what happens to the email after php sends it that's the problem.

 

Normally mail logs will be in /var/log .. where exactly depends on which package you are using.  /var/log/mail.log and /var/log/postfix often have something useful in them.

 

They were the actual e-mails I'd sent. And they had the correct datestamps, too (as far as I could tell). They just seem to be taking their sweet time getting to their destinations  ???

Link to comment
Share on other sites

I think your SMTP server socket is being closed and not opened in the second time.If you wanna know what actually is going on and if you wanna know what conversation is goung on with SMTP server and your HTTP server (e.g. How does mail() works) there is only 1 way.

Go here and download the Class (version 1.0.1.0 has some Bug Please download the previous versions)

http://zigmoyd.sourceforge.net/man/mail.php#mail

Please Read This Page for Installation and Instruction on how to use it.

http://zigmoyd.sourceforge.net/man/index.php

And While using it Just Do this Simple Hack / Patch  php:

<?php
......................
$mail = new mail("admin@localhost", "Mail Body"); 
$mail->show_cnv = true;//Show Conversation//Not written in the manual
..................
?>

It will Show the Compleate Conversation Like Client Chatting with SMTP server.

I hope this will help you to See the compleate Conversation.

It may also Happen you server doesnt let you sending 2 mails in a certain fraction of time. Use sleep(2); In that case.

Link to comment
Share on other sites

Were they failed mail delivery notifications or were they the actual emails you sent?  If they failed it should give a reason.

 

Either way, it looks like your php script is fine :)  It's what happens to the email after php sends it that's the problem.

 

Normally mail logs will be in /var/log .. where exactly depends on which package you are using.  /var/log/mail.log and /var/log/postfix often have something useful in them.

 

They were the actual e-mails I'd sent. And they had the correct datestamps, too (as far as I could tell). They just seem to be taking their sweet time getting to their destinations  ???

 

Okay now it seems like only Yahoo! isn't receiving the emails. Gmail seems to be working properly, and who knows? Maybe in an hour or two, I'll see the e-mails sitting in my Yahoo inbox...argh.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.