Scaraux
Scaraux

Reputation: 4152

Php Mail does not work if the sender mail isn't valid

In my website, I have a little contact form that uses PHP to send a mail to my personal e-mail address. In the form, I ask the user for his e-mail address. If the address exists, the script works perfectly, and will receive the message very quickly. But if the user enters a wrong mail address, I won't never receive it. Is it normal ? Here is a little piece of my script...

/* Prepare Email Subject */

$email_subject = '[CONTACT FORM]' . "\t" . $title;

/* Prepare Email Message */

$email_message .= "Nom: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Telephone: ".clean_string($tel)."\n";
$email_message .= "Message: ".clean_string($msg)."\n";

/* Prepare Email Header */

$headers = 'From: '.$email."\r\n".

'Reply-To: '.$email."\r\n" .

'X-Mailer: PHP/' . phpversion();

/* Send Email and redirect */

mail($email_to, $email_subject, $email_message, $headers);  
header ('location: /?status=success#contact');

I would like to thank you all for your answers. I am very sorry if the problem isn't clear enough, and I hope my english is understandable (I'm a Frenchy) !

For those who don't understand what is my problem :

In my script, I put the user's mail adress in the mail header. If the user gives me a valid e-mail, then I will receive the message, otherwise I won't.

Don't worry about the variables, this is just the last part of my script, but I verify all of them.

When I said "only if the address exists", I really wanted to mean that apparently, if the address is not registered, or if the domain does not exists, it does not work. Before sending the mail, I verify if the address is "valid" by applying a Regex test. And if I enter, for example : [email protected] I won't receive the mail. That's why it is weird, the mail address format is good.

blue112, I am going to create a postmaster address, and use it to send these mails.

Upvotes: 0

Views: 941

Answers (2)

Varedis
Varedis

Reputation: 758

Since you most likely can't actually do anything useful with the email if they have entered an invalid address, you should check that the email address is valid, there are varying degrees of annoyance for the user that you can go through to get this information to be valid:

  • Using a regex to match the email address
  • Asking the user to confirm their email, this captures mistypes (some annoyance)
  • Ask the user to register first before they can use the contact form (big annoyance)

You could also make your mail have static From and Reply-To addresses that you own, and include the users entered email address in the actual body of the message, this ensures you actually get the email even though they may have given your contact form a rubbish email address.

Upvotes: 0

blue112
blue112

Reputation: 56432

You shouldn't use the user's mail to send a mail.

  1. It's bad practice. You are kind of usurping it's identity
  2. If user uses, for instance, a gmail, and you're setting it to gmail, gmail won't trust you because he knows it never sends the email
  3. They are great chances the email falls in spam box

The best way to go is a use a "valid" and trusted email to use in the "from" header field.

Also, about your script, you can wrap the `mail``function in a if to know if everything went fine during the sending.

if (mail($email_to, $email_subject, $email_message, $headers))
{
    header ('location: /?status=success#contact');
}
else
{
    echo "Sending mail failed";
}

This provides you no garentee that the mail succesfully arrived, however.

Upvotes: 1

Related Questions