How to check if mail server has been configured or not? - php

I need to make a installation script, in which I need to check whether mail server is configured properly or not.
If not, then the message should insist to enable it.
Can someone please tell how can i check this?
I tried to make a mail script and run it using PHP CLI.
It returns true but never received any mail.

Basically you have no perfect way of checking if the server is configured to send mails correctly.
You can check the php.ini parameters, for example sendmail_path, if these represent some correct values. You can try and send a test mail too, but read the manual concerning return value of the 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.
Meaning if mail() returns true, this does not indicate that the server is configured properly. The mail may be accepted for delivery, but the MTA (mail transfer agent) may have problems actually connecting to the internet, or may crash, or ...
You may want to use a third-party-library, which directly connects to a MTA via SMTP and does not use PHPs internal mail function. This third-party-library may provide a test method to check if this SMTP server is reachable. While this seems to provide more appropriate feedback if it is configured correctly, actually it's limited too. The SMTP server which was provided in this configuration is likely a local one. This one will accept mails to be send, but this does not guarantee that these are sent actually.
This may seem like a dissatisfying answer, but actually the problem is not that serious. Most shared hosters set up their mailing environment correctly. If one has a dedicated server, one can assume that the owner is capable of solving issues with the mail server on ones own.
If you check the sendmail_path and then send a mail (checking the return value) to a test mail address while outputting the information, that one has to check the mail server configuration, if this mail does not arrive correctly, this should be enough.
Please see also my other posting concerning how one could check if mails are actually being sent or not. The information there applies to PHP as well.

Related

PHP - In which cases can an email not be accepted for delivery?

With the following statement we will get a boolean whether the mail we try to send gets accepted for delivery or not:
if(mail($to $subject $message $headers))
Since I always get true even if I am not connected to the internet, I assume that this response has nothing to do with the fact if it gets sent for real or not. So in which cases can I get false? And where does this 'getting accepted for delivery' happens (on my computer, on the server, in the aether...)?
The mail() function uses the system mailer - which can be sendmail, or a mail transfer agent directly. The return value only indicates that the message was delivered successfully to whatever intermediary your system is configured with (usually sendmail on linux systems).
If you need actual data on deliverability, you could use a third party mail service such as SendGrid or MailGun, etc, which use APIs to send and also can report back delivery status, and other data.
As explained in the php.net documentation,
mail() 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.
In other words, the mail() function returns true if he gets all the values he needs to send the mail. But cannot know if the mail was received or not because PHP is not the one sending the mail. The mail server (most likely exim) is.
There's a nice PHP library that can help you validate if an email is valid/exists and belongs to a user. It's called kickbox
So before using the mail() function, you could run the email in kickbox and in combination with the returned true of mail() you can assume the mail was sent out as long as exim does his job.
You can also use an API like MailJet to send out your email in your name instead of using the servers mail server and receive a status report directly from the API to know if the mail server was successful or not in sending the mail.
As most of the replies here already answered your question, may I suggest an alternative which is PHPMailer (https://github.com/PHPMailer/PHPMailer)?
It's a library for sending emails in PHP and you can define the SMTP, account info and even attach files when sending out emails. It even allows for SMTP debugging of sorts as well using "$mail->SMTPDebug = SMTP::DEBUG_SERVER;" for example.
Quite easy to use too. You can refer to the official tutorial here: https://github.com/PHPMailer/PHPMailer/wiki/Tutorial

How to track when mail comes out of my server and if he succed?

I need to track when the mail leave out my server to know possible issues:
Recipient inbox is full;
Recipient's server rejected
whatever..
The main problem is: how?
Currently I use a CentOS server. I can track only if my server receive and queue the mail to sent, but not the process of sending it self.
Is it a issue which could not be solved with PHP? Where should I start follow?
As mentioned in the comments above, one way is to check your mail server's logs. These will show whether or not the receiving MTA accepted delivery, or if delivery failed for some reason.
Another way is to send the message from your PHP script using phpmailer, through a hosted SMTP server which provides you with delivery information, such as http://www.sendgrid.com or http://www.ultrasmtp.com.

php mail() function not working

I'm getting a 'could not instantiate mail function' error from PHPMailer. From reading around, I understand this to mean that the PHP mail() function isn't working for some reason.
The results of phpinfo() for the mail settings are:
To me, this means that mail() should work and that port 25 is open. Is that right?
Is there anything else I can check to make this work please? I had a look at the docs for the mail() function, but I couldn't see what exceptions it threw and how I'd print them out to screen. I did a:
mail('name#email.com', "test", "test") or die("Doesn't work");
type test, but that's my error message and I could do with something a bit more helpful.
Grateful for any help on this.
Many thanks
it doesn't mean port 25 is open, it just means that PHP should use port 25 for contacting the SMTP server. You don't state what OS you're on, but note that sendmail would be a unix-only thing, and will fail if you're on Windows.
That list merely show you your current settings. That doesn't mean that they are right. :)
Your localhost is probably not configured to be a mail server. Set the smtp server to a real server than can be reached from your PHP server.
I am probably way off, but check to see if sendmail is installed, maybe it is malfunctioning. This depends on your OS.
The settings from phpinfo() show the PHP is set up to use SMTP but it does not mean that you have an SMTP server set up on the machine. Your error message suggests that one is not setup.
Good luck
My answer would be - don't send emails by calling Sendmail. The sendmail method (or ANY local method) is a mess of pitfalls... and even if you get past those issues, the bottom line is many spam filters (at the places you send mail TO) simply do not like this type of mail.
To provide just a little detail why the sendmail approach is bad, your sendmail daemon is unlikely to be configured to have an SMTP HELO which matches the reverse DNS of your IP address. Your webserver is unlikely to have valid reverse DNS matching a standard hostname. NO reverse DNS at all is bad, as is rdns like 123-123-123-123-static.someisp.com. SpamAssassing will flag such "unconfigured or default reverse DNS" hosts for example.
Fortunately you don't need to understand or fix everything I just said. The much simpler to accomplish (and easier to test/debug) is to GATEWAY your emails through a working SMTP mailserver. To do this:
1a) Install PHPMailer http://phpmailer.worxware.com/ ... OR
1b) Install the PEAR Mail() library http://pear.php.net/package/Mail
Either 1a or 1b will replace the limited "mail()" function in PHP. These replacements support both SMTP, and Authenticated SMTP.
2) I suggest using Authenticated SMTP over plain SMTP. Either works, but with authenticated SMTP you can literally send mail through another mail server just as IF your script were a local email client like Outlook. This has major benefits. For example, if you are a company sending mail, your mail is more likely to be trusted by remote/target mailservers, since your mailserver has a good reputation and (hopefully) proper reverse DNS setup. But if you originate the email off a webserver, you have none of that (and if you use shared webhosting, you will inherit the email reputation of whatever other sites run on your webserver IP.).

