Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I used this code all around my projects and it works fine:

mail("", "Subject", "message");

Now, I´m developing on php and if I use this code, the message is not sent, but it works correctly from the terminal.

Am I missing something?


I send the mail from the terminal like this:

echo "Test email" | mail -s "My Subject"
share|improve this question

closed as off topic by Michael Hampton, Nils, mdpc, Khaled, MadHatter Mar 8 '13 at 22:13

Questions on Server Fault are expected to relate to server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

Make sure that in your php.ini you have display_errors on, and error_reporting turned up to E_ALL, and let us know if there are any errors. – nickgrim Mar 7 '13 at 12:23
there is no error on /var/log/httpd/error_log and I did what you said... – Kioko Kiaza Mar 7 '13 at 12:30
up vote 0 down vote accepted

It's hard to send a well formed email with PHP, especially if you want to use more advanced feature like attached files. And the risk to pass in spam with possibly bad header or content is high.

I really advise you to use SwiftMailer ( which is an excellent PHP library to send emails.

share|improve this answer
It is not my php application, I am suppose to bring it "online" as well as I can so I cann´t touch any code.... – Kioko Kiaza Mar 7 '13 at 14:12
That's not a problem, you just have to put the SwiftMailer directory somewhere in your project, then use require to include it in your code. It's compatible with any project. – Godefroy Mar 7 '13 at 16:59
Finally we speak with developers and the are going to use this library. Thx – Kioko Kiaza Mar 13 '13 at 9:25

Include the Header field as:

$to = ""; $subject = "Test mail"; $message = "Hello! This is a simple email message."; $from = ""; $headers = "From:" . $from; mail($to,$subject,$message,$headers); echo "Mail Sent.";

share|improve this answer
Could you expand a bit on what you think the problem is and why you think this will fix it? – nickgrim Mar 7 '13 at 12:31
I did, it says "Message sent" but I don´t receive any message. – Kioko Kiaza Mar 7 '13 at 12:32
echo was just to display some message. Copy the above code in <?php ?> tags. save in a .php file and call it from terminal like php filename.php and check if the mail is delivered. – Ankit Mar 7 '13 at 12:39
I did and I have this answer: php testmail.php. RESULT: sh: -t: command not found – Kioko Kiaza Mar 7 '13 at 14:12

If you are using a system with SELINUX Enforcing then you may need to tell SELINUX to let php send mail.

setsebool -P httpd_can_sendmail=1
share|improve this answer
I did and it says: setsebool: SELinux is disabled. I tried after that and still no mail is send. – Kioko Kiaza Mar 7 '13 at 14:11
Okay yes, SELinux is disabled then this answer will have no effect as you have observed. – squarebear Mar 7 '13 at 14:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.