This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I want to script a simple registration form with activation mail and so on. But for some reason mail() doesn't send the emails, or my 3 different email accounts (hotmail,gmail,yahoo) don't receive them and therefore don't even put them in the spam folder.
Code:
<?php
$mailto = 'xxx#example.com';
$subject = 'the subject';
$message = 'the message';
$from = 'system#example.net';
$header = 'From:'.$from;
if(mail($mailto,$subject,$message,$header)) {
echo 'Email on the way';
}
?>
Everytime it outputs 'Email on the way' so mail() returns true, right? I really don't get it, I've even tried to turn off my little snitch (although I didn't block SMTP).
See this article by Jeff Atwood.
In short: Just because your code has handed the e-mail to a Mail Transfer Agent, it doesn't mean it will be delivered. Yes, mail() returning true means "accepted for delivery" - which means "Looks like an e-mail, I'll try to deliver this", not "It is delivered". Even the manual for mail() says:
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.
Soooo: check your MTA (is the e-mail sent from your local computer?), try to send to a local address (if the address is local, does it get delivered?), try to send an e-mail from your mail client, using the same settings as your PHP script, try to send to a smaller mail-hoster which allows you tu switch off antispam (is it delivered outside your network?). Also, read that article, and check the points mentioned there.
Maybe your server is not configured to handle mail().
<?php
print phpinfo();
?>
and look at sendmail_path
You may need to add correct end of line characters to the Headers. It may be \n or \r\n
Check your phpinfo and/or php.ini for your mail settings and make sure you can send mail with whatever program php is trying to use. The function will succeed if the command executes but doesn't know if the mail actually went out.
Check your mail server's mail log. On Unix-ish systems, it's generally /var/log/maillog. On Windows, who knows, but there should be a log somewhere. If mail is returning TRUE, then whatever mail server it's connecting to has accepted the mail for eventual delivery. After that, mail() is no longer involved in any way and it's up to the SMTP servers to do the actual delivery.
In real world terms, mail() is you walking a letter down the block and dropping it into a mail box. Everything after that is utterly outside of PHP's scope and control.
If this is a linux server it's probably set up to send to the local mail queue. When I had this problem I got it working by adding an MX entry on the DNS server used by the linux servers which pointed to our ISP's mail server.
I had the same problem on Ubuntu and I resolved it following the next tutorial:
http://www.nixtutor.com/linux/send-mail-with-gmail-and-ssmtp/
I hope it works for you also.
Related
This question already has answers here:
How to configure XAMPP to send mail from localhost?
(11 answers)
Closed 2 years ago.
I want to send e-mail from a php file (Windows 10, localhost, XAMPP).
I followed this tutorial: Link
My php.ini file looks like this:
[mail function]
; For Win32 only.
SMTP = smtp.secureserver.net
; For Win32 only.
sendmail_from = some.email#gmail.com
My php file contains these:
$to = "another.email#gmail.com";
$subject = "Subject";
$mesaj = "Message";
$headers = "From:some.email#gmail.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
mail($to, $subject, $message, $headers);
When running, this warning appears:
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\store\email_sender.php on line 61 No e-mail sent.
I saw similar questions (Link_1, Link_2, Link_3).
But I don't understand what I have to do. I have read that I need to install a SMTP server. What server should I install?
I have also followed this example (sending e-mail from mail function php), but the warning is still there and no e-mail is sent.
EDIT:
I have modified the information provided into the php.ini file:
[mail function]
SMTP=smtp.gmail.com
smtp_port=587
smtp_port=465
sendmail_from = some.mail#gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
I have also modified the information provided into the sendmail.ini file:
smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=auto
error_logfile=error.log
;debug_logfile=debug.log
auth_username=some.mail#gmail.com
auth_password=somepassword
force_sender=some.mail#gmail.com
I have to mention that, in Gmail, the 2-step verification is disabled and the access to less secure apps is enabled. I have also stopped and started the Apache server.
All of the help links that you included in your question are correct. What I hear you saying, is that you don't fully comprehend what those links are telling you to do. Let me see if I can help you understand what is necessary to accomplish what you are trying to do.
When you send an email message from any program that you create, whether you're writing code in PHP, C++, Java ... makes no difference, the underlying libraries from your programming language do fully understand how to send an email. But you can only send an email using an email server that is actively working on the Internet, and one with which you have an account that has permission to send an email.
If email servers just let anyone send email through them, you can imagine how much worse spam would be on this planet.
Installing an SMTP server on your local machine won't solve your problem either, because you would need to have a subdomain that you control (whateverwhatever.com) and you would need to create MX records in a publically visible DNS server. You could buy a domain name with GoDaddy, then create your MX records and point them to your IP address, etc. but that's a lot of work.
What I suggest you do, is if you have a GMAIL account, you can use a Gmail server to send your email through, but you will need to configure your PHP code (either using ini_set() commands or in your php.ini file under the [mail function] heading with the information that the Gmail servers require.
Here are the fairly common pieces of information that most SMTP servers require, which you must define in your code or the php.ini file:
SMTP Server address (smtp.gmail.com)
Your Gmail account name
Your Gmail account password
The port numbers that the Gmail server requires
And there may be other pieces of info that it needs to see before it allows you to send the email.
Take a look at this page which explains how to use your own Gmail account to send an email for free. Also, do some Google searches using phrases like 'how to send SMTP through Gmail using my personal account' ... the information is out there.
Once you have learned what the Gmail servers require in order to send SMTP email, you simply input all of those required pieces of information into your PHP code or in the php.ini file. And there is plenty of documentation out there on how to do that.
Further discussion:
A little more clarification on what you're actually doing: ... you need to understand that your PHP program that you are writing is - for lack of a better term - en ad-hoc email client. You are use to sending email either with Outlook, or a web interface or some other email client, and you just write the email, put in the address of the person that you are sending to and you just click send and it goes ... but now, you're writing software to do the portion of the email sending that happens after you click send from an email program ... the part of the email process that you never have to think about ... you are now needing to create with your code. So your code needs to know where to place that email message, and email servers will not accept an email message from any place without proper credentials.
So you're basically writing with PHP code, a very light version of an email client that needs to be taught how to send an email... which is all the stuff that happens after you click SEND when you send an email to your mom.... you have never needed to know what happens to your email after you click send until now ... because you're hard coding the process literally in your PHP code.
I hope that helps you understand what's happening here a little better than you did.
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
I am running into a problem with zend mail sending functionality.
I have a functionality where we have set up some cron jobs and those cron jobs processes some php script and then sends a mail. Actually mails are going fine, but sometimes it is getting dropped into spam directory. After some research I have found that the return-path of mail body is causing problem. Since the mail sending script us as a root, so the return-path is root#domain.com and I want change it to support#domain.com
Is there any way I can achieve that.
Note: I did try to add that in headers, but it is not working.
It's return-path not reply-to... There's no such thing as reply-path :)
There are lots of parameters for being marked as spam and I'm not sure it's because of return-path only. You have to fix it though and you can try by altering the headers while sending:
$mail = new Zend_Mail();
$mail->addTo($this->email, $this->name)
->setFrom($message->from_email, $message->from_name)
->setSubject($message->subject)
->setBodyHtml($message->getHtmlEmailContent($subscriber))
->setBodyText($message->getTextEmailContent($subscriber))
->setReturnPath($settings->get('return_path'))
However SMTP servers might override this (gmail definitely does). Just open the email in raw and see if your header is there and if it's overriden or not.
If mails are marked as spam in your test account randomly you might want to check the contents and subject of your email. SPF records for your SMTP domain is important too.
See https://www.campaignmonitor.com/blog/post/1971/what-are-some-good-methods-for
and http://mailchimp.com/resources/guides/how-to-avoid-spam-filters/ for some details.
See this answer for the explanation of reply-to and return-path.
A few days ago, i can send email via mail() function on my server. But now, mails doens't sending . There isn't anything at error log. I don't get any error. I don't know, why emails not sending.
What can i do? How can i solve this?
EDIT
Now i get all emails (about 18 mails :) ) . Why mails delayed?
PHP's mailer function does not actually deliver the mail. It simply hands it off to an SMTP server for that. Whether or not it actually gets delivered is not considered by mail(), it's just concerned with the hand-off.
If the SMTP server accepts it, then mail() will return true and pretend everything worked fine. The SMTP server may delete the mail, send it to the wrong place, route it through a tangle of other SMTP servers causing the mail to be delayed by a year, etc..., but mail() will still have said everything's fine.
The place to look for the cause of this is your mail server's own log. If there was a delivery problem, there'd be a list of each delivery attempt in there somewhere.
Have you tried doing this?
The problem seems to be that PHP use
the ini directive sendmail_from to set
the from email address in the SMTP
protocol. If this is not correctly
set, or if it does not match the from
header in the email headers, the email
is caught by spam protection software.
The simplest solution is to set the
directive during execution:
ini_set("sendmail_from", $email_from);
$headers = "From: $email_from";
mail($to, $subject, $message,
$headers);
Ok, now i have got 18 mails together. I don't know, why mails are delaying.
Maybe your receiving mailserver implemented greylisting (http://en.wikipedia.org/wiki/Greylisting) ...
Do you have access to the mailqueue or maillog of your webserver? In that case you can search for delayed or bounced messages there.
I have currently the following and it's not working. (normally I have a body text and header information, but nothing works)
mail("idealvisions#live.com","Nieuw offerte aanvraag","test");
We'll on my server it did because I'm running php 5 or higher, no problem there. But the contact page is from a client which runs php 4.4 I believe. Now the client is really pushing me to get this fixed but it won't seem to fix. I've tried everything
What's the problem here? is it the php version?
I checked also with phpinfo and have this
sendmail_from no value no value
sendmail_path /usr/local/bin/bgc_webhosting_mailer.sh /usr/local/bin/bgc_webhosting_mailer.sh
This sounds like the Client's server is not set up properly with a mail-sending handler
some ISP's that provide hosting and some free hosting platforms disable the mail function, can you try something like this:
error_reporting(E_ALL);
if (mail ('you#yourdomain.com', 'Test subject', 'Test Body')){
echo 'Mail sent';
}else{
echo 'Mail not sent';
}
if there is an issue with mail() on their server you could consider posting to a mail form on one of your servers and then bouncing them back to a thankyou page on the client's server after.
From the sendmail path, your host uses a custom sendmail program/shell script.
Since the sendmail from isn't set, you have to set it within the mail() function, unless it is hardcoded into /usr/local/bin/bgc_webhosting_mailer.sh in which case you should ask the hosting.
Example setting sender:
mail('recepient#example.com', 'Test subject', 'Test Body', "from:sender#example.com\n");
If the mail() function really is NOT available, then ask your hosting about an alternative, such as their local SMTP server.
You can then use the local SMTP server to send email. Probably the easiest way to do this is donwload an email library that supports sending via SMTP such as PEAR Mail or SwiftMailer for example.
Pear: http://pear.php.net/package/Mail
Swift: http://swiftmailer.org/
If your host does not provide an SMTP server, you should probably look for a different host. However, you can always use a free email service such as Gmail, or Yahoo and send mail through their SMTP.
Also check SeLinux, it can prevent any thread spawned by Apache to send mail. In that case you get no error and no mail.