php imap: can't see actual recipient if bcc - php

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.

Related

How to get an email delivery acknowledgement using PHPMailer?

I am using PHPMailer to send emails, I need to rectify which of my emails have been reached successfully to receiver's inbox. Is there any way to get the acknowledgement or delivery receipt for each email sent?
If possible I need those email id's inserted into my MySQL database.
There are several approaches to this, none of which are reliable.
You can request delivery receipts by adding the Disposition-Notification-To header. PHPMailer has built-in support for this:
$mail->ConfirmReadingTo = 'confirmations#example.com';
Not many clients support this, and even if they do, it's likely they have it disabled for security reasons. Generally it's only likely to work within company intranets.
Secondly you can add an opening tracker or beacon image in your message body so that when a clint opens the message, the loading of the image tells you which message was opened. You need to embed a unique identifier for the recipient and/or the message in the URL, such as a hash of the email address and timestamp. Again, this is not reliable because most clients have image loading off by default, or the images may be opened by inbound mail scanners, giving you false positives.
The most effective way is really to get to know your mail server, and handle bounces correctly, but even then, just because a message has been devivered successfully to the recipient's mail server does not tell you whether it reached their inbox, nor whether it has been seen or opened.

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

Emailing Processing Bounces

I have a database that contains 20K of what the email I was sending newletter The problem I found was that each of the email I sent back type
Delivery Status Notification (Failure)
I am willing to transfer this email which is inactive knowing that I filter all email from the standpoint of the field formatting and existence.
as I have to see how little research has addressed this problem I find the notion that bounces are already implemented on the software.
If any one has any That a idea how I can apply that bounces in the php will be welcome
cordially
Well, it's pretty hard to understand what are you talking about, but...
As you mentioned, when e-mail can't be delivered (for many reasons) you will get Delivery Status Notification which will goes to inbox of e-mail that you used to send the message.
For example, if you send newsletter from newsletter#test.com to customer#test.com and the message can't be delivered then you get notification at newsletter#test.com. So just open your newsletter#test.com inbox with php, read the messages, parse them, and save the errors (if occurs) in database, matching by e-mail.
This link could be useful:
http://php.net/manual/en/function.imap-open.php
If you have your mailbox at your own server then the thing is much simpler, because all you need to do is to make ln, and then just open the file to parse.
ln -s /var/mail/my-inbox /var/www/my-script/inbox

PHP detect and store returning emails

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.

Detecting bounced messages

I am working on a tool that will be sending bulk messages (not spam :) and I need to add a feature that will detect bounced messages. Is there a standard response associated with bounces? Would it be in the header of the body?
This is typically achieved by setting the Return-Path header of your outgoing Mail to a unique Address for every recipient. For example, you could use bounce+userid#example.com if you have a unique userid identifing every recipient.
If the Mail gets bounces, you recieve it and parse the reciever (which will bounce+userid#example.com), you can then take appropriate actions.
Using the + syntax makes it possible to create a single mail-user (bounces#example.com) which recieves all bounces and still distinguish between the recipients.
You can not rely on either the headers or the body of the bounced message to be able to reliably identify the original recipient, especially if you want to automate the process. Even if you add your own custom headers, it's likely that the bouncing server will strip them when it sends notification back to you. And trying to parse text in the body of the message in order to pull out the recipient's email address can be dodgy at best, as there's no standard format and every bounce you get will be different. The only piece of information that will remain completely untouched in a bounce is the return path email address -- the address that your server advertises it wants bounces sent to. Thus, the only way to automate truly accurate bounce catching is to encode the recipient directly into the return path address itself.
This would typically be done by overriding your server's default return path address for each outgoing message, using a unique value per recipient, like bounce-XXXXX#yourdomain.com, where XXXXX is some encoded and/or obfuscated representation of the recipient's email address or some other internal identifier. This technique requires the use of an email server which can support this type of wild card catch-all address so that you don't have to set up a new bounce account for each email address you're sending to. Assuming that, you simply configure the server to dump all such bounce-* emails to your script, which needs only to decode the XXXXX in order to determine who the original recipient was.
If your tool is going to be talking directly to the recipients' SMTP servers, it
might be more advisable to check the error codes returned via the SMTP protocol for
4xx (temporary failure, e.g. "mailbox full") or 5xx (error, e.g. "no such user")
responses. Due to antispam/backscatter prevention mechanisms, you shouldn't rely on
the recipient's server to reply with a non-delivery report whenever a message doesn't
go through.

Categories