PHP Basecamp reply with e-mail feature - php

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.

Related

How can I process incoming emails using 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/

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

how can I fetch a message using a php script?

I want to make it so that I can email a url and a php script or something at that url will take the contents I emailed such as message body and write them into a DB. I know how to write something into a DB but I dont know what to do in order to EMAIL to a url or person and have it pull out the message.
The reason I am thinking of emailing to a url if possible is because this saves me the massive blob of trouble of setting up IMAP and using all that mess. I know this can easily be done with php I just have no idea how.
You cannot send email to a URL, you must use a valid email address.
If you want to read your email from within php to import into a DB, you'll need to communicate to your IMAP/SMTP/POP3 server via a socket connection with the fsockopen() function.
I suggest you download any open source web based mail client such as RoundCube, Squirrel Mail, etc... and examine the source code.
There are quite a lot of options when it comes to receiving email. You'll still need to use an email address to send to but you can forward that email on via an HTTP POST using a couple of options such as cURL scripts or using a third party like CloudMailin.
I wrote a blog post about this in Rails (although the principles apply directly to PHP too without much modification. This is a dup of a couple of other questions but it was asked in a fairly different way hence the answer.

Is it possible to send mail asycronously using PHP while giving user feedback on delivery?

Is it possible to send mail asycronously using PHP while giving live user feedback on delivery?
I have recenty written a small app for our company's intranet for sending formatted emails to customers. The interface is quite clean and only requires the input of a job number, it then builds and sends the mail. The mail, while being built, obtains a number of attachments from another server and everything is automated. The library used is PHPMailer.
Is there a way, using other technologies, possibly, but still using PHP as the main language, to show progress of the mails being sent? I have coded robust error checking to check if the mail was actually sent, etc. but i am missing a way of giving users the visual clue of actually delivering the mail to the server via a progress bar, etc.
Is this possible using PHP and something like Ajax? How would you determine the progress of the mail in transit?
I'm not familiar with PHPMailer, but you certainly need support of the library to be able to query it about the status of the emails being sent.
Given that PHP doesn't have threading, I would suggest having a database queue for deliveries, and have an external PHP process triggered from the main site (or via cron) that processes the deliveries on the side, marking on the database the current status on each delivery: NOT_PROCESSED, IN_PROGRESS, CONNECTING, CONNECTED, SENDING_DATA, ACCEPTED, FAILURE_X . You can query the database for the status on each delivery via Ajax.
If PHPMailer internally uses the standard PHP mail() function, which uses a relay SMTP server in your machine, you cannot have that much information about status (which you would have if you created the sockets yourself), you can have just three main states NOT_PROCESSED, IN_PROGRESS, FAILURE_X.
(FAILURE_X really represents many states, as it explains the reason for the failure).
A final consideration on using mail() is that the status you'll be able to know is just the status from the local SMTP relay, which will always accept very quickly, and you won't be able to tell if the mail was really delivered to the outgoing server (at least not without having to interface to it or read mailq, which are nasty things to do).
DISCLAIMER
Given that even in the good case where you know for real the status you cannot know if the email has been received on the other end, nor how much time will it take, I'm not sure how useful such a construction would be. It'd certainly be fun to program, but I doubt it'd be really useful, maybe just some eye candy with standard email disclaimers (emails can be lost in transit, if it fails try again, leave sometime before retrying) would be enough.
I think the best option here is by estimating the time. You can test how much time some 10MB mails takes to be sent, to know the receiving speed of you SMTP server. With that information you can estimate transfer time of any email based on its size, and give your client some visual distraction based on that.
If you email process can send back information, then it may to possible to update a progress bar or progress text with the messages.
This is similiar to the way Wordpress upgrades/installs work. As the process is completed, text is displaying telling each step: "Downloading xxxx.xxx.xxx", "Deactivating Plugin", "Installing Plugin", "Attempting Reactivation", Reactivation successful": something similar. You would need a listener on the client side and a sender of messages on the server: as the script executes, it sends back messages to the client.
As said before, this can only realistically go as far as your server. You can signify if the mail left your server successfully, but without some sort of email reciever conformation step, I think that is as far as you can go.
I gues there us a chance to complete it, but can't be sure that this solution is done already by someone.
My thoughts:
As I know you can work with socket in non-blocking mode stream_set_blocking()
then you could try to use that approach to send emails via that non-blocking socket.

Categories