Callan Heard Callan Heard - 2 months ago 12x
PHP Question

PHP 'mail()' Function Not Sending Email

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

$recipient = "";
$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!


Sending without headers, i.e.:

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

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


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

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, beacuse 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, 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)