Email sending with PHP

In order to send Emails with PHP it is neccesary to use the SMTP protocol and not the builtin mail() function from PHP. The mail() function is not available in our PHP Containers.

There are many ways of sending mails via SMTP under PHP. Good choices are PHPmailer, SwiftMailer and PEAR's Mail. PEAR's Mail is preinstalled in our PHP containers and can be used directly.

Example with PHPMailer

require_once('../class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth   = true;
$mail->SMTPSecure = "tls";
$mail->Host       = "smtp.mx.pyrox.eu";
$mail->Username   = "website@example.com";
$mail->Password   = "super-secure-password";
$mail->From       = "website@example.com";
$mail->FromName   = "Example Website";
$mail->To         = "john.doe@example.com";
$mail->Subject    = "Test Email";
$mail->Body       = "Test\n1\n2\n3";
$mail->Send();

Example with SwiftMailer

require_once 'lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance()
    ->setHost('smtp.mx.pyrox.eu')
    ->setPort(25)
    ->setEncryption('tls')
    ->setUsername('website@example.com')
    ->setPassword('super-secure-password');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
    ->setFrom(array('website@example.com')
    ->setTo(array('john.doe@example.com')
    ->setSubject('Test Email')
    ->setBody('Test\n1\n2\n3');
$mailer->send($message);

Example with PEAR's Mail

require_once "Mail.php";
$smtp = Mail::factory('smtp', array(
    'host'     => "smtp.mx.pyrox.eu",
    'auth'     => true,
    'username' => "website@example.com",
    'password' => "super-secure-password"));
$headers = array(
    'From'    => "website@example.com",
    'To'      => "john.doe@example.com",
    'Subject' => "Example Email");
$body = "Test\n1\n2\n3";
$smtp->send($to, $headers, $body);