All,
I have the standard mail code to send an email in PHP.
$to = $resultset['email_address'];
$subject = "New client inquiry from Website.com";
$message = $email_message;
$from = $your_email;
$headers = "From:" . $from;
mail($to,$subject,$message,$headers,"-f $from");
The variables are created earlier in my code. However what I want to check is to make sure that the mail function was executed successfully. With this code, how can I determine if sent the email and if it didn't echo out "failure"?
Simply said: This is not possible with the PHP mail() command.
the return value from mail() just indicates, whether the mail was successfully handed over to the MTA, but not if it was sent. If e.g. your MTA is postfix, and the postfix service is stopped, mail() will happily return true, as queueing the mail to postfix worked. It will however never be sent, if postfix is not manually started (or even correctly configured).
If you really want to make sure, the mail has been sent, you need to talk to a MTA via sockets. There are frameworks for that.
Related
I stumbled on the following script today for sending an e-mail using PHPMail.
<?php
$to = "some_address#domain.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "my_address#domain.com";
$headers = "From:" . $from;
mail($to, $subject, $message, $headers);
echo "Mail Sent.";
?>
Above can be runnable through php mail.php and instantly you'll get an e-mail sent to $to from $from despite not needing to set outgoing/ingoing servers out.
It really intrigued me, since my CMS uses an SMTP outgoing server (well, same way Mail PHP does), which I need to set up with my Outlook SMTP username and password - some sort of verification.
However, about Mail PHP just.. sends an e-mail. To the address you set it as. From the address you set it as.
Looking at PHP docs it does not really reveal how it works. Does Mail PHP not have any issues with spamming since anyone can send anyone anything anytime programmatically without verification of the from identity?
EDIT:
It's rather funny the people in the comments were talking about the POTUS, since I had the exact thing in mind:
It did land in my junk folder, but I'm sure it isn't hard to make this look convincing enough and still be considered "oh damn spam filter lost my e-mail!"
The mail function uses the settings from php.ini. The details of this configuration can be found in Mail Runtime Configuration.
The defaults can be set in php.ini, although you can override them using ini_set.
I bet you sent the mail from a PHP script on a hosted server. That server probably has SMTP settings configured beforehand. If you would try this locally on a WAMP/LAMP server, you would have to do this configuration yourself, since PHP cannot read your Outlook/WhateverMailclient settings.
As stated in the comments, you can specify the sender/from address yourself. SMTP doesn't require this to be the actual sender domain, so that's why this works. The missing link is the pre-configured SMTP server of your host.
Some relay servers do check for this, and your mail might be blocked or sent to a junk mail folder. You can however configure this in your DNS to indicate that <Your server's IP> is indeed allowed to send email for <yourdomain>. For more information about that subject, you might want to read this question on ServerFault.
It uses the smtp protocol or send_mail, you can even configure what php should use to send mails in php.ini. It can send e-mail but the e-mail will end-up in your spam filter take a look to DKIM and SPF records for more information
Till yesterday below code was working. not sure, today below code is not working. I am not getting any email. Though, I am getting echo as right, but I am not getting any email.
$to = "yyyyy#yahoo.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "xxxxx#example.com";
$headers = "From:" . $from;
if (mail($to, $subject, $message, $headers)) {
echo "right";
} else {
echo "wrong";
}
Any reason why this is happening?
I added print phpinfo(); and check for sendmail_path. I found below.
sendmail_path /usr/sbin/sendmail -t -i
Answer
It was server problem. Hence email was not getting sent.
From what i understand the email is sent from the PHP mail() function. This function uses the local MTA to deliver the message.
If you have control over the server and its MTA you can start checking the logfiles of the server. There you should see the email from the PHP mail() function showing up. If this is not the case PHP seems to not pass it on to the MTA (in your case sendmail).
If it shows up in the logs, check the lines for more details. The reason can be in there. If you are not sure, post the log content (do not forget to mask private details of it).
But if it does not show any problems in the logs there could be a couple of other reasons. Like your IP to be blacklisted. To check that visit the following website http://mxtoolbox.com. But it would be possible as well that the receiving email server is bouncing the email back ... which would show up in the servers root inbox where you could see the return reason in it.
I hope that was helpfull for you!?
To fix the problem in Ubuntu, Apache. You have to make sure that you have a mail sending software installed in your computer!
in ubuntu, open terminal and type the following command:
sudo apt-get install sendmail
after that try again!
There is no PHP error here, it is a problem with your server setup. PHP will return true on mail() once the mail has been send off to sendmail, or whatever you are using. It does not know whether the email has actually been sent or not.
Try
$message = "test 1\r\ntest 2\r\ntest 3";
$message = wordwrap($message, 70, "\r\n");
mail(SETYOURMAIL#MAIL.COM', 'My Subject', $message);
if this does not work check the php.ini
http://www.quackit.com/php/tutorial/php_mail_configuration.cfm
Some time mail() function may not work, so you should use PHPMailer, A well written documentation is written here :
rohitashv.wordpress.com/2013/01/19/how-to-send-mail-using-php/
I am developing an application and have been testing the mail() function in PHP. The following works just fine on my local machine to send emails to myself, but as soon as I try to send it from the testing environment to my local machine, it silently fails.
I will still get the "Mail Sent" message, but no message is sent. I turned on the mail logging in the php.ini file, but even that doesn't seem to be populated after I refresh the page.
Again, the .php files and php.ini files are identical in both environments. Port 25 has been opened on the testing environment, and we are using a Microsoft Exchange server.
<?php
$to = "user#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "user#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
SMTP area of the php.ini file:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = exhange.server.org
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = user#example.com
First of all, even when mail fails, the echo "Mail Sent." will be shown. The php function mail() will return true on success and false on failure. Put it in an if and you can check if the mail has been sent:
if(mail($to,$subject,$message,$headers)) echo "Mail Sent.";
Should be working to check if the email was sent or not.
Regarding your problem that it is not working, I am not quite sure and I might be wrong, but some servers as of my experience want the \r\n behind each headerline.
$headers = "From:" . $from . "\r\n";
But as already said, I might be wrong and related to the examples on here, it is not necessary when using one headerline - http://php.net/manual/en/function.mail.php
When I am testing the mail function, I do not put any header information into the mail function, just $to, $subject, $message. You might give it a try. I really hate using the php mail function by myown, I always use a PHP mailer class.
Sorry if I couldn't answer to your real problem, that the email can not be sent. I hope you
Check in your PHP distro for PEAR and Mail.php. On the cmd line, "php -i" to find your resources. I believe PEAR and Mail.php is fairly common for distros over 5.2. I'm on a Mac and Linux server and prefer PEAR mail over the PHP mail function. Windows should be similar. Here is an example of sending multiple emails using PEAR Mail.
/** PEAR::MAIL
* PEAR::Mail only opens one mail socket for multiple emails sent
*/
require_once('/opt/local/lib/php/Mail.php');
$body = $_POST['message'];
//using sendmail on backend
$params['sendmail_path'] = '/usr/sbin/sendmail';
//using factory method
$mail_object =& Mail::factory('sendmail',$params);
//loop through selected users to send
for ($i=0;$i<count($recipients);$i++){
if (!empty($recipients[$i]['email'])&&($recipients[$i]['alt_email'])){
//concatinate email and alt_email
$address = $recipients[$i]['email'].",".$recipients[$i]['alt_email'];
}
else {
//only one user address
$address = $recipients[$i]['email'];
}
//send the mail instance
$mail_object->send($address,$headers,$body);
if (PEAR::isError($mail_object)) {print($mail_object->getMessage());}
} //close the for loop
Some time your hosting service providers are block outgoing SMTP Authentication. Please confirm with you hosting providers.
I have a rackspace cloud where I want to set up LAMP. the server has CentOS.
I have sendmail installed and the php mail function use this, but it takes too long to send an email using the php mail function
<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
$to = "email#somedomain.com"
$subject = "activation code";
$message = "Activation code: 10";
$from = "activate#mywebsite.com ";
$headers = "From: $from";
if(mail($to, $subject, $message, $headers)) {
echo 'success';
}
else { echo 'deny'; }
but this takes a lot of time, and when I ask rackspace about this they said the mail function might be using a public mailserver to send emails and as the queue is too long it takes time. but I have another email server too.
is there anyway I can get this to work fast? and can I make the send mail installation to use that email server I have ?
Try using the PHP Pear Mail package it allows you to send via sendmail, pop, smtp, or imap http://pear.php.net/packages.php?catpid=14&catname=Mail theres also the ability to easily add attachments and queue messages.
I had the same issue on my rackspace cloud and this resolved it.
I am trying to get this simple php mail script to send mail to my email addres (mike_minerva#yahoo.com) and I cannot get it to work. I set my sendmail_path in php.ini to the right folder (/etc/sbin/sendmail) but that did not seem to help. What else could I be missing? The script always returns failure.
<?php
$to = "mike_minerva#yahoo.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
if(mail($to,$subject,$message,$headers))
echo "Mail Sent.";
else
echo "failure";
?>
SwiftMailer is a good library for the purpose of authenticating to your SMTP server to send mail.
http://swiftmailer.org/
try to use PEAR MAIL package.
In case anyone else comes to this question via google, another main cause of php mail not working is that the function is blocked on many servers due to the danger of outgoing spam.
There are some good smtp mail classes out there that are very easy to use. I only use mail() for debugging purposes... almost never in a live environment.