How to get information of mail delivery in PHP - php

I want to send mail by PHP mail() function.
Does PHP mail() function return delivery status? If not then what is the way to find whether my mail is delivered or not in PHP?

Is php mail() function returns delivery status
No. There is no common way to tell an E-Mail's actual delivery status. mail() will tell you only whether the E-Mail has been accepted by the outgoing server.
Here is a list of approaches to request a delivery notice, but none of them is waterproof.
If you have trouble with E-Mail arriving, there are several related SO questions, among them this one.

It returns a status telling you if it has been successfully delivered to the MTA responsible for actually sending the mail.
You cannot find out if the mail has been actually delivered to its recipient or not without setting up a special mailbox which receives bounces for your emails. If you decide to create one and set the headers to ensure bounces go there remember that it might take some time before a bounce arrives so right after calling mail() and getting a TRUE return value, you simply have to assume the mail has been sent successfully.

You can check /var/log/mail.log, which is the usual location of the sendmail log and only if sendmail has been configured properly in syslog.conf.
# Log anything of level info or higher.
* .info;authpriv.none;cron.none
# Log all the mail messages in one place.
mail.* /var/log/maillog

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 check if mail server has been configured or not?

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.

PHP mail() and sendmail (Unknown User error)

I'm having an issue with PHP mail() and the standard sendmail MTA on debian. I want to be able to send mail through PHPs mail()-function from "users" not existing on the system. As it works now I can send mail just as long as the user (user#domain.tld) exists. I want to be able to send mail from any "user", e.g from random#domain.tld where random may be a non existent "user".
Now to the real question; Is there any setting in sendmail that will allow this?
No setting needed, typically. You can send e-mail from anyone in the world... it's just a header.
However, you should know that spam filters are smart to this! There are numerous methods for checking that an e-mail was legitimate, such as SPF records and what not. It is poor practice to send from non-existent accounts, and your chances of ending up in a spam filter are quite high.

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