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/
Related
Below is my php code to send email.
I have configured xampp for squirrel mail and hmail server.
am able to send mails locally but my php progam is working but am unable to receive mails.
how can i configure my smtp,pop and imap in xampp/hmail server.
<?php
require_once "Mail.php";
$to = 'admin#hatsoff.com';
$subject = 'Customer_Details Report';
$msg="php mail";
$headers = 'From: abd#abd.com' . "\r\n" ;
$result=mail($to, $subject, $msg, $headers);
if($result)
{
print 'mail sent';
}
else
{
print 'mail not sent';
}
?>
Unless you need live emails you could consider a local SMTP mail catching program such as :
http://download-codeplex.sec.s-msft.com/Download/Release?ProjectName=smtp4dev&DownloadId=269147
I ended up using it to test out my sent emails etc, much easier than configuring mail servers and the such, especially if you jump around on different computers.
Please have a look at the official documentation:
http://www.php.net/manual/en/mail.configuration.php
You will need to edit the php.ini file of your PHP runtime.
Also you should think about possible security issues, spam prevention like using captchas etc.
go through this link :
http://www.php.net/manual/en/mail.configuration.php
and also edit php.ini file.
i think this is work fine for you.
I found a php-script on internet:
<?php
$to = "*******#yahoo.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "****#*****.nu";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
and it work perfectly if I use it from my web-hotel. But i need it to by run on my home-server and I just simply not receiving anything. I tried to run it with php -f phpTest, and through both local ip (10.0.0.2) and by using a global hostname. I just get "Mail send" without getting the mail.
It is a ubuntu-server I am running at and it uses apache2. Like I said, it is no wrong in script because it worked on web-hotel. It should be something with my local settings, but I can't figure out what.
All help would be appreciated!
\\demon
The PHP mail function uses a local MTA (Mail Transport Agent), that is a mail deamon running on the same machine as your PHP interpreter and your Apache web server.
So you must install and configure one on your Ubuntu setup. sendmail is the classic one, but exim is said to be easily configurable, and can use your current SMTP server as a smarthost relay, which works well for a home server.
sudo apt-get install exim4-daemon-light
sudo dpkg-reconfigure exim4-config
Instead of relying on the builtin mail function which needs a working MTA, you could use a library like Swiftmailer to send the mail through an existing SMTP server (e.g. your regular mail address). That way you have the additional bonus of less spam filtering (because spam filters are sometimes very suspicious of mails that were sent from home servers).
In an attempt to send mails from a php script, I am testing the mail() function with this code:
<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);
$to = 'example#gmail.com';
$message = 'test';
$from = 'test#gmail.com';
$subject = 'Test';
$headers = "From: $from\n";
mail($to, $subject, $message, $headers) or die ("Message not sent");
?>
This is not the only code I have tried it with, but nothing seems to be working. One of the reasons I am inquiring here is because it returns no errors whatsoever. I get no indication of any error at all, except that the mail never reaches it's destination.
Things that I have done to try and fix the problem:
Changed the SMTP in php.ini from localhost to gmail's smtp
Checked spam filters and outboxes on both ends
Tried various methods of error reporting
Tried several different functions for sending mail
Checked seemingly related things in phpinfo() and looked them up to no avail
I think it may be an issue with the sendmail function in my terminal, but I have no way to test or fix any issues related to the sendmail function.
I am running apache 2.0 on a PPC Mac with OS X 10.4 Tiger and PHP 5.3.12.
The SMTP settings in php.ini is for Windows only. On other systems the local MTA (Sendmail) is used, always, and the SMTP settings you provide is silently ignored. If you want to use Gmail, make sure that you configure it in your MTA.
Check the mail logs from Sendmail on the sending system. You should be able to find hints about message delivery in there.
I am new to PHP and I'm using the mail function to send emails which is not working. I get a success message, but still it does not work
same code
<?php
$email_to = "abc#abc.com";
$email_subject = "Test mail";
$email_body = "Hello! This is a simple email message.";
if(mail($email_to, $email_subject, $email_body)){
echo "The email($email_subject) was successfully sent.";
} else {
echo "The email($email_subject) was NOT sent.";
}
?>
Am I missing anything, do I need to include any files for this function.. I am from asp.net & this is the basic script which found on website.
I tried other scripts related to mail they didn't work either..
I AM RUNNING THIS SCRIPT ON THE WEBSITE NOT on the localhost
If you are using Ubuntu and it seem sendmail is not in /usr/sbin/sendmail, install sendmail using the terminal with this command:
sudo apt-get install sendmail
and then run reload the PHP page where mail() is written. Also check your spam folder.
This is probably a configuration error. If you insist on using PHP mail function, you will have to edit php.ini.
If you are looking for an easier and more versatile option (in my opinion), you should use PHPMailer.
This might be the issue of your SMTP config in your php.ini file.
Since you new to PHP, You can find php.ini file in your root directory of PHP installation folder and check for SMTP = and smtp_port= and change the value to
SMTP = your mail server e.g) mail.yourdomain.com
smtp_port = 25(check your admin for original port)
In case your server require authentication for sending mail, use PEAR mail function.
"Just because you send an email doesn't mean it will arrive."
Sending mail is Serious Business - e.g. the domain you're using as your "From:" address may be configured to reject e-mails from your webserver. For a longer overview (and some tips what to check), see http://www.codinghorror.com/blog/2010/04/so-youd-like-to-send-some-email-through-code.html
The mail function do not guarantee the actual delivery of mail. All it do is to pass the message to external program (usually sendmail). You need a properly configured SMTP server in order for this to work. Also keep in mind it does not support SMTP authentication. You may check out the PEAR::Mail library of SwiftMailer, both of them give you more options.
Check your SMTP settings in your php.ini file. Your host should have some documentation about what credentials to use. Perhaps you can check your error log file, it might have more information available.
For HostGator, you need to use the following for your headers:
$headers = 'From: user#yourdomain.com' . " " .
'Reply-To: user#yourdomain.com' . " " .
'X-Mailer: PHP/' . phpversion();
It only worked for me when the from user was host e-mail while the Reply-To can be something different e.g. From: owner#domain.com, Reply-To: info#domain.com
http://support.hostgator.com/articles/specialized-help/technical/php-email-from-header
http://support.hostgator.com/articles/specialized-help/technical/how-to-use-sendmail-with-php
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.