sidebar
The PHP mail() function can be problematic on IBM i. Sometimes it works fine, other times it seems to not work at all. When it does not work, it can be very difficult or even impossible to determine why. Fortunately, the SMTP Transport found in Zend Framework has proven to be very reliable, and it is easy to implement.
This article presents an example written with Zend Framework 1, and another example written with Zend Framework 2.
Environment
Any version of Zend Server for IBM i, running on any supported version of IBM i.
Make sure you have Zend Framework installed
Replace some values in the examples Framework 1 example script: The above example assumes you have the Framework directory in your include path, which is a default for Zend Server. If needed, please add a line to include Framework in the path: References: Framework 2 example script: References:<?php
ini_set('include_path',ini_get('include_path').':/usr/local/zendphp7/var/libraries/ZendFramework_1/');
require_once('Zend/Mail.php');
require_once 'Zend/Mail/Transport/Smtp.php';
$tr = new Zend_Mail_Transport_Smtp('mail.example.com');
Zend_Mail::setDefaultTransport($tr);
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('somebody@example.com', 'Some Sender');
$mail->addTo('somebody_else@example.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send();
?>
ini_set('include_path',ini_get('include_path').':/usr/local/zendphp7/var/libraries/ZendFramework_1/');
Introduction - Zend_Mail (ZF 1.12)
Sending via SMTP - Zend_Mail (ZF 1.12)<?php
//ini_set('include_path', '/usr/local/zendphp7/share/ZendFramework2/library');
ini_set('include_path', ini_get('include_path').':/usr/local/zendphp7/var/libraries/ZendFramework_2/');
require_once 'Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));
$loader->register();
use Zend\Mail\Message;
use Zend\Mail\Transport\Smtp as SmtpTransport;
use Zend\Mail\Transport\SmtpOptions;
$message = new Message();
$message->addTo('somebody_else@example.com')
->addFrom('somebody@example.com')
->setSubject('TestSubject')
->setBody("This is the text of the mail.");
// Setup SMTP transport
$transport = new SmtpTransport();
$options = new SmtpOptions(array(
'name' => 'example.com',
'host' => 'mail.example.com',
'port' => 25,
));
$transport->setOptions($options);
$transport->send($message);
?>
Zend\Mail\Transport — Zend Framework 2 2.4
Zend\Mail\Transport\SmtpOptions — Zend Framework 2 2.4