5

I have tried to set-up an email.html and an action.php so that someone can send an email from a website. Here is the code in email.html

<form action="./action.php" method="post">
    <p>Your Name</p>
    <p><input type="text" name="name" /></p>
    <p>Email</p>
    <p><input type="text" name="email" /></p>
    <p>Services</p>
    <p><input type="text" name="service" /></p>
    <p>Requests</p>
    <p><textarea rows="8" cols="32" name="comment"></textarea></p>
    <p><input type="submit" value="Send" /></p>
</form>

In action.php I have

<?php
    $to = "foo@outlook.com";
    $subject = "Test mail";
    $message = "Hello! This is a simple email message.";
    $from = "foo2@gmail.com";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
    echo "Mail Sent.";
?>

The information entered in email.html successfully load into action.php, but nothing is received in my Outlook inbox from the email method. Am I missing something?

jh_
  • 411
  • 2
  • 5
  • 9
  • You're blindly assuming that the email's been sent. You need to check the return value of `mail` to check if it's actually sent: `if( mail(...) ) { // mail sent }`. – Amal Murali Sep 28 '13 at 19:32
  • 1
    Is there an active email sender on your hosting machine like sendmail or PHPMailer? – Felix G. Sep 28 '13 at 19:32
  • 1
    Do you have an SMTP server installed on your system? – aaron Sep 28 '13 at 19:34
  • 2
    @AmalMurali To be clear, a return value of true indicates *acceptance* by the local SMTP. **It says nothing about delivery**. – quietmint Sep 28 '13 at 19:37
  • Thank you, that should have been my next move. I just threw in the if/else logic, and the else is running, so it is not sending. I am hosting the website at namecheap and there is definitely PHP support, I might need to look into the setting. – jh_ Sep 28 '13 at 19:38
  • @user113215: That's correct. But in its current state, the 'email sent' message will be echoed out every time regardless of whether or not the email's actually sent. – Amal Murali Sep 28 '13 at 19:42

3 Answers3

6

Start by checking the return value of mail to see if the message is being accepted by your SMTP server.

Also, per Namecheap's documentation, your From address is invalid.

Only domains that are hosted on our servers can be used in 'From' field. Any domain that is not hosted with us cannot be added to 'From' field. We had to take this measure to prevent sending spam using forums, guest books and contact forms scripts. For your site scripts to work properly you should set 'From' field to email account that has been created in your cPanel.

Even if this were allowed by your hosting company, you shouldn't be sending mail from @gmail.com using non-Gmail servers anyway. It will often be blocked by SPF and other such anti-spam measures on the receiving end.

quietmint
  • 13,885
  • 6
  • 48
  • 73
2

First check result of the mail. You also should check the php error log file.

when your mail is really going out, use an valid sender email, then you can get bounces from the receiving mail server.

don't forget, there are many anti spam technology's. most important is that you respect and know how SPF is working. that's one of the most fails, websites doesn't send emails which don't arrive at recipient.

coding Bott
  • 4,287
  • 1
  • 27
  • 44
0

Well, banged my head hard.. Just to realise they were going in Spam folder of my "$to" email id. Also check this on your side.

Mitech
  • 400
  • 4
  • 6