How can I process incoming emails using PHP - php

I'm fairly new at using PhP, and I'm trying to write a script that will:
Connect to a mail server
Retrieve new emails
For each new email, extract as variables the sender's email address, the subject, and the message in plain text.
Mark the message as read once processed
The script would use these variables to interact with my database, and would be triggered by a CronJob every minute to check for new mails.
I've looked around a found a handful of library that apparently allow to interact with emails (IMAP, Zend Mail...). Before going any further, I have a few questions:
Do I need to install a library (I've never used one before) or do PhP have functions that would allow me to do what I want without having to install anything?
If PhP itself can do it, where can I find a tutorial helping me with that?
If I need a library, which one would recommand using and where can I find a tutorial to help me with it?
Thanks a lot in advance!
Arthur

For those with the same issue:
http://davidwalsh.name/gmail-php-imap
For French-speaking people (the code is understandable by non-French speakers):
http://nicolas-vieux.developpez.com/tutoriels/php/fonctions-imap/

Related

Retrieve and parse emails from (POP3 or IMAP) mailbox in Symfony2

I am looking for a package/bundle that can access an email inbox, retrieve the emails in the inbox and parse them (Sender Email, Subject, Body, Attachments) for processing in my Symfony2 application. The idea is a "helpdesk" where people can email a specific email address and a cron job will run through the email inbox and convert emails into helpdesk tickets (with attachments).
Before anyone tells me to Google or something equally unhelpful, please note that I have spent over an hour on Google as well as gone over more than 100 StackOverflow threads looking for something that can help me with this. Most of the content involves sending emails and not retrieving them. I have built a pure PHP parser before and it was a nightmare (as almost every email client composes the emails a little differently).
I found https://packagist.org/packages/lasso/mail-parser-bundle, but it seems to only be for Zend. I have also found https://github.com/iJanki/MailMimeDecodeBundle, but it seems to still be a work in progress and there is no documentation to speak of. Additionally, I found Correct way to retrieve mails by IMAP in symfony2, but that looks just like the start of the nightmare I had last time when I built one from scratch.
I would appreciate any suggestions or pointers from anyone who has implemented something similar or has been in a similar situation.
I have found a PHP class that does exactly what I wanted and neatly parses the emails into fromAddress, subject, body and attachments. It even saves the attachments to specified location on your server.
https://github.com/barbushin/php-imap
While it is not a Symfony2 Bundle, it is very easy to integrate into Symfony by supplying the class with a namespace and then using it in your desired controller.

import email data to the DB

I built support system (web) where my helpdesk can open new tickets/issues.
I want my clients to be able to send email with the issue text and file attache, to specific email address, and new ticket will be opened in my system.
For that I need to know how to scan the folder and how to add the email data to my DB.
what do I need to look for, in the internet, for that? what is the subject?
(I'm using PHP)
As the other's have commented, it's likely a larger task than you're ready to handle. But it doesn't hurt to try.
If I was tasked with the job, I'd take advantage of Gmail (for it's spam reducing features and large storage) to accept incoming email. From there, you simply need to setup a script that connects to your email account and processes the email for storage in your database.
Normally I recommend a solid library for making the job easier and cleaner, but I have a suspicion that you may not be familiar with OOP. If you at least know how to utilize classes, then check out Github:
https://github.com/search?l=PHP&q=imap&ref=searchresults&type=Repositories
Otherwise, if you're new and don't mind writing something "messy" then the following should at least point you in a good direction:
Connecting & retrieving emails for IMAP:
http://www.php.net/imap
http://www.php.net/manual/en/function.imap-open.php
Fetching/processing attachments:
(note that attachments are part of the email body)
http://www.php.net/manual/en/function.imap-fetchstructure.php
Storing attachments (in the filesystem):
http://www.php.net/manual/en/function.mkdir.php
http://www.php.net/manual/en/function.file-put-contents.php
There's plenty of Googling left for you to do. So go forth and make a lot of mistakes. Read the manual. Kick yourself for not having read it sooner, then go make more mistakes. Isn't that how most of us learn?
The first thing that comes to mind is to pop the most recent emails if you have pop3 set up or use imap functions. I did something similar to this using c# using openpop.net. So that could be a starting point.
You can use this method, using the cURL to fetch the emails from Gmail server through feed atom. XML response will return and we can convert it to HTML.
http://www.code4share.net/items/get-unread-email-in-gmail-by-php/XRGXVVh.html

Creating a database of emails from stored emails in my inbox

I have an inbox with 5000+ emails saying that somebody has signed up. Within the body of the email is their email address and name which I want to store in a database.
Q) How can I get this into a database? Ideally mysql.
Ideally I would like to do this in a PHP which I am most familiar with.
I am using iRedMail with: OpenLDAP
UPDATE: since posting the question I have already written the actual email parser using Plan Cake Email Parser, literally took 5 mins once I found this.
UPDATE 2:
To make things easier I have moved all of the emails I want in the database into a separate IMAP folder.
But when I do a search it returns nothing: find /var/vmail -name 'Subscribers' -type d –
UPDATE 3:
Another alternative is that I already have the emails downloaded in my email client locally using Thunderbird Windows 7. When I check my Profiles I can see 3 files related to:
Subscribers.msf
Subscribers-1
Subscribers-1.msf
The real tricky part isn't parsing the emails, but getting the emails from your server. There are three ways I can think of doing this:
Use php's imap library http://php.net/manual/en/book.imap.php. I personally haven't worked with it very much but since you're just fetching all emails, it might be useful. The downside is that you'll end up fetching all of your other mails as well.
Use a 3rd-party service to handle the imap and expose your email via a simpler REST interface. Ideally you'd want to search for a key term (maybe "user signed up") and then parse it.
Use offlineimap to download all the emails as a Maildir and store it localy. Now you can use PHP to search all the emails and it'll be faster and easier to test the version here.
As for parsing, I personally recommend using grep to find the mails that contain the information you want to extract, and awk to convert this information into a CSV file. You can do text processing using PHP but it's far simpler and cleaner to do it with these tooks.
You can now easily push the data into MySQL.

