This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Sending mail in php
(5 answers)
Closed 6 years ago.
I am using the following code. No error is coming, but mail is not received.
<?php
$msg="";
$from_add = "example#gmail.com";
$to_add = "example#gmail.com";
$subject = "Test Subject";
$message = "Test Message";
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";
if(mail($to_add,$subject,$message,$headers))
{
echo $msg = "Mail sent OK";
}
else
{
echo $msg = "Error sending email!";
}
?>
It's difficult to diagnose why your script not be sending emails unless there is an obvious syntax error.
So you need to troubleshooting this problem as the following to find any potential pitfalls you may be encountering:
1- Make sure error reporting is enabled and set to report all errors.
Placing the following code at the top of your PHP files (or in a master configuration file) will enable error reporting.
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
2- Check your server's mail logs.
(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.
3- Check to see if mail() returns true or false.
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:
If you receive a FALSE return value you know the error lies with your server accepting your mail. This probably isn't a coding issue but a server configuration issue. You need to speak to your system administrator to find out why this is happening.
If your receive a TRUE return value it does not mean your email will definitely be sent. It just means the email was sent to its respective handler on the server successfully by PHP. There are still more points of failure outside of PHP's control that can cause the email to not be sent.
So FALSE will help point you in the right direction whereas TRUE does not necessarily mean your email was sent successfully.
4- Check your spam folders / Prevent your email from being flagged as spam.
5- Make sure you supply mail headers.
$header = "From: noreply#example.com\r\n";
$header.= "MIME-Version: 1.0\r\n";
$header.= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$header.= "X-Priority: 1\r\n";
6- end to multiple accounts.
To help rule out email account issues.
7- Consider using an alternative mailer.
PHP's built in mail() function is handy and often gets the job done. But there are alternatives that offer more power and flexibility including handling a lot of the issues outlined above. One that would might consider using is the popular PHPMailer
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I use mail() to send emails but the output says it is success but i do not received any emails. can someone help me please in using mail()?
function send_mail() {
$message = 'Please reset your passwrod etc.';
if (mail('ruedastefano#gmail.com', 'password reset', $message, 'from: bjmpncr#thefourpobu.com')) {
echo 'success';
} else {
echo 'failed';
}
}
Its because your mail config not set correctly.
Check
Environment(if local check this / if live-host most of time it will config automaticaly)
User Authentication(Username / password)
Sample code
$to = "abdulla#stackoverflow.com";
$subject = 'Place Order From ';
$message = 'My message goes here';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: ibjmpncr#thefourpobu.com";
mail($to, $subject, $message, $headers);
Requirements for php mail() function
php.net
For the Mail functions to be available, PHP must have access to the sendmail binary on your system during compile time. If you use another mail program, such as qmail or postfix, be sure to use the appropriate sendmail wrappers that come with them. PHP will first look for sendmail in your PATH, and then in the following: /usr/bin:/usr/sbin:/usr/etc:/etc:/usr/ucblib:/usr/lib. It's highly recommended to have sendmail available from your PATH. Also, the user that compiled PHP must have permission to access the sendmail binary.
w3Schools
For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I have these exact same PHP contact form codes on 2 different servers.
1 is working as intended, but the other is returning the error message instead of the success message. Also, email is not sent to me.
Contact.php codes as follows:
<?php
ob_start();
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$to="email#gmail.com";
$subject="Enquiry";
// $headers = 'MIME-Version: 1.0' . "\r\n";
//$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n";
$message="
Name:
$name
Email:
$email
Message:
$query
";
if(mail($to,$subject,$message,$headers))
header("Location: http://www.website.com/contact.php?msg=Successful Submission! Thank you for contacting us.");
else
header("Location: http://www.website.com/contact.php?msg=Error Sending Email!");
}
?>
I'm wondering if it is permissions/server problem?
Any help will be greatly appreciated! Thanks!
Your code construct is correct, and the return value (which is your custom error) might be correct "if" the mail function is failing.
A lot of time we find php mail() function is not working. In some cases, the email is not sent even the function returns true. Following are the points you may check while debugging:
First check if the sendmail_path variable is set properly. You can
see it from echo – ing phpinfo() in a page. The typical value is
/usr/sbin/sendmail -t -i
If you are sending extra headers in the 4th parameter of the
function, try to remove it and send. It is a very common scenario to
have incorrect header in the mail function.
Check if you have a spam filter installed in your server which is
blocking your email.
If you have access check your mail log file to see if the email is blocked by the mail server or have not reached the server yet. If there is no entry of your email in the log, it means you have problem in your php code or php configuration. If it is in the log, you can see why it was not delivered.
The typical path to mail log file is /var/log/maillog
This question already has answers here:
Mail go to spam instead of inbox and some errors in header part of mail using PHP
(2 answers)
Closed 8 years ago.
I am trying for send mail using php.But got these warning message in mail. 'This message may not have been sent by: zamisoft#gmail.com Learn more Report phishing.
My header is
$header_mail="select content from mail_header where id='1'";
$header_mail2=mysql_query($header_mail);
$fet=mysql_fetch_array($header_mail2);
$content= $fet['content'];
$Headers = "From:$content\r\n" .
"Reply-To:$content\r\n" .
"Content-type: text/html; charset=UTF-8 \r\n";
$Headers.= "MIME-version: 1.0\n";
$Headers .= 'X-Mailer: PHP/' . phpversion()."\r\n";
Content in $content variable is zamisoft<zamisoft#gmail.com>
Any body help me for solve these issue?
I'd already answered the same on your previous post but it is more relevant here.
The problem is simple that the PHP mail() function is not using a well configured SMTP Server.
Nowadays Email-Clients and Servers perform massive checks on the emails sending server, like Reverse-DNS-Lookups, Graylisting and whatevs. All this tests will fail with the php mail() function. If you are using a dynamic ip, its even worse.
Use the PHPMailer-Class and configure it to use smtp-auth along with a well configured, dedicated SMTP Server (either a local one, or a remote one) and your problems are gone.
https://github.com/PHPMailer/PHPMailer
I am using the following code to try and send an email - I can't see any problem with it - but it's not working, it displays the message "email sent" but I don't receive anything.
<?php
$to = "email#address.com";
$subject = "Query";
$message_body.="Name: ".$_POST["name"]."<br>\n";
$message_body.="JobTitle: ".$_POST["jobtitle"]."<br>\n";
$message_body.="Phone: ".$_POST["phone"]."<br>\n";
$message_body.="Email: ".$_POST["email"]."<br>\n";
$header = "Reply-To: ".$_POST["email"]."\n";
$header .= "Content-type: text/html;
mail($to, $subject, $message_body, $header);
echo "Email sent";
?>
Can anyone help?
Check 1) For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file.
You mentioned that there are no errors.. so next..
Check 2) Check the return value of function.. TRUE or FALSE
Check 3) See if any warning by enabling error_reporting(E_ALL)
Check 4) Actually mail sent, but went to Spam folder.
For mail() , 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.
Also there is a difference in a way how it works in Windows and Unix.
Your example is missing a " in the last $header line
try adding:
error_reporting(E_ALL);
ini_set("display_errors","On");
at the beginning of the file to show the errors / warnings you get. Most likely sendmail is not running on your server
Try to check your SMTP server configuration.
http://php.net/manual/en/mail.configuration.php
Probably it blocks your mail.
$subjt = $subject;
$message = $message;
$toinfo .= $snteadd[$i];
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: <$fromemailid>\r\n";
$headers .= "Cc: <$sendCC>\r\n";
$headers .= "Bcc: <$sendBCC>\r\n";
$headers .= "Return-Path: <$fromemailid>\r\n";
$headers .= "Errors-To: <$fromemailid>\r\n";
$headers .= "X-Mailer: PHP 4.x". phpversion()
#mail($snteadd[$i], $subjt, $message, $headers);
Return Values
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.
Then you can do:
if (#mail($snteadd[$i], $subjt, $message, $headers))
{
echo 'delivered to smtp';
}
else
{
echo 'not delivered to smtp';
}
If you want check mail reach the your sender, then probably better solution will be get link to your script with unique arguments to determinate mail id, and get information in mail text to click link.
The mail function returns true on success or false on failure.
if (!#mail($snteadd[$i], $subjt, $message, $headers))
{
// It failed...
See http://php.net/manual/en/function.mail.php
Check the return value of mail function which returns false when there it fails.
if(#mail($snteadd[$i], $subjt, $message, $headers) === false) {
// mail failed.
}
Just to expand on previous answers, the # is used to explicitly suppress error messages from the following function being displayed - so what your code does is send an email and DON'T TELL ME if it it fails... This is usually only good if you either a) don't care or b) are checking that it worked yourself - eg with an if block as shown elsewhere
While the mail() function returns true on success and false on failure, the status it reports only means "accepted for delivery by the local server", absolutely not "delivered".
The SMTP protocol used for most email guarantees nothing. It should try to deliver the e-mail, sometime; again, there's no guarantee that it will be in ten minutes, tomorrow, or this month (it happens). Even worse, there is no reliable way to tell whether a mail you've sent has actually been delivered to the intended recipient.
Even if the e-mail is delivered to the recipient, it may get shuffled to the spam bin, and the user won't see it.
In other words, the only thing that you can be sure of with e-mail is whether or not you've sent it; anything beyond that is uncertain. (see also this for other possible pitfalls)
In case someone else is wondering why the "Return-Path" header they are setting is not being honored, many php settings replace that header with something else, usually a default email address for the server (eg., something#webserver.domain.com). Use the (often overlooked) 5th parameter, $additional_parameters in this way:
mail($toMail, $subject, $message, $other_headers, "-fyour_return_path_email_address#whatever.com")
The your_return_path_email_address#whatever.com is the email you want the return path to be. The -f part that precedes this makes the "envelope sender" that email address.