2

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

$recipient = "[email protected]";
$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?

8
  • are your smtp and pop servers configured? Commented Jul 25, 2016 at 8:57
  • @MridulKashyap I wouldn't know, but as I said - it has worked before without me having to configure them Commented Jul 25, 2016 at 9:03
  • what does the mail() function return? true or false? Commented Jul 25, 2016 at 9:05
  • True, however I've realised the messages are sending if I don't include the headers (they were in my spam folder) - is there an error in my headers anywhere that would cause this? Commented Jul 25, 2016 at 9:09
  • @CallanHeard Your headers look fine. probably a server credibility issue? issue? Commented Jul 25, 2016 at 9:13

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Ah yeah this makes sense thanks for the info
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 [email protected] and his name is "user user", and you send the email to [email protected] 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 "[email protected]" 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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.