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

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.

Related

PHPMailer using Sendgrid, customizing each email but sending just one email

Our main programmer died from covid. He had built a program in his code using Swiftmailer, which is no longer active.
I am going to reprogram it all using PHPmailer.
But I don't know how to customize each email with their first name, username in our website and their custom info.
In the PHP for swiftmailer, he did it like this:
$hdr = new SmtpApiHeader();
// Set all of the above variables
$hdr->addTo($toList);
$hdr->addSubVal('~USERNAME~', $usernameList);
$hdr->addSubVal('~FirstName~', $firstnameList);
$hdr->addSubVal('~code~', $codeList);
~code~ was a merge field to replace a security code when it sent an email to them, like if they changed their password, and they needed to get a code to authenticate it was from them.
or update their profile.
so those are where he added their personalizations, those were arrays.
$toList // this would be an array of every email. then it matched the position of the other arrays for their personal info, to put in each email.
He built these as the function to send one email, or many, depending on what the email is for. All based upon subscribers who have opted in to get transactional emails from the system.
So how do we do this with PHP Mailer? I have been reading since last night, everything I could find and I cannot see how to do it.
can someone point me to a place online with the right documentation? Most of what I read, none of it touched on it.
I feel like I've been spinning my wheels.
Thanks for any pointers.
-Rich
You can set all the message parameters through equivalent properties in PHPMailer, such as From, FromName, Subject (and many more, see the docs), but PHPMailer does not have built-in templating. There are many ways to do that, and many packages that can help you do it (Smarty, Twig, etc), so I recommend you use them.
For sending to lists, look at the mailing list example provided with PHPMailer.
Alternatively, while SwiftMailer itself is no longer supported separately, that only happened because it became Symfony's mail component. I've not looked at it, but I strongly suspect that it will be largely unchanged, so you may find it easier to use that.

Reading File and Mailing with PHP

I would like to read a data file (such as csv or txt) and mail it using PHP. I would like to be able to have at 5 minute pause in between each send of data. I would also like to use the email field as the senders email. Any help would be appreciated.
Each data chunk will contain:
name
email
address
city
state
zip code
phone
comments
I'm guessing you want to write a newsletter application, so proceeding under that assumption:
You are probably not going to find this answer very helpful, but you should really consider using a newsletter service instead of writing your own code. The reason for this is that sending newsletters requires a LOT more effort and different skills than writing a simple script.
For example, but not exclusively:
Setting up a mailserver correctly.
Taking care about the reverse DNS.
Managing digital signatures (DKIM) for your mail.
Subscribing and tracking sender feedback loops.
Tracking and avoiding DNSBL blocking lists for your server.
etc.
If you really want to write your own code, you have been warned. Take a look at str_getcsv() and SwiftMailer and don't be surprised if you get headaches from this project.

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

Forwarding IMAP Messages with Perl

I need to handle some mail. I already have a script built that can parse through a mailbox and perform several actions like save attachments, move email to a folders and other administrative tasks. A few of the emails are identified as rogue during this process and need to be forwarded. The messages may or may not have one or more attachments and are dumped into their own folder labeled fwd.
I can create and send new email messages but am having trouble finding information on forwarding or replying to existing email. One solution would be to save the parts (body, subject, attachments) to a database and construct a new message with MIME::Lite but this seems inefficient at best.
I am handling the email with Net::IMAP::Simple::SSL and MIME::Parser.
Since the email is dumped into a temporary folder for holding I am not totally against using a PHP script to handle the messages, but prefer something in line with my current Perl handler to execute the task.
Looking for some helpful info to help complete this task.
You might want to look into CPAN at Mail::Box, a rich (and a bit complex) module handling mail messages, including primitives such as message->copy and message->reply.
For documentation and examples, author's website is at http://perl.overmeer.net/mailbox/

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