Undelievered Emails PHP processing - IMAP - php

I have a website that let users subscribe with their email IDs.
I want to create a cron job that checks the bounced emails via imap connection, and I am using PHP class for that.
The cron job's script supposed to search for emails from "Mail delivery system" and scan the body of the email to detect the invalid email ID in order to mark it in mysql database.
I don't have any problem with IMAP connection or search, but the problem is: I can't find the pattern that the script should look for to define where is the invalid email ID written in the message.
Could you please help me what shall the script look for? (e.g. some word that the email ID will come after or before? Or maybe an HTML tag in the body of the message that carries the invalid email ID)
Thanks for the help

Related

Email to trigger PHP Script... Possible?

I'm in need of setting up an auto-response from an email account that I control, based on trigger words within the body of the email. But also, it needs to add/delete rows from a database table based on trigger words sent to this email account (that belongs to my site) and it needs to create a topic, in some cases, within a forum, if sent to a different email address (but on the same server). I know PHP to be able to do this, but not really sure how to trigger a PHP script to be executed when an email gets sent to a specific email address account that I control. Or if there is another solution to accomplish this, please let me know.
This is basically an inquiry on how to accomplish something like this, based on an email sent to a specific email address on my server with words like: "Join", "Leave", "Set Mail", "Set Digest", etc. etc.
There will be another email address account set aside that will need to send those subscribers in the database, the same exact email (Mass Send). This is for a CDB-L ListServ. Kind of old school I suppose, but we want to transfer this ability to our server, since these old school methods still work today and is very much active.
Curious on security issues, what type of server software I'll need and just a basic approach on how to set something like this up.
You can alias your email address directly to a php script if you run your own mail server (on linux this would just be in the /etc/alias file or equivalent where the target was your php script instead of an email address) eg http://www.topwebhosts.org/bbs/board.php?bo_table=server_mgmt&wr_id=73
If not, then your only real choice is to set up a php process that checks an email address for mail every x minutes.
I have used both these methods over the years to great success
You can walk through emails with PHP's IMAP functions and undertake action (based on conditions/content). More info.

PHP / MySQL Ticket Response - Store E-mail repsonse in database?

I am building a basic support request system where the customer can log in and ask a question and an admin can go in and reply and it will set the status to "Responded" and e-mail the customer to let them know someone has responded.
My question is.. I have a "comments" section which is a log of the interaction between the admin and the customer. If I e-mail the customer the initial response from the admin, then I have a feeling they will just hit "Reply" from their email and start communicating through there, and the logs won't be stored.
I could either e-mail the customer and say "Log in to view the response", or maybe if the customer does hit reply I can somehow track it and insert that in the comments table like they did it from the website. If that is even possible?
Just wondering if there is a standard way to do this and any suggestions you may have.
Thanks!
When sending the email to the user you can have it sent from an email address created for that specific ticket. Something that can identify it with your email system to help you route it back to the php ticketing system.
support(ticketnumber)#domain
support12345#mydomain.com
Then it depends on your email server how to go from there.
There are several useful tips at this question that may help or get your started.
How to get email and their attachments from PHP
If you want their reply to be automatically inserted into the DB, you'll have a assign a cron job in your server to run a php script to detect whether there's a reply from a customer (you need a table listing the customers' email and names.
Each time a customer uses the ticket system their email and name goes into this table).
You'll need to connect to your Inbox too via imap or SMTP, and there are scripts to do this (phpmailer, swiftmailer, etc) and "walk" through each email and see if the sender email matches any in your customers table. Then so an INSERT to the comments table.
Anther way is to read through the emails each time the comments page is loaded, but this will cause the page to take longer to load. However, the data will always be more "real-time" compared to cron jobs.
You could use email piping (if your server supports it).
In the subject, you'd have a unique identifier which contains the ticket ID or something unique to the ticket. Example: "How do I eat food [Question: #1234]", where 1234 is the ticket ID.
In your control panel, you would set up an email forwarder to your email piping script.
This tutorial offers the basics to email piping, and I used it as the base for my piping script: http://www.damnsemicolon.com/php/parse-emails-in-php-with-email-piping-part-1

Recognizing an email i.e ticket ID with PHP

Looking to be pointed in the right direction to create the ie. someone emails to support#domain.com, with their issue, the email via php is recognized that it's a "fresh" email that's not in the system so the email is registered in a database, the sender of the email would receive an auto response with a ticket ID saying we'll get back to them & that's their ticket and any emails back and forth with that ticket id in the subject line and/or bottom body would be registered in the database so the customer could login to the client area and see them and reply there as well.
I know DreamHost & Apple (specifically iTunes Customer Support) makes use of this "id - ticket" type system, and just want to be pointed in the correct direction.
Thanks.
This would probably be a good read for retrieving and working with emails in PHP - http://www.tuxradar.com/practicalphp/15/6/1.
Each email message should have message id in the headers, but this not something that you can really rely on so I would use some combination of a few email message attributes. Maybe the sender's email address and a checksum (http://php.net/manual/en/function.crc32.php) of the body and subject.
You could use this (http://php.net/manual/en/function.uniqid.php) to generate unique ticket ids and store them in the db or just use the primary key of the tickets/emails table.

PHP Pear Mail on reception

I would like to know if there's any way I can send a automatic answer to client when I open them mail who's comming from my website ?
Anybody have sugestion ?
Has asked here is more information:
I Have a online cart who send me a
e-mail when I receive order, I would
like that when I open the order
there's a email automaticly been sent
to the client to tell him that i've
took his order. Like when you open a
e-mail and that tell you that the
sender asked to receive a confirmation
that you've received the mail.
For resume I would like that when I receive a order mail, and open it there anoter email who's sent directly to the client telling him that i've openned his order and processing it.
You can create a script on your website, for example 'mysite.com/confirmread.php'. In this script you can write code to send an e-mail to the customer.
Then, in the e-mail that is sent to you on the customer's order, you can create a link towards this script. You can click the link then, for example: 'mysite.com/confirmread.php?ordernr=2883992' or 'mysite.com/confirmread.php?mail=customer#domain.com'.
Another way might be an automatic script that is fired once you open your e-mail. I'm thinking of a javascript / ajax script, which calls the same script (confirmread.php), but without you having to click the link. I'm not sure to which extent scripting is supported in e-mail clients (think of the difference between online vs offline mail clients).
You can, of course, also reply the e-mail to another address of your own. For example sale#mysite.com. This address can then be read by the PHP script. But I guess that's more complex.
You have to hook into the IMAP server and check the status of the emails periodically. And when on changes its status from unread to read you can send an email. But this means you have to track the status of the emails in a separate database against this DB you have to check then if the email was already read.
Better solution:
Add into your order mail a link at the bottom which links to a script which sends an email to the customer. So, you have to manually trigger this email but its very easy to implement.
Example in pseudo code:
Link at the bottom of the order email:
yourserver.com/sendEmailThatOrderWasReceived.php?orderid=123&customerEmail=test#example.com
Script on your server:
<?php
mail($_REQUEST['customerEmail'], 'Order '.$_REQUEST['oderId'].' is confirmed', 'Thank you for your order, we had a look at it.');

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