phpmailer and server maillog - php

for those who use phpmailer, and love it, I have a question about getting server response information, if possible.
The limit I've seen is that I can send an email, but there's no way to get a post-message-sent response from the mail server. I am running my own mail server, and I usually watch the maillog file to see what comes back. And there's some interesting responses from places like yahoo, cox, and other mail servers.
Has any one done anything cool to capture such responses, and tie them back to phpmailer events?
The only recourse I can come up with is to log the entries to the maillog into a database, and somehow match the to= and time sent to any emails I send out using phpmailer. But I find that's not quite accurate.
So, I'm open to any ideas.

You should look at message IDs; any bounces should contain the original. You can use custom headers to add another unique identifier, but there's no guarantee you'll get it back in a bounce. Failing that, you need to use VERP addressing in order to identify bounced messages accurately, and you can set that explicitly via the Sender property in PHPMailer.

Related

PHPMailer store finished mail

I'm searching for a good way to store the e-mail generated by PHPMailer,
in a database instead of sending it out directly via SMTP.
The reason for doing this is, that mails are getting sent via customer provided mail servers, which will probably be unreliable. So that's why I would like to queue mails in the database instead of directly sending them.
The obvious idea would be to store PHPMailers internal $MIMEHeader, $MIMEBody and similar variables and then later put them back into the PHPMailer object.
Unfortunatly this isn't really possible, because most of these variables have protected-access modifiers set on them without any other way of accessing them.
I thought about doing things like Reflection to change the access modifiers to public, but that sounds like a crude hack, which is prone to breaking...
Also: I don't really want to modify PHPMailer itself, to be able to still update PHPMailer.
Serializing the whole PHPMailer object was another idea, but that would be a little bit impractical, because I'd need to make sure that (for example) attachments in the filesystem are still there when sending the mail.
You can get hold of a complete message without sending it by doing this:
$mail->preSend();
$message = $mail->getSentMIMEMessage();
You can drive the SMTP class by itself if you want to submit a pre-built message, but it's not especially straightforward - you are better off following the other suggestions of storing the parameters rather than the pre-built message.
Being able to set headers and body directly would imply that PHPMailer contains a complete MIME parser, and that's just not its job. Also getting around the protected access is not sufficient because calling the send function would cause all your changes to be overwritten because the message is only built at send time (which is why those properties are protected in the first place - they are for internal use). In short, you're approaching this the wrong way.
As others have said though - you're queuing in the wrong place - this is what mail servers are for, they are very good at it, and you really don't want to write your own MTA. This is what I do, and I have no problem sending a million message per hour - my MTA takes care of all the reliability issues.
I'm searching for a good way to store the e-mail generated by PHPMailer
Perhaps it would be sufficient to store just data you build your mail content from (even as serialized arrays, JSON whatever). Then when needed, you can build the mail again and resend.
which will probably be unreliable
By the same logic your code will probably be buggy.
Jokes aside, once send() returned success, it's not your problem what will happen with the mail delivery. If it will fail, then it's not you who should fix it as the culprit is outside your code. Just add "reliable mail server" to your app's requirements :)

Catching emails with a script, then deleting

I need to create a system that will pipe incoming emails to a PHP script, then get rid of the email. Technically, I don't want these email addresses to actually exist. I would want to validate the email address by checking a database. So if I have a database with say 100 email addresses (i.e. 2323#mydomain.com), the PHP script would parse the email, store the contents in a database for later use, and then discard the email.
I piped emails to a PHP script in the past and parsed the email. However, my understanding of this requires the email address to exist.
I figure a catch-all account would be a bad idea, as SPAM could account for the majority of incoming emails, which I have no use for.
I could create the emails dynamically perhaps, if that is even possible with PHP, but again, I don't actually need to store the incoming email. Eventually the server would get bogged down with emails that I have no use for, especially since the contents would be stored in a database.
There will be no manual management of the incoming emails. Everything would be automated. While I'm sure I could use the PHP imap extension to delete emails say every 30 days, this seems unnecessary.
Any suggestions on the best setup for this?
Automation of email address creation or wildcard catch
Parsing of email, storing and discarding
Please have a look at a GitHub Project PHPMailer and the documentation is available here.
You can also try sendgrip, another good SMTP mailer for PHP.
These projects do not have a direct functions to delete e-mails, however, you will have to bake your own code extending some of the classes. Good Luck!
See http://harrybailey.com/2009/02/send-or-pipe-an-email-to-a-php-script/ for an excellent article that explains how to pipe incoming mail to a PHP script. You should be able to configure your MTA with a wildcard on your domain (so that it accepts mail to anyaddress#yourdomain.tld) and forward each incoming message to your PHP script. Your PHP script can then pickup the sender, recipient, subject, etc., query your database, and process the message accordingly.

imap_num_msg not detect forwarded emails

In php imap_num_msg not count how many mails are in inbox if there are forwarded mails. Also imap_headerinfo not display them.
I there a method to do that?
If you are having problems reading the headers, perhaps some of your messages are not correctly formatted. That may also explain why other functions, such as imap_num_msgs are not giving the expected answer.
If it is happening with forwarded messages, perhaps whatever is forwarding the messages is trashing the headers in the process. That is much more likely to happen if they are being forwarded by some kind of custom script, than if they are forwarded by a proper mail server.

Handling Incoming Mail to Multiple Recipients in PHP

Alright, this may take a moment or two to explain:
I'm working on creating an Email<>SMS Bridge (like Teleflip). I have a few set parameters to work in:
Dreamhost Webhosting
PHP 5 (without PEAR)
Postfix
MySQL (If Needed)
What I have right now, is a catch-all email address that forwards the email sent to a shell account. The shell account in turn forwards it to my PHP script.
The PHP script reads it, strips a few Email Headers in an effort to make sure it sends properly, then forwards it to the number specified as the recipient. 5551234567#sms.bridge.gvoms.com of course sends an SMS to +1 (555) 123-4567.
This works really well, as I am parsing the To field and grabbing just the email address it is sending to. However, what I realized that I did not account for is multiple recipients. For example, an email sent to both 5551234567 and 1235554567 (using the To line, the CC line, or any combination of those).
The way email works of course, is I get two emails received, end up parsing each of them separately, and 5551234567 ends up getting the same message twice.
What is the best way to handle this situation, so that each number specified in TO and CC can get one copy of the message.
In addition, though I doubt its possible: Is there a way to handle BCC the same way?
If you check the headers of the mail, you should find a Message-ID field (according to RFC2822 - section 3.6.4). So you could test if you have already sent an SMS for a mail with the same Message-ID & phone number to prevent sending the same message to the same number twice.
Why not use something like imap to check the catch-all mailbox, loop through the messages and then delete them once finished? That way you don't need to forward them to a seperate account.
Stupid dirty solution: parse all recipients from the mail, then send them SMS, then put em all into temporary table with md5 of message text. And check all incoming mails against this table.
Although wimvds had the best answer here, I found out elsewhere that Dreamhost includes a "X-DH-Original-To" header in the way I'm running it through the system. Using this, I'm able to send to each number individually upon receipt of the email without checking it against a database. This should also work with Blind Carbon Copy (I don't know the specifics of how email works enough to tell you how that works).

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