Sending an email is one of the most common tasks encountered.
PHP has a one-line
mail∞ function that does the job. This is one of the biggest asset to the PHP language.
Because sending a mail using the mail() function may use slightly heavy resources, an alternative is to use a 3rd party PHP script that can handle sending of mails using different methods like
SMTP∞ and handle errors and queuing. The most common open-source PHP Mailer is
PHPMailer∞. Another recent, more update 3rd party solution is
SwiftMailer∞, which is OOP oriented and provides separate packages for PHP versions 4 and 5 respectively.
Note that ASO's servers use port 26 for SMTP and requires authentication.
Example: Sending a newsletter email using SwiftMailer
<?php
$subj = "Subject Here";
$body = '<html><head><title>Page Title</title></head><body><table border="1" align="center"><tr><td>cell 1</td><td>cell 2</td></tr></table></body></html>';
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
$smtp =& new Swift_Connection_SMTP("mail.domain.com", 26);
$smtp->setUsername("username+domain.com");
$smtp->setpassword("password");
$swift =& new Swift($smtp);
$message = new Swift_Message($subj, $body);
$message->setContentType("text/html");
$recipients = new Swift_RecipientList();
$recipients->addTo("email_address_1@yahoo.com");
$recipients->addTo("email_address_2@gmail.com");
$recipients->addTo("email_address_3@hotmail.com");
$swift->batchSend($message, $recipients, new Swift_Address("myusername@domain.com", "My Name"));
?>
There are no comments on this page. [Add comment]