Generate And Email Excel File using php

Is this possible without having to first create a physical file on the server?
Initially I had to make a php page that would email each week information about new members of a site... That was fine I just used a cron job running the php page weekly... Now I've been asked to set up the email so it sends an Excel file of the data automatically... Does anyone know if this would be even possible? and how?
Regards,
Vinoth S
Yes, this is entirely possible. Libraries such as PHP Excel allow you to construct spreadsheets in memory without writing anything to disk. Libraries like Swiftmailer allow you to easily add attachments to e-mails from memory. Simply combine the two to do what you want.
Theoretically, one could create the email attachment by creating a carefully crafted email message. And then carefully constructing a document. This might mean breaking some of the email message composition rules if using the php email function.
Seems possible, but the most feasible implementations would be to use the well-suited tools to do it.
Do you know about OpenOffice? I think that can be scripted to run from a server context.

PHP Basecamp reply with e-mail feature

I was using basecamp, and for clients to reply to a message and automatically replies to it in basecamp is a pretty nice feature.
I was wondering how it's done, anybody have any ideas?
I am a PHP CodeIgniter developer, would be helpful if somebody gave me some pointers to do some research into this..
Thank you !
Creating this kind of a functionality is quite daunting, but let's give it a try:
You should have some sort of identification for each e-mail send, so you know to which message/topic/object it belongs. I believe Basecamp is using a special return-address (hash#basecampnow.com) for each message.
Every time a new message is delivered to the object's inbox, read it contents and match all data above the ====== reply above this line =====. Check the sender's e-mail address, verify if the user is allowed to post to this object, write to the object and delete the message. You could check if you mail server has some kind of hook support, that fires every time a new messages comes in. Alternatively, you could run this as a cron-job every few minutes (but this is not very efficient, as you may imagine).
Personally, I've been wanting to make a Node.JS implementation of the incoming mail server, but I haven't come around to it yet (but check out this GitHub project for some inspiration.)
I started using SendGrid to handle parsing incoming e-mails. It works very nicely.

Categories