Page 1 of 1

Not Receiving Email

Posted: Fri Sep 15, 2006 5:48 am
by Mig
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, I have written a contact form which doesn't seem to send any email. However, I have tested the mail function and it returns true, so why am I not getting an email!? For the purpose of testing, I have stripped it down and made it as simple as possible and still I get no email. Does anybody know what the reason for this may be? I have tested it with 3 different email addresses. This is so typical. Always happens to me

Code: Select all

<?php
$errmsg = "";
$name = "";
$email = "";
$subject = "";
$message = "";
if(isset($_POST["send"])) {
	$name = $_POST["name"]; 
	$subject = $_POST["subject"];
	$message = $_POST["message"];
	$to = "example@site.com";
	mail($to, $subject, $message);
} else {
?>
      <table width="100%" style="margin-top:20px;"> 
        <form id="contactform" name="contactForm" method="post"> 
          <tr height="25px"> 
            <td width="70px" align="right" ><h2 class="contactTitle">Name:&nbsp;&nbsp;</h2></td> 
            <td><input type="text" id="name" name="name" size="40" class="contactField" value="<?=$name;?>"></td> 
          </tr> 
          <tr height="25px"> 
            <td align="right"><h2 class="contactTitle">Email:&nbsp;&nbsp;</h2></td> 
            <td><input type="text" id="email" name="email" size="40" class="contactField" value="<?=$email;?>"></td> 
          </tr> 
          <tr> 
            <td>&nbsp;</td> 
          </tr> 
          <tr height="35px"> 
            <td align="right"><h2 class="contactTitle">Subject:&nbsp;&nbsp;</h2></td> 
            <td><input type="text" id="subject" name="subject" size="40" class="contactField" value="<?=$subject;?>"></td> 
          </tr> 
          <tr> 
            <td valign="top" align="right"><h2 class="contactTitle">Message:&nbsp;&nbsp;</h2></td> 
            <td><textarea rows="10" id="message" name="message" cols="60" class="contactField"><?=$message;?></textarea></td> 
          </tr> 
          <tr height="35px"> 
            <td>&nbsp;</td> 
            <td><input type="submit" id="send" name="send" value="Send Message"></td> 
          </tr> 
        </form> 
      </table> 
      <?php
}
?>
Please help! Thanks.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Sep 15, 2006 5:53 am
by blackbeard
Is the web hosting site configured to send out mail?

Posted: Fri Sep 15, 2006 7:39 am
by Chris Corbyn
mail() returns true if SMTP or sendmail accepts it but does not know if it was actually sent or not. SMTP/sendmail can reject based in bad addresses etc but not really much more than that.

The best thing to do is to create a special email account for collecting "bounce" emails. If you set this as your return path all bounces will come back to this account so you can deal with them accordingly.

EDIT | By the way it wouldn't surprise me if you email is not arriving due to badly formed headers or smtp blacklisting.

Posted: Fri Sep 15, 2006 8:52 am
by Mig
But I don't know what SMTP means... :oops: I'm new to PHP.

What is SMTP blacklisting and what are headers?

Could you suggest what I need to do?

Posted: Fri Sep 15, 2006 9:04 am
by Mig
Ok I know what SMTP stands for but I don't know what it would imply if it's blacklisted (by who)?

Are you saying if I add a return path, the emails will arrive to that?

Posted: Fri Sep 15, 2006 4:01 pm
by Chris Corbyn
Mig wrote:Ok I know what SMTP stands for but I don't know what it would imply if it's blacklisted (by who)?

Are you saying if I add a return path, the emails will arrive to that?
SMTP is the protocol which sends mail in the same way HTTP is the protocol that serves web pages.

Headers are the top part of the email:

If you've ever viewed the source of an email it looks like this:

Code: Select all

Return-path: <mail@chriscorbyn.co.uk>
Envelope-to: chris@w3style.co.uk
Delivery-date: Mon, 11 Sep 2006 09:12:09 +0000
Received: from [127.0.0.1] (helo=www.swiftmailer.org)
	by w3style.co.uk with esmtp (Exim 4.54)
	id 1GMhph-0005gm-5X
	for chris@w3style.co.uk; Mon, 11 Sep 2006 09:12:07 +0000
To: "Chris Corbyn" <chris@w3style.co.uk>
From: "User" <user@tld.com>
Reply-To: <user@tld.com>
Subject: Some subject
Date: Mon, 11 Sep 2006 09:12:01 +0000
X-Mailer: Swift 2.1.13 by Chris Corbyn
MIME-Version: 1.0
Content-Type: multipart/alternative;
	boundary="_=_swift-17121382824505286127cae2.75326524"
Content-Transfer-Encoding: 8bit
X-Spam_score: -2.7
X-Spam_score_int: -26
X-Spam_bar: --

This part of the E-mail should never be seen. If
you are reading this, consider upgrading your e-mail
client to a MIME-compatible client.

--_=_swift-17121382824505286127cae2.75326524
Content-Type: text/plain; charset="ISO-8859-1"; format=flowed
Content-Transfer-Encoding: quoted-printable

Message here
--_=_swift-17121382824505286127cae2.75326524--
If you follow the email from top to bottom you eventually hit a blank line. That's where the headers stop.

The headers are responsible for telling both the SMTP server and the email client (Outlook/Thunderbird..) how to deal with the message. They contain information about encoding, recipient, what servers the mail passed through and it's way to delivery, the subject etc etc...

Those headers can be very tricky to get right.

The mail() function takes an extra parameter (the 4th argument) if you read at php.net in the documentation. That's where you can list additional headers to use. By default mail() will send a very rudimentary set of headers that look suspicios to many spam checkers.

The return path is like this:

1. You write an email and you click send
2. Now, your mail client (outlook?) tells the SMTP server two things. It says what address you *asked* to send it from and it says what address you actually sent it from. Outlook just sends the same address for each but using mail() or more easily by using another mailer if you feel comfortable with it you can specify two different addresses. The return-path address is where any errors go to.

You've probably sent emails before where you've mistyped the address and you start getting "Mailer Daemon Sending Failed..." messages? Those are bounce messages that would go to the return path address if you had one ;) They are quite easy to parse and get the email address from.

EDIT | Sorry I forgot to cover your point about blackilisting. Basically (most) SMTP servers run spam checkers for obvious reasons. These spam checkers use something called RBL (Real-Time Blacklisting). They basically lookup addresses on certain major lists online whom store IP addresses of known spammers. Sadly entire web hosts can be blacklisted due to one lone spammer who spoils it for the rest. Dynamic IPs are almost always blacklisted purely because they are difficult to keep track of and any "proper" host would be using a static IP.

You can check your IP in blacklists here: http://www.robtex.com/rbls.html

EDIT 2 | Changed "email addresses" to "IP addresses"