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.
Any version of Zend Server for IBM i, running on any supported version of IBM i.
Make sure you have Zend Framework installed
Before you start, please make sure you have Zend Framework installed. In earlier versions of Zend Server, these libraries were usually installed automatically. Starting with version 2018, you need to install them. Please go to this article to learn how: Zend Server for IBM i - Zend Mail Transport(SMTP)
Replace some values in the examples
In the following examples, we have used some dummy values that can be replaced with the real values for the mail to be sent. We specified 'mail.example.com' as the mail server, 'firstname.lastname@example.org' as the email of the person sending the email, 'email@example.com' as the email of the person to receive the email, 'TestSubject' as the subject line of the email, and 'This is the text of the mail.' as the text for the email. We suggest creating a simple test script with just these values replaced with some real values, to verify that you can send a simple email. Then, you can visit the reference links provided to learn how to do more, like add recipients, include attachments, authentication, and so on.
Framework 1 example script:
$tr = new Zend_Mail_Transport_Smtp('mail.example.com');
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('firstname.lastname@example.org', 'Some Sender');
$mail->addTo('email@example.com', 'Some Recipient');
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:
Introduction - Zend_Mail (ZF 1.12)
Sending via SMTP - Zend_Mail (ZF 1.12)
Framework 2 example script:
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));
use Zend\Mail\Transport\Smtp as SmtpTransport;
// Important: Code above this comment must be placed in the outermost scope of the file, preferably right at the top.
// Code below this comment may be placed in a function, provided the function runs after the "use" statements have completed.
$message = new Message();
->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,
Zend\Mail\Transport — Zend Framework 2 2.4
Zend\Mail\Transport\SmtpOptions — Zend Framework 2 2.4