Bounce invalid dynamic email address from PHP through postfix pipe - php

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?

Related

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

Create email address account php

I saw in some advertising websites, that when you want to send an email to the ad owner, it does not show the real email, but rather the email address under that website's domain(which definitely looks like auto-generated, rather than chosen by the user), say smth like n8MngCo5cHg#example.com.
So, AFA I understand, when an email is being sent to that address, it is being redirected to the ad owner's real email. I am trying to to do the same thing by php.
QSN1 By php script, how to create that email address for the user, when he registers ?
QSN2 Is there a way (again by code) to set up a forwarder, so emails sent to n8MngCo5cHg#example.com automatically will be redirected to user's real email user_email#gmail.com, or I should fetch emails by IMAP from time to time and send to to the user ?
I am using apache2.x/Debian 7, if it matters.
These two questions I found, are for cpanel, but I am not using it or any other UI.
Create an email account with PhP - With some things I need to work around
Create emails accounts using PHP
Thanks
Here is one way that you can create an random 'alias' email address:
$aliasaddress=md5(uniqid(mt_rand(),1)) . "#example.com";
Then, as SyedQarib suggests, you would store the alias address and the actual address in a database, so that your program can get the actual address given an alias address.
Finally, you would need to setup your mail server with wildcarding such that it accepts messages to *#example.com, and forward each incoming message to your PHP script, so that for each incoming message to an alias address, your script would parse the incoming message, capture the alias address that it's addressed to, query the database to find the corresponding actual address, then forward the message to the actual address. For information on how to setup a mail server to forward incoming messages to a script, see http://harrybailey.com/2009/02/send-or-pipe-an-email-to-a-php-script/ or How to setup a mail server?

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.

mailto: replacement due to size

I have a web page that generates several email addresses. I need to be able to click on a link, which will open an email client such as outlook and populate the bcc field with those email addresses. In the past, we have used html's mailto, which achieves this goal perfectly.
My problem now is that I need to send emails to over 200 people, and mailto cannot handle that much information. Since the page also uses PHP, I have considered PHP's mail() and phpmailer(), but since both require that the entire email be generated on the page and the email client is never opened, they will not work.
Does anybody know of an alternative method I can implement to achieve this functionality?
Create a mailing list, add those e-mails to the mailing-list and send mail there instead.
As a bonus, you can use VERP to prune invalid addresses.
I don't think there is a convenient alternative method.
You could offer a textarea field containing all the addresses in a comma-separated list. That list could be easily copy+pasted into the client's E-Mail program.
If the client's E-Mail is on the same domain as the web site, and you have full control over your server, you could randomly generate E-Mail addresses on your server using PHP:
1293820239453202349#example.com
that E-Mail address would be configured to forward incoming mail (that your user with the mail client writes and sends to that one random address) to the big list of recipients. This is a very advanced method but hard to implement.
Maybe you can make an email group, depending on your mail system, such that when you send a mail to the address of the group, it will distribute to all members?
On our company, we have several groups. all#company.com, sales#company.com, developers#company.com etc. Sending one e-mail to such an address will make all members of the respective group receiving it.
MailTo with a Copy
<a href="mailto:astark1#unl.edu?cc=ASTARK1#UNL.EDU">
MailTo with a Blind Copy
<a href="mailto:astark1#unl.edu?bcc=ASTARK1#UNL.EDU">

Forwarding email to distribution list

I recently had to move servers. On the old server, I had a Mailman listserv distribution list, but since Mailman was not supported on the new server, I wrote up a PHP script to read messages from IMAP and resend via SMTP. The script is brittle (my fault) and complex (not my fault): I'm having to parse each message, find the bits that I can use (if it's multi-part or HTML or it has attachments), and then reconstruct the message for SMTP so that each member except the sender gets a copy and the Reply-To is the distribution list address. The thing is, I know that the raw source of the message is fine as is. I just need to change/add a few headers.
On Windows servers, you can configure an email dropbox folder where you could save outgoing mail messages in their raw form. Is there anything similar on *nix? Is there any other method I should look at?
I know I can set up a forwarder address on the server, but I really like the behavior of listserv systems: senders don't get a copy of their own message, the subject is identified with the list's label and hitting "Reply" sends a message back to the distribution list, not the original sender.
In order to create your own bespoke list server system you'll need to do the following:
Configure your mail server to have an alias such as no-reply#yourdomain.com to redirect messages to black-hole or /dev/null or something like that, basically deleting any messages on arrival without returning NDR (non-delivery report) email messages to senders. This is the address your script will use in the To: header of your email messages when sending to the group.
Write your PHP script section that sends to the group in such a way that: (a) first pulls all your subscribers out of a database (or securely stored data file) into an array, (b) next remove the message author's address from the array, (c) then finally loop through all the remaining subscribers in the array in batches of about 15 recipients and send the message with the mail() function using To: "Your List Name" <no-reply#yourdomain.com and the BCC: (blind carbon copy) field to prevent receipients seeing who else is on the list.
If you're wanting to try out some of the existing solutions for this why not take a look at
Majordomo.

Categories