2

I'm using a basic script on a 1&1 hosted server:

$recipient = "email@domain.com";
$sender_name = $_POST['name'];
$sender_email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];

$headers = "MIME-Version: 1.0"."\r\n";
$headers .= "Content-type:text/html; charset=UTF-8"."\r\n";
$headers .= "From: {$sender_name} <{$sender_email}>"."\r\n";
$headers .= "Reply-to: {$sender_name} <{$sender_email}>"."\r\n";

mail($recipient, $subject, $message, $headers);

..but for some reason am not receiving any emails, nor any errors as per PHP mail() function not sending email instructs.

I thought this may be a server issue but 1&1 states that it is fully supported. I've also sent emails from this server/hosting before using just a recipient, subject and body and so I'm rather unsure as to why it is not working now!

UPDATE

Sending without headers, i.e.:

mail($recipient, $subject, $message);

..does work, so it would appear to be an issue with using the headers?

Community
  • 1
  • 1
Callan Heard
  • 727
  • 1
  • 8
  • 18

2 Answers2

7

There may be many reasons, for example you should study what SPF is.

The $sender_email can't be any address, for example you can't put a gmail address and send emails claiming to be that sender, without any authentication, you aren't allowed to send email on behalf on that address, because I could for example send emails putting your address in the from, pretenting to be you when I'm not (I tried to use simple words)

You should use in the From something ending in @yourdomain.com, and set up SPF to allow your server's IP to send emails for that domain. OR otherwise send emails through SMTP (with PHPmailer, for example, it's very easy)

the_nuts
  • 5,634
  • 1
  • 36
  • 68
1

Errors in header does affect the mail delivery. if you send email with wrong headers, for example, let's say the email of the user is user@email.com and his name is "user user", and you send the email to user@email.com it might cause the email to go in spam or not accepted at all if it doesn't match on server.

Gmail, for one, has a name associated with every email id. if you put a wrong name with the email, there's a chance the email might not show up in inbox or sometimes even spams.

"But in the link I attached the example given uses a 'from' header set by a form input?"

if you want your users to be able to send email to you, best idea would be to use your own email address as the sender email. E.g. if your website is at website.com, configure an email like "contact@website.com" and configure your script to use this as From header, you can send it to your own email at the same domain(website.com) or any other email which you authorize. You can add the users's actual details in the mail. that'll ensure the successful delivery of the email always.

Mridul Kashyap
  • 704
  • 1
  • 5
  • 12