Queue email messages in files to send them later
This is a simple app for sending emails through PHP without blocking sending and waiting answer from the SMTP server. It provides some classes as:
Messageto compose the message
Serverto connect to the SMTP server
Senderto run and send the queued messages
Runnerto fire a Sender object
First we need the server configurations. There is a sample file on the
host = smtp.gmail.com port = 587 user = taq password = secret path = /tmp/delayedmailtest
The only different parameter there is the
path parameter. This is where the
mail files will be stored.
If you use two-phase authentication on a Gmail account, sending emails with this library will not work. You need a less secure account to make it works.
The data store used are just regular plain text files. They are stored on the
path configured above. On that dir there will be another two subdirs:
deliverywhere the queued messages are.
sent, where the messages are moved after
error, where the messages are moved if there are some errors on them.
Just include the
delayedmail.php on your app, create a new
configure it, compose and queue a new message:
<?php include_once "delayedmail.php"; $server = new DelayedMail\Server("myconfigs.ini"); $msg = new DelayedMail\Message(); $msg->from("taq <email@example.com>")-> to("Eustaquio Rangel <firstname.lastname@example.org>")-> cc("Eustaquio Rangel <email@example.com>")-> subject("DelayedMail test!")-> text("This is just\na test!")-> attach("taq.jpg"); $server->push($msg); ?>
If you check the
delivery dir now, there will be a file there with the message
You can use arrays on
Just edit the
runner.php file with the desired interval and configuration file
(usually the same config file as the server) and run it from the command line:
<?php $dir = dirname(__FILE__); echo "- loading classes from $dir\n"; include_once "$dir/delayedmail.php"; $sender = new DelayedMail\Sender(5,"delayedmail.ini"); $sender->run(); ?>
$ php runner.php - initializing ... - checking for files in /tmp/delayedmailtest/delivery ... - no files found.