PHP detect and store returning emails - php

I'm sending some newletters using PHP mail() with a Sender address.
For some reasons that I will have to check, many of these newlsetters are bounced and return a "failure notice" to my Sender address.
At the moment these emails are simply returned to the email client where we read the emails of the Sender.
I wish I could store and manage all these "failed" email addresses for my marketing staff.
Of course it is very hard to copy/paste each email address from the email client (they are hundreds).
Is there a way to detect and catch via PHP these addresses to be stored in a Mysql table?
Thanks in advance.

You can't get the information about failed messages from the mail() command as this only sands the message to your local mail server for handling.
You will need to implement a cronjob or otherwise regularly called script that will check the mailbox. So you will need to use the IMAP and POP functions in PHP to fetch the messages and process failed messages.
Or you will need to check the manual for your mail-server to check if you can include some code to execute when the server encounters a failure. So the mailserver itself will flag the failed messages inside your database or at least calls a script providing it with the failed message.

The customary solution to this is VERP http://cr.yp.to/proto/verp.txt -- basically, use a unique envelope sender for each message; anything sent back to that particular address is a bounce, from the address you tried to send to.
For example, if your list is called fnord#example.org and you send message 12345 to djb#example.net.invalid then the envelope sender for that message would be something like fnord-12345-djb=example.net.invalid#example.org and your MTA should be configured to route any email to this address to the correct place (a script to remove the failed address from your database, or whatever).
Of course, if you use a proper mailing list manager (and you should!), it will already contain the logic to take care of all of this; it should be a simple matter of configuration.

Related

php imap: can't see actual recipient if bcc

I'm working with PHP IMAP functions to poll an inbox and process the emails. Sometimes the emails are received via bcc -- this email was bcc'd by the sender. I need to be able to retrieve the actual email bcc'd -- but imap_fetchheader and imap_rfc822_parse_headers don't provide that if the recipient is bcc'd (even though it's in the return spec).
The issue is that I'm using plus addressing (myemail+value#domain.com) as part of my script, so it's not sufficient to know the email landed in my inbox -- I need to know the specific version of the address it was emailed to.
I understand the whole purpose of bcc is to be hidden. But I would think that there should be something in the header of the bcc-recipient to indicate the email was sent to them.
The "bcc address" that message is sent to is not part of the message itself, it is part of the message exchange protocol. It is used on the protocol level of smtp whilst handing over the message to the receiving server. The content that is handed over, so the message payload itself, is something different. There is no way to read that address from a received message by means of a protocol like imap4 or pop3. You'd have to scan and parse the smtp servers log files for that.
You can easily check that yourself: open the source code of a message received. So the original, technical payload including everything. That looks ugly, but it shows all information actually contained in the data. You will not see any mentioning of "bcc" or a "bcc address" in there.

Bounce invalid dynamic email address from PHP through postfix pipe

I have a web application in PHP that allows users to create documents and send them as PDF attached to an email.
Originally, it was sent with a reply-to pointing to the user's email address, but I am implementing an internal email address system. The goal is to keep all the replies to the original email related to a specific document in the application.
All internal addresses start with PREFIX followed by delimiter "-". Postfix processes all email sent to PREFIX#mydomain.com through a pipe to a PHP script that tries to match the address to a specific user and document (eg: PREFIX-U1A-DJX8#mydomain.com refers to document JX8 of user 1A)
Everything works well but I am wondering how to handle invalid emails:
When no document is found related to the address, it is most probably spam.
However it's possible someone manually wrote the address and was mistaken, in that case the sender should be notified.
Should I simply send a generic mail from the PHP script to the sender stating the address is invalid?
Or is there a way to send a bounce either through the script or to tell postfix to handle it?

Bulk Search E-Mail Headers

I would like to find out all e-mails that have been received by our mail server from a particular IP address, contained in the Message Headers.
It is a static IP, and I have found some already by manual means, but I am trying to find a way to either do this programatically, perhaps using a PHP script with full access on my mail server, or perhaps there is a function within cPanel itself which will do this?
How would one go about searching all e-mail headers for this IP?
You may want to have a look at procmail. It's a tool that can be used to process email messages as they arrive to your mailbox. You can specify the processing on the email message based on any field in the message header. In your case, you would be considering the "Received:" field which displays the IP address upstream email server.
Although procmail is generally used for processing email as it arrives, it is also possible to use it to process existing mail stored in mailboxes if you can cat and pipe the messages from the mailbox to procmail.
There is a simple example in the link below that explains the basics of using procmail.
Howto filter and forward e-mail with procmail: example

Do I need to whitelist any IPs to use Mandrill API?

I am trying to send mail using Mandrill API and I have got following responses :
[{"email":"_emailid_","status":"queued","_id":"772237d78ce74e5c9bd8ccbd9a1cfb8c"}]
{"email":"_emailid_","status":"scheduled","_id":"10a0b596a95e44ddb5e8cf504e3899ce","reject_reason":null},
{"email":"_emailid_","status":"scheduled","_id":"b699d58f021b43b2a0b25d801d1b4f91","reject_reason":null}
But I am not getting any mail on the email addresses sent. Please help.
Do I need to whitelist the IP of the system from where I am doing the curl call ?
You do not need to whitelist any ip addresses, you however do have to configure the DNS records for your email domain properly, head over to the domain settings page in the mandrill control panel for more information.
Everything seems fine.Either your you are not providing correct email to api or mail are coming to your spam folder.You need to check mail from address which you give to the api.
It looks like at least the last two were scheduled, which means they won't send immediately, but rather at the scheduled time in the future. You'll probably want to double check the time and date that they were scheduled to be sure.
For the first one, there are a number of reasons that emails are queued, including if they have attachments that need to be scanned, or if they're part of a large/batch API call. We'd need more information about the API parameters that you passed to determine the reason for it being queued and/or what happened once it was queued.
You can search your Outbound Activity by the recipient email address, subject line, sender, and a number of other criteria for emails that have been sent. Messages that are scheduled but haven't sent yet cannot be searched since they've not been processed. For any messages that have sent, click on the word "Delivered" on the Outbound Activity page to see more details about the response that Mandrill got from the receiving server when delivering the message.

How to handle mail delivery errors with PHP

I am building a symfony module for sending newsletters. the idea is to build a queue list of adreeses to receice the mail and then ,with a cron job, send let's say 50 at a time.
What i don't now how to do is to read the Mail Delivery reports that are sent back by the server when an email adress doesn't exist or the mail is full. The idea is to store these error reports an clean the adress list.
Any ideea how to implement that?
When reading the "bounced inbox", you can use a class like this to actually parse the mail and see what status was returned (e.g. permanent or temporary error):
http://www.phpclasses.org/browse/package/2691.html
To really parse a mail accurately will give you a hard time, as not all mailservers are alike and some will send you a "mailbox full"-error marked with a "permanent" flag while others may tell you that the error "user doesn't exist" is "temporary".
I tried a solution for this once and ended up setting up my own parser connected to a huge database containing possible server replies (and their "real" meaning :).
You can use a reply to address while sending. So bouned emails will be sent to this id. You can also create another PHP script which will read this "reply to" email inbox and get the id from it. You can then remove this id from the list you have.

Categories