<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: yoursite.com';
$to = 'contact@yoursite.com';
$subject = 'Customer Inquiry';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
I've tried creating a simple mail form. The form itself is on my index.html
page, but submits to a separate "thank you for your submission" page, thankyou.php
, where the above PHP code is embedded.
The code submits perfectly, but never sends an email. please help.
For anyone who finds this going forward, I would not recommend using
mail
. There's some answers that touch on this, but not the why of it.PHP's
mail
function is not only opaque, it fully relies on whatever MTA you use (i.e. Sendmail) to do the work.mail
will ONLY tell you if the MTA failed to accept it (i.e. Sendmail was down when you tried to send). It cannot tell you if the mail was successful because it's handed it off. As such (as John Conde's answer details), you now get to fiddle with the logs of the MTA and hope that it tells you enough about the failure to fix it. If you're on a shared host or don't have access to the MTA logs, you're out of luck. Sadly, the default for most vanilla installs for Linux handle it this way.A mail library (PHPMailer, Zend Framework 2+, etc), does something very different from
mail
. What they do is they open a socket directly to the receiving mail server and then send the SMTP mail commands directly over that socket. In other words, the class acts as its own MTA (note that you can tell the libraries to usemail
to ultimately send the mail, but I would strongly recommend you not do that).What this means for you is that you can then directly see the responses from the receiving server (in PHPMailer, for instance, you can turn on debugging output). No more guessing if a mail failed to send or why.
You also get the benefit of a better interface. With
mail
you have to set up all your headers, attachments, etc. With a library, you have a dedicated function to do that. It also means the function is doing all the tricky parts (like headers).I think this should do the trick. I just added an
if(isset
and added concatenation to the variables in the body to separate PHP from HTML.Provided your sendmail system works, your code must be modified as follows:
This enables you to send HTML-based emails.
Of notable interest:
Note:
<<<EOB
syntax requires the last EOB marker begins as the beginning pf the line and has no space or whatever character after the semicolon.Try this
For those who do not want to use external mailers and want to mail() on a dedicated linux server.
The way, how php mails, is described in
php.ini
in section[mail function]
. Parametersendmail-path
describes how sendmail is called. Default value issendmail -t -i
, so if you get workingsendmail -t -i < message.txt
in linux console - you will be done. You could also addmail.log
to debug and be sure mail() is really called.Different MTA can implement
sendmail
, they just make symbolic link to their binaries on that name. For example, in debian default is postfix. Configure your MTA to send mail and test it from console withsendmail -v -t -i < message.txt
. Filemessage.txt
should contain all headers of a message and a body, destination addres for envelope will be taken fromTo:
header. Example:I prefere to use ssmtp as MTA because it is simple and do not require running daemon with opened ports. ssmtp fits only for sending mail from local host, it also can send authenticated email via your account on a public mail service. Install ssmtp and edit config
/etc/ssmtp/ssmtp.conf
. To be able also to recive local system mail to unix accounts(alerts to root from cron jobs, for example) configure/etc/ssmtp/revaliases
file.Here is my config for my account on Yandex mail:
are you using SMTP configuration for sending your email? try using phpmailer instead. you can download the library from https://github.com/PHPMailer/PHPMailer. i created my email sending this way: