PHP mail function doesn't complete sending of e-mail
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.
Although there are portions of this answer that apply to only to the usage of themail()
function itself, many of these troubleshooting steps can be applied to any PHP mailing system.
There are a variety of reasons your script appears to not be sending emails. It's difficult to diagnose these things unless there is an obvious syntax error. Without one you need to run through the checklist below to find any potential pitfalls you may be encountering.
Error reporting is essential to rooting out bugs in your code and general errors that PHP encounters. Error reporting needs to be enabled to receive these errors. Placing the following code at the top of your PHP files (or in a master configuration file) will enable error reporting.
See this Stack Overflow answer for more details on this.
It may seem silly but a common error is to forget to actually place the mail()
function in your code. Make sure it is there and not commented out.
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
The mail function takes three required parameters and optionally a fourth and fifth one. If your call to mail()
does not have at least three parameters it will fail.
If your call to mail()
does not have the correct parameters in the correct order it will also fail.
Your web server should be logging all attempts to send emails through it. The location of these logs will vary (you may need to ask your server administrator where they are located) but they can commonly be found in a user's root directory under logs
. Inside will be error messages the server reported, if any, related to your attempts to send emails.
Port block is a very common problem which most developers face while integrating their code to deliver emails using SMTP. And, this can be easily traced at the server maillogs (the location of server of mail log can vary from server to server, as explained above). In case you are on a shared hosting server, the ports 25 and 587 remain blocked by default. This block is been purposely done by your hosting provider. This is true even for some of the dedicated servers. When these ports are blocked, try to connect using port 2525. If you find that port is also blocked, then the only solution is to contact your hosting provider to unblock these ports.
Most of the hosting providers block these email ports to protect their network from sending any spam emails.
Use ports 25 or 587 for plain/TLS connections and port 465 for SSL connections. For most users, it is suggested to use port 587 to avoid rate limits set by some hosting providers.
When the error suppression operator @
is prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored. There are circumstances where using this operator is necessary but sending mail is not one of them.
If your code contains @mail(...)
then you may be hiding important error messages that will help you debug this. Remove the @
and see if any errors are reported.
It's only advisable when you check with error_get_last()
right afterwards for concrete failures.
The mail()
function:
Returns TRUE
if the mail was successfully accepted for delivery, FALSE
otherwise. It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
This is important to note because:
So FALSE
will help point you in the right direction whereas TRUE
does not necessarily mean your email was sent successfully. This is important to note!
Many shared webhosts, especially free webhosting providers, either do not allow emails to be sent from their servers or limit the amount that can be sent during any given time period. This is due to their efforts to limit spammers from taking advantage of their cheaper services.
If you think your host has emailing limits or blocks the sending of emails, check their FAQs to see if they list any such limitations. Otherwise, you may need to reach out to their support to verify if there are any restrictions in place around the sending of emails.
Oftentimes, for various reasons, emails sent through PHP (and other server-side programming languages) end up in a recipient's spam folder. Always check there before troubleshooting your code.
To avoid mail sent through PHP from being sent to a recipient's spam folder, there are various things you can do, both in your PHP code and otherwise, to minimize the chances your emails are marked as spam. Good tips from Michiel de Mare include:
See How do you make sure email you send programmatically is not automatically marked as spam? for more on this topic.
Some spam software will reject mail if it is missing common headers such as "From" and "Reply-to":
Invalid headers are just as bad as having no headers. One incorrect character could be all it takes to derail your email. Double-check to make sure your syntax is correct as PHP will not catch these errors for you.
Sometimes the problem is as simple as having an incorrect value for the recipient of the email. This can be due to using an incorrect variable.
Another way to test this is to hard code the recipient value into the mail()
function call:
This can apply to all of the mail()
parameters.
To help rule out email account issues, send your email to multiple email accounts at different email providers. If your emails are not arriving at a user's Gmail account, send the same emails to a Yahoo account, a Hotmail account, and a regular POP3 account (like your ISP-provided email account).
If the emails arrive at all or some of the other email accounts, you know your code is sending emails but it is likely that the email account provider is blocking them for some reason. If the email does not arrive at any email account, the problem is more likely to be related to your code.
If you have set your form method to POST
, make sure you are using $_POST
to look for your form values. If you have set it to GET
or didn't set it at all, make sure you using $_GET
to look for your form values.
Some Web hosting providers do not allow or enable the sending of emails through their servers. The reasons for this may vary but if they have disabled the sending of mail you will need to use an alternative method that uses a third party to send those emails for you.
An email to their technical support (after a trip to their online support or FAQ) should clarify if email capabilities are available on your server.
If you are developing on your local workstation using WAMP, MAMP, or XAMPP, an email server is probably not installed on your workstation. Without one, PHP cannot send mail by default.
You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail.
You can also use SMTP to send your emails. See this great answer from Vikas Dwivedi to learn how to do this.
In addition to your MTA's and PHP's log file, you can enable logging for the mail()
function specifically. It doesn't record the complete SMTP interaction, but at least function call parameters and invocation script.
See http://php.net/manual/en/mail.configuration.php for details. (It's best to enable these options in the php.ini
or .user.ini
or .htaccess
perhaps.)
There are various delivery and spamminess checking services you can utilize to test your MTA/webserver setup. Typically you send a mail probe To: their address, then get a delivery report and more concrete failures or analyzations later:
PHP's built in mail()
function is handy and often gets the job done but it has its shortcomings. Fortunately there are alternatives that offer more power and flexibility including handling a lot of the issues outlined above:
All of which can be combined with a professional SMTP server/service provider. (Because typical 08/15 shared webhosting plans are hit or miss when it comes to email setup/configurability.)
Add mail header in mail function
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:
it worked for me on 000webhost by doing the following :
Enter directly the email address when sending the email
Use ''
and not ""
This code works but the email was received with half an hour lag
Just add some headers before sending mail:
And one more thing. The mail()
function is not working in localhost. Upload your code to a server and try.
Mostly the mail()
function is disabled in shared hosting.
A better option is to use SMTP. The best option would be Gmail or SendGrid.
SMTPconfig.php
SMTPmail.php
contact_email.php
If you only use the mail()
function, you need to complete the config file.
You need to open the mail expansion, and set the SMTP smtp_port
and so on, and most important, your username and your password. Without that, mail cannot be sent. Also, you can use PHPMail class to send.
Try these two thigs separately and together:
You can use config email by codeigniter, example using smtp (simple way) :
It's works for me!
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 use mail
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.
If you're using SMTP (i.e. you're calling isSMTP()
), you can get a detailed transcript of the SMTP conversation using the SMTPDebug
property.
Set this option by including a line like this in your script:
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.
Try this
Try this
Make sure you have Sendmail installed in your server.
If you have checked your code and verified that there is nothing wrong there, go to /var/mail and check whether that folder is empty.
If it is empty, you will need to do a:
if you are on an Ubuntu server.
If you're having trouble sending mails with PHP, consider an alternative like PHPMailer or SwiftMailer.
I usually use SwiftMailer whenever I need to send mails with PHP.
See the official documentation for more info on how to use SwiftMailer.
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]
.
Parameter sendmail-path
describes how sendmail is called. Default value is sendmail -t -i
, so if you get working sendmail -t -i < message.txt
in linux console - you will be done. You could also add mail.log
to debug and be sure mail() is really called.
Different MTA can implement sendmail
. For example, in debian default is postfix. Configure your MTA to send mail and test it from console with sendmail -v -t -i < message.txt
. File message.txt
should contain all headers of a message and a body, destination addres for envelope will be taken from To:
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(from cron jobs, for example) configure /etc/ssmtp/revaliases
file.
Here is my config for my account on Yandex mail:
Maybe the problem is the configuration of the mail server, to avoid this type of problems or you do not have to worry about the mail server problem, I recommend you use PHPMailer, it is a plugin that has everything necessary to send mail, the only thing you have to take into account is to have the SMTP port (Port: 25 and 465), enabled
First of all,
You might have to many parameters for the mail() function...
You are able to have 5 max. mail(to,subject,message,headers,parameters);
As far as the $from
variable goes, that should automatically come from your webhost if your using linux cPanel. It automatically comes from your cPanel username and ip address.
Also make sure you have the correct order of variables in your mail() function.
the mail($to,$subject,$message,etc.)
in that order, or else there is a chance of it not working.
Let me know if this helps...
This will only affect a small handful of users, but I'd like it documented for that small handful. This member of that small handful spent 6 hours troubleshooting a working PHP mail script because of this issue.
If you're going to a university that runs XAMPP from www.AceITLab.com, you should know what our professor didn't tell us: The AceITLab firewall (not the Windows firewall) blocks MercuryMail in XAMPP. You'll have to use an alternative mail client, pear is working for us. You'll have to send to a Gmail account with low security settings.
Yes, I know, this is totally useless for real world email. However, from what I've seen, academic settings and the real world often have precious little in common.
You can use empty() and isset() functions. If you want to make it work with different files, just modify the action='yourphp.php'
to the html I'm giving you, and store the PHP script
to that yourphp.php
file. Also you need to change your index.html
into index.php
to activate PHP functionality.
PHP
HTML
Best Regards!
If you are running this code on a local server (i.e your computer for development purposes) it wont send the email to the recipient. What will happen is, it will create a .txt
file in a folder named mailoutput
.
In the case if you are using a free hosing service like 000webhost
or hostinger
, those service providers disable the mail()
function to prevent unintended uses of email spoofing, spamming etc. I prefer you to contact them to see whether they support this feature.
If you are sure that the service provider supports the mail() function, you can check this PHP manual for further reference,
PHP mail()
To check weather your hosting service support the mail() function, try running this code, (Remember to change the recipient email address)
Hope this helped.
You can use libmail: http://lwest.free.fr/doc/php/lib/index.php3?page=mail&lang=en
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?