Search code examples
phpemailsmtppear

Read email from file and send using PEAR's Mail_Mime library


I've come across a few issues when sending mail in our php-based email application, specifically the rate at which our mail server will accept incoming mail. To try overcome this particular problem, and also to preempt more general SMTP issues, I'm trying to write an email to file for later sending if the SMTP server returns an error.

I'm currently using the PEAR Mail_Mime library to send mail. From what I've read in the docs, it's possible to save a message to file using the saveMessage method. My problem comes in where my PHP script needs to read that message and attempt to send it again at a later stage. Short of manually parsing the file and looking for the various headers "FROM", "TO", etc, and then manually reconstructing the entire mail object from scratch, is there any function in the Mail_Mime library that allows me to read the entire file at once and reconstruct the various parts and headers automatically?


Solution

    1. Serialize your constructed mail object
    2. Insert it into some form of queue (database, directory full of files containing 1 message each, beanstalkd)
    3. daemon reads entries from the queue one at a time
    4. Deserialize
    5. Send message
    6. sleep until next allowed send (1 second? 30 seconds? 60 seconds?)