views:

272

answers:

3

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?

A: 

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.

Havenard
This is something i did consider but unfortuately the net connection at my workplace is very unpredictable because of traffic management.
Gary Willoughby
Many professional programs do that. Browsers, CD burners, they often display "100%" without getting job done, or get it done before the end... it is completely normal, but who cares :P
Havenard
strange advice :-(
twk
+2  A: 

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.

Vinko Vrsalovic
A: 

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.

Robert DeBoer