Php mail(), how to get undelivered address list?

I'm getting single emails, for each undelivered address that mail() send couldn't reach.
Since this can be a painfull process, to keep copy pasting every single address, I'm wondering if it's possible to access a LOG or something ?
Some notes:
- I've got this app running on a GoDaddy server (I know it sucks);
Thanks for your atention ;D
There is no log as such, because feedback from the receiving mail server is not immediate. There's only the "undeliverable" messages that get returned to you.
However, it should be possible to specify a reply-to or errors-to address pointing to a separate mailbox. That would make it easier to find out which recipient addresses failed.
That is the best way I can think of to group failed messages.
I'm wondering if it's possible to access a LOG or something ?
Maybe - it depends on what the MTA (mail transport agent) is, how it's configured and whether you have the permissions to access the file.
I've got this app running on a GoDaddy server
Then the above are all questions for your GoDaddy provider.
The MTA log should record details of messages which the MTA failed to offload elsewhere but the elsewhere may be a smart relay (i.e. still on the sending side) - in which case you'd need to look at the relay MTA's logs to see if it managed to pass the message on to one of the recipient MXs. Even if the message got as far as a recipient MX, you don't know that it was successfully delivered. By convention, the receiving system should send back a DSN if it fails (and you can specifically ask for a DSN regardless of outcome) but a lot of systems don't implement this properly.
Regardless it may provide information about some messages which fail - you'd need to poll the return mailbox to identify failures.
C.
If mail() fails immediately, it returns false and you went get any e-mail. However, if a problem arises after the mail has been sent but before it has been received, you get an e-mail.
The mail() function will send the e-mail to some mailserver, which will send it to another, until it reaches its destination. When the user does not exist, this is only discovered at the destination mail server and it responds with an e-mail, called a bounce.
There is no way to group these bounce e-mails, as the destination mail servers do not know that they came from the same script.
Another option you have is to pipe the incoming emails to a PHP script and process it that way. How this is accomplished depends on the server setup you have and whether your server is set up to use sendmail or exim.
A good tutorial on this is located here.

Success verification of mail() function PHP

Is it possible to check if the php can get some kind of a ping/flag back from exchange mail server to say "yes, email has been sent off to the intended recipient"?
According to the PHP manual, the return of mail() boolean could mean; "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."
Does this mean, PHP can return success but actually there could be a problem on the mail server that php wouldn't know about it? and in this case no email has been sent and the user is none the wiser?
TIA
Jared
The mail() function will just connect via SMTP to the server and send the message. Then if the server says the server has received the message, mail will return successfully.
In the meantime, things can go wrong. The user's account could be deleted, the harddisk on the mailserver could crash, the SMTP server could fail to find the user's mail server. The user's mail server could reject the message because the user's mailbox is full. Many, many things could go wrong.
This isn't PHP's fault. And no reasonable improvement could be made to any programming language API to make sure someone got the message. But some companies like CampaignMonitor offer a paid service that will check for bounces and if people open the message to update mailing your mailing lists. Using APIs from services like these, you could check in a few days if the message was successfully received.
So the PHP docs are just saying, 'We can only tell you if the first SMTP server said they got it. Any number of issues could happen between the computers from there to the person with the email account.'

Categories