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!

How can I get mail delivery errors returned while using mail()?

Status
Not open for further replies.

jimoblak

Instructor
Oct 23, 2001
3,620
US
When I send mail to a bad email address (such as an unknown user) with Groupwise or Outlook, the mail server at the destination will appropriately reply with an undeliverable error message. If I try the same thing with the script below, no error message is returned. Is there a header that I need to get these mail delivery errors to bounce back to me?

Code:
<?php

print ("<html><head><title>Web Mailer</title></head><body>\n");

$to = array ("me@mydomain.com","spamjim@yahoo.com","spamjim@hotmail.com");

for ($n=0; $n < count($to); $n++)
{

$notice_text = "This is a multi-part message in MIME format.";

$plain_text = "This is the plain text message";

$html_text = "<html><head><title>no title needed</title></head><body>This is the HTML mail</body></html>";


$html_text = wordwrap($html_text, 72);
$plain_text = wordwrap($plain_text, 72);

$semi_rand = md5(time());
$mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";
$mime_boundary_header = chr(34) . $mime_boundary . chr(34);

$from = "me@mydomain.com";
$replies = "me@mydomain.com";
$subject = "This is the subject line";

$headers = "From: Me <$from>\n";
$headers .= "Reply-To: $replies\n";
$headers .= "X-Sender: $from\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n"; //1 UrgentMessage, 3 Normal
$headers .= "Return-Path: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative;\n";
$headers .= "     boundary=";
$headers .= $mime_boundary_header;

$body = "$notice_text

--$mime_boundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

$plain_text

--$mime_boundary
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

$html_text

--$mime_boundary--";

if (@mail($to[$n], $subject, $body, $headers)) {
    echo "$to[$n]<br>";
} else {
    echo "<font color=red><b>FAIL:</b></font> $to[$n]<br>";
}

}

print ("</body></html>\n");
?>

- - I hope this helps - -
(Complain to someone else if it doesn't)
 
When you send an email you are giving the email to the server. You are not waiting for your SMTP server to respond if it was successful. You can't do this simply because it can take seconds, hours, or days to deliver an email.

To do this you would have to monitor the box that is sending the email ($from).
 
I am monitoring the mailbox for the sender ($from).

The problem is that something is wacky with my mail function that errors are not being bounced back to $from.

<i>me@mydomain.com</i> is standing in for the real email address that is used in my script. I removed the real email address to avoid email address harvesting from this forum.

Could this be an SPF issue?

- - I hope this helps - -
(Complain to someone else if it doesn't)
 
I just checked... Outlook and Groupwise are not sending through a server with SPF. This is not a SPF issue.

I am finding that the mail output is being overridden by the web server.

Code:
Return-Path: <www-data@thewebhost.company.com>

Can anyone suggest a way to keep my original return-path variable?

- - I hope this helps - -
(Complain to someone else if it doesn't)
 
Jim,

Have you considered using SMTP? It is one of the options with PHPMailer. You could connect to localhost's SMTP port and send mail, thus the return to sender will be just whatever you set it to be.
 
I wanted to avoid SMTP for various reasons. Here is what finally worked...

Code:
if (@mail($to[$n], $subject, $body, $headers, "-f $from")) {
    echo "$to[$n]<br>"; 
    } else {
    echo "<font color=red><b>ERROR</b></font> $to[$n]<br>"; 
    }

Note the addition of "-f $from" sent after the headers. This kicks sendmail into gear.

It helps to scroll all the way down the PHP online reference manual page for mail(). [bigsmile]

- - I hope this helps - -
(Complain to someone else if it doesn't)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top