I\'m having issues sending emails using the php mail() function. I know the php script I have works because I have an identical copy of it on another web-hosting company and it
Are you performing any kind of checks on the mail function? It should return true if it's executing successfully - knowing that would help us cut down on other possible reasons you may not be receiving the mail, such as filters, server or smtp configuration etc. Doing something like:
if (mail($to, $subject, $body, $header)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
Should give you a better idea, and should die outright if the function does not exist for some reason. Php's mail function is incredibly finicky on free web hosts, since it's commonly abused for spam purposes.
Posting full headers also can help legitimate messages pass spam tests.
$headers = "Return-path: <sendingemail@test.com>\n";
$headers .= "Reply-to: <sendingemail@test.com>"."\n";
$headers .= "Content-Type: text/html; charset=windows-1252\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";
$headers .= "From: <sendingemail@test.com>\n";
$headers .= "X-Priority: 3\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Organization: My Organization\r\n";
$headers .= "\n\n";
Write a really simple script, like
<?php
mail('your_mail@example.com', 'test subject', 'test msg') or die('no mail()');
echo 'mail sent.';
Execute it, and make sure the mail is not caught by your spam filter (if you can afford it, set up your own domain/DNS server, netcat -l -p 25
is sufficient).
If that doesn't work, contact the support of your web hoster. Do they have an FAQ or any other documentation?
mail() function of php, will send your mail to junk only. Instead use SMTP php mailer function.
Why we should use SMTP instead PHP mail():
SMTP log in to an actual account on a mailserver and send the mail through SMTP to another mail server. If the mail server is configured correctly, your mails are sent from an actual account on a mailserver and will not wind up flagged as spam.
Mail sent with the mail() function is sent with sendmail in most cases. There is no authentication going on and it will almost always be flagged as spam if you use the "From:" in the extra headers.
This is because if you take a look at an original email file in say, gmail, you will see the headers that are sent. You are actually sending from user@serverhostname.tld and not someone@example.com like you had told the mail function to do. If you use SMTP and view the original the email is actually sent from someone@example.com
You can download SMTP class from:
Whatever the solution, check your mail()'s output.
Most common solution
Ask your hosting company if your current web host has SMTP set up to relay mail from your scripts. If they say "no", then they might have another SMTP host for you to use like smtp.example.com, or you'll have to use another SMTP relay (check with your current e-mail provider).
Alternative
The SMTP server you're talking to might not understand what your script is saying. I've seen situations before where my mail script will work with Postfix but not qmail. This is easily solved by using a third party e-mail library: there are tons out there, but my favorite is Flourish's (http://flourishlib.com/docs/fEmail).
If it is a dedicated server, make sure you have postFix Mail installed (http://www.postfix.org/)
I faced this error today itself as the SMTP server was not available (i assumed it as there by default, but not)