Drop Shadow

Mail Queue

Sometimes you need to have your PHP scripts send e-mails to yourself for notification. This plug-in will help you do it.

Never send e-mails to addresses you’ve collected over web forms! Unless you personally know that this particular address is valid and expects mail from USD, do not send it mail. Otherwise, you’ll be enabling web spam.

If you just want to send e-mail to yourself whenever someone fills out a web form, see Email Form. That plug-in uses this plug-in behind the scenes to automatically send submitted forms to an address you specify. This plug-in is for when you need more control than the automatic plug-in supports.

Queuing a message

The basic script portion is fairly simple:

include_once("/web/includes/mail.phpi"); $message = new EMail($recipients, $subject, $sender, $body); $message->queue();

This will queue up the message, and it will be sent within the next fifteen minutes.

If there is more than one recipient, use an array to set up that variable:

$recipients = array('user1@sandiego.edu', 'user2@sandiego.edu');

If you don’t want to set up a custom $body, and you are using the Form Verification plug-in, you can use $form->allFields() in place of $body. If you’re using the Easy SQL in PHP plug-in, use $sql->form->allFields().

Advanced use

Only the $recipients and $subject parameters are required. You can set up the $sender and $body later in your script.

$message = EMail($recipients, $subject); … $message->paragraph($line1); … $message->paragraph($line2); … $message->sender('youraddress'); $message->queue();

If you have an array of paragraphs to add, you can use $message->paragraphs() to add them.

More recipients

While you have to have at least one recipient in mind when you set up the message, you can add more recipients later.

$queue->recipient('user3@sandiego.edu'); … $morerecipients = array('user4@sandiego.edu', 'user5@sandiego.edu'); $queue->recipient($morerecipients);

Default senders

The system checks to make sure that your sender is valid. If it isn’t valid, then it won’t use that sender. However, senders are required in order to send a message.

If you can’t trust that your sender is going to be valid, you can set up a default sender:

$message->defaultSender('address@sandiego.edu', 'name');

Always use a known valid address for the default sender. If your default sender fails as well, your messages will not be sent.

Special headers

If you need to add special headers to your message, use the “headers()” method:

$message->headers('Reply-To: myalias@sandiego.edu');

You can also pass an array to the headers method.

Form verification

Don’t forget that you can use Form Verification to verify form input.

Long lines

E-mail messages can’t have any line longer than 1,000 characters; most servers will break lines well before that to avoid problems. The plug-in will normally leave your formatting alone, but if any line is longer than 850 characters it will autowrap all text to 75 characters per line.