Date: Fri, 29 Mar 2024 06:16:02 +0000 (UTC) Message-ID: <2047139366.446.1711692962627@library.aws.roguewave.com> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_445_2044061370.1711692962625" ------=_Part_445_2044061370.1711692962625 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
The PHP mail() function can be problematic on IBM i. Somet= imes 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 anot= her example written with Zend Framework 2.
Environment
Any version of Zend Server for IBM i, running on any supported version o= f IBM i.
Make sure you have Zend Framework inst= alled
Replace some values in the examples
Adjust your paths to use earlier versi= ons
The path to Zend Server 2021 begins with /usr/local/zendphp74. For= versions 9 and 2019, you should change zendphp74 to zendphp7. For ve= rsions 6 to 8.5, you should change zendphp74 to zendsvr6.
Framework 1 example script:
<?php ini_set('include_path',ini_get('include_path').':/usr/local/zendphp74/var/l= ibraries/ZendFramework_1/'); require_once('Zend/Mail.php'); require_once 'Zend/Mail/Transport/Smtp.php'; $tr =3D new Zend_Mail_Transport_Smtp('mail.example.com'); Zend_Mail::setDefaultTransport($tr); $mail =3D 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(); ?>
The above example assumes you have the Framework directory in your inclu= de path, which is a default for Zend Server. If needed, please add a = line to include Framework in the path:
ini_set('inclu= de_path',ini_get('include_path').':/usr/local/zendphp74/var/libraries/ZendF= ramework_1/');
References:
Introducti=
on - Zend_Mail (ZF 1.12)
S=
ending via SMTP - Zend_Mail (ZF 1.12)
Framework 2 example script:
<?php //ini_set('include_path', '/usr/local/zendphp74/share/ZendFramework2/librar= y');=20 ini_set('include_path', ini_get('include_path').':/usr/local/zendphp74/var/= libraries/Zend_Framework_2/default/library'); require_once 'Zend/Loader/StandardAutoloader.php'; $loader =3D new Zend\Loader\StandardAutoloader(array('autoregister_zf' =3D&= gt; true)); $loader->register(); use Zend\Mail\Message; use Zend\Mail\Transport\Smtp as SmtpTransport; use Zend\Mail\Transport\SmtpOptions; // 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 functi= on runs after the "use" statements have completed. $message =3D 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 =3D new SmtpTransport(); $options =3D new SmtpOptions(array( 'name' =3D> 'example.com', 'host' =3D> 'mail.example.com', 'port' =3D> 25, )); $transport->setOptions($options); $transport->send($message); ?>
References:
Zend\=
Mail\Transport =E2=80=94 Zend Framework 2 2.4
Zend\Mail\Transport\SmtpOptions =E2=80=94 =
Zend Framework 2 2.4