07
Thu, Nov
7 New Articles

TechTip: Use PHP and Zend_Mail to Send Email

Web Languages
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Do yourself a big favor. Replace QTMMSENDMAIL and SNDDST.

 

At the recent COMMON Conference in Reno, I went to just about all of the sessions related to PHP, Zend Framework, and MySQL. I got a lot of good information and made a bunch of new friends. Most of the PHP and MySQL sessions were standing-room-only, which was great to see. Yet I ran into too many people who were still on the fence, stalling rather than embracing these powerful new tools. This article is a direct response to those people.

 

Since I was a boy, I have been told to always use the right tool for the job. Today, that advice rings at least as true as it did when I was a boy rebuilding car engines with my dad. As a developer, using the right tool means knowing the strengths of each language in your toolbox. RPG does a lot of things extremely well, but email is not one of them. On the other hand, PHP handles email very well. The Zend_Mail class in Zend Framework handles it even better still. For example, if you are sending emails with one or more attachments, Zend_Mail offers a vastly simpler solution than SNDDST or QTMMSENDMAIL. Let's see how it works.

 

The first thing you'll need to know is how to use PHP-CLI, the PHP Command Line Interface. PHP-CLI is simply a way to run a PHP script from a command line--in this case, the QShell (QSH) command line on the IBM i (iSeries, AS/400, et al). The example below shows how you might execute a PHP script from the QSH command line:

 

php-cli /www/zendcore/htdocs/scriptName.php    

 

PHP-CLI is the "command" to execute. If your PHP server were running on a Windows machine, the command would be PHP-CLI.exe. The path and filename that follow identify the PHP script to be executed. Any parameters to be passed to the script would be separated by a blank space and would follow the script filename and path. See the example below:

 

php-cli /www/zendcore/htdocs/scriptName.php "parm 1" "parm 2" 3

 

In the example, the first two parameters are enclosed in quotes because they contain embedded blanks. We will use PHP-CLI to execute our email script directly from an RPG program by using the QSH command via the QCMDEXC command interface. But we are getting ahead of ourselves.

 

The next thing we will do is write a PHP script that will accept up to six parameters and then use those parameters to send an email. The code for this script is shown below:

 

<?php

/**********************************************************************  

 * Send Email script

 *

 * This script will accept parameters from the command line and

 * use those parameters to build and send an email.

 *

 * @author Jeff Olen - Olen Business Consulting, Inc

 *

 *

 * @param     String argument value 1 - Subject text for the email

 * @param     String argument value 2 - Email recipient addresses  

 *                      comma separated

 * @param     String argument value 3 - Email attachment

 *                      filename(s)/path(s) comma separated. Leave

 *                      blank if no attachments.

 * @param     String argument value 4 - This is the body of the

 *                      email and must be normal text not HTML.

 * @param     String argument value 5 - Email carbon copy (CC)

 *                      addresses comma separated. Leave blank if no

 *                      attachments.

 * @param     String argument value 6 - Email blind carbon copy

 *                      (BCC) addresses comma separated. Leave blank if

 *                      no attachments.

 *

 *      

 ********************************************************************

*/

 

// set error display and reporting level

// (remove after test and debugging!!)

ini_set("display_errors",1 );

error_reporting(E_ALL);

 

// load parameters to variables

$subject      = $_SERVER['argv'][1];

$toEmail      = $_SERVER['argv'][2];

$attachments  = $_SERVER['argv'][3];

$body         = $_SERVER['argv'][4];

 

if (isset($_SERVER['argv'][5])) {

       $ccEmail = $_SERVER['argv'][5];

       var_dump($ccEmail);

}

 

if (isset($_SERVER['argv'][6])) {

       $bccEmail = $_SERVER['argv'][6];

}

 

// load the mail class

require_once('Zend/Mail.php');

require_once 'Zend/Mail/Transport/Smtp.php';

 

// set the transport to SMTP

$tr = new Zend_Mail_Transport_Smtp('localhost');

Zend_Mail::setDefaultTransport($tr);

 

// create the mail object

if (!($mail = new Zend_Mail())) {

       die("failed to create Zend_Mail object. ");

}

 

// set the email text

$mail->setBodyText($body);

 

// set the subject

$mail->setSubject($subject);

 

// set sender

$mail->setFrom(This email address is being protected from spambots. You need JavaScript enabled to view it.', "Jeff Olen");

 

// add the TO email address(es)

$toAddresses = explode(',',$toEmail);

foreach ($toAddresses as $address) {

       $mail->addTo(trim($address));

}

 

// add the CC email address(es) (if any)

if (isset($ccEmail)) {

       $toAddresses = explode(',',$ccEmail);

       foreach ($toAddresses as $address) {

              $mail->addCc(trim($address));

       }

}

 

// add the BCC email address(es) (if any)

if (isset($bccEmail)) {

       $toAddresses = explode(',',$bccEmail);

       foreach ($toAddresses as $address) {

              $mail->addBcc(trim($address));

       }

}

 

// add the attachments (if any)

if (strlen(trim($attachments)) > 0) {

       $attachFiles = explode(',',$attachments);

       foreach ($attachFiles as $Filename) {

              $fileContents = file_get_contents(trim($Filename));

              $at = $mail->createAttachment($fileContents);

              $at->filename = trim($Filename);

       }

}

 

 

// send the email

$mail->send();

 

?>

 

This code is fairly straightforward. The parameters to be passed are clearly defined in the PHPDoc comments at the top. Since most of the parameters can have embedded blanks, we will want to enclose them in quotes when we paste together the command.

 

We could examine each piece of the code and describe the purpose of it. However, in the interest keeping this article to a reasonable size, I'll leave you to decipher most of it your self. The sole exception is the following code:

 

// set the transport to SMTP

$tr = new Zend_Mail_Transport_Smtp('localhost');

Zend_Mail::setDefaultTransport($tr);

 

In the code above, we are using some object-oriented (OO) coding techniques to set the default email transport to SMTP and set the SMTP server to localhost. If you are unfamiliar with OO, you are welcome to use this code without understanding exactly how it does what it does. As a friend once told me, "You can use object-oriented techniques in your PHP code, but you don't have to." And it's true. You can use completely procedural PHP without any trouble whatsoever. Then, when you want to, you can start using object-oriented. No problem.

 

The reason we need to set the default email transport is that by default the Zend_Mail class will attempt to use the UNIX-based SENDMAIL command to send email. Since the IBM i doesn't usually have SENDMAIL installed, we are overriding the default setting and forcing it to use the standard IBM i TCP SMTP server. This means that you will need to have the IBM i TCP SMTP server running and correctly configured in order for this script to function correctly.  A simple test to determine if your IBM i is correctly configured is to attempt to send an email using SNDDST. If it works, then all is well.

 

Once you have the PHP script written, you need to write the RPG program or service program that you will call to send the email(s). I have created a standalone program to show you how this works, but it could easily be converted to a service program procedure. The code is shown below:

 

H OPTION(*NODEBUGIO:*SRCSTMT) DFTACTGRP(*NO)  actgrp(*caller)                

  // ----------------------------------------------------------------------  

  // Program name: EMAILSRVC                                                  

  // Description : Provides email service via PHP/Zend_Mail                  

  // ----------------------------------------------------------------------  

  // ----------------------------------------------------------------------   

  // Parameters:                                                             

  //                                                                         

  //   inSubject      - string to be used as the subject of the email.       

  //   inToAddresses  - String of email addresses separated by commas        

  //                      to be used as the TO: email addresses.             

  //   inAttachFiles  - String of IFS files (with path) to be attached       

  //                      to the email as attachments.                       

  //   inBodyText     - String of plain text to be used as the body of       

  //                      the email.                                         

  //   inCCAddresses  - (optional) String of email addresses separated by    

  //                      commas to be used as the CC: email addresses.      

  //   inToAddresses  - (optional) String of email addresses separated by    

  //                      commas to be used as the BCC: email addresses.   

  //                                                                      

  // ----------------------------------------------------------------------

  // Modifications:                                                       

  // ----------------------------------------------------------------------

  // 05/02/09 Jeff Olen - Olen Business Consulting, Inc.                  

  //          initial version                                             

  // ----------------------------------------------------------------------

                                                                          

d EMailSrvc       PR                  extpgm('EMAILSRVC')                 

d  inSubject                   125a   const                               

d  inToAddresses...                                                       

d                            32767a   options(*varsize) const             

d  inAttachFiles...                                                       

d                            32767a   options(*varsize) const             

d  inBodyText                32767a   options(*varsize) const             

d  inCCAddresses...                                                       

d                            32767a   options(*nopass:*varsize) const     

d  inBCCAddresses...                                                      

d                            32767a   options(*nopass:*varsize) const     

                                                                           

                                                                            

d EMailSrvc       PI                                                       

d  inSubject                   125a   const                                

d  inToAddresses...                                                         

d                            32767a   options(*varsize) const              

d  inAttachFiles...                                                        

d                            32767a   options(*varsize) const               

d  inBodyText                32767a   options(*varsize) const              

d  inCCAddresses...                                                        

d                            32767a   options(*nopass:*varsize) const      

d  inBCCAddresses...                                                       

d                            32767a   options(*nopass:*varsize) const      

                                                                           

  // -----------------------------------------------------------------------

  //  External procedure prototypes                                        

  // -----------------------------------------------------------------------

                                                                            

d runCommand      pr                  extpgm('QCMDEXC')                    

d  cmdString                 32767a   options(*varsize) const              

d  cmdLength                    15p 5 const                                  

                                                                              

  // -----------------------------------------------------------------------

  //  Global variables

  // -----------------------------------------------------------------------

                                                                              

d cmdString       s          32767a   varying                                

                                                                             

d wkToAddresses   s          32767a                                          

d wkAttachFiles   s          32767a                                          

d wkBodyText      s          32767a                                          

d wkCCAddresses   s          32767a                                           

d wkBCCAddresses  s          32767a                                          

                                                                             

                                                                              

d PHP_CLI_PATH    C                   '/usr/local/Zend/Core/bin/php-cli'     

                                                                             

d ZEND_MAIL_SCRIPT_PATH...                                                   

d                 C                   '/www/zendcore/htdocs/jeffo/misc_scr-  

d                                     ipts/TestZendMail.php'                 

                                                                             

d SINGLE_QUOTE    C                   ''''                                   

d DBL_QUOTE       C                   ''''''                                 

                                                                            

 /free                                                                       

  // -----------------------------------------------------------------------

  //  Mainline

  // -----------------------------------------------------------------------

                                                                             

  wkToAddresses = %subst(inToAddresses:1:%len(%trim(inToAddresses)));       

  wkAttachFiles = %subst(inAttachFiles:1:%len(%trim(inAttachFiles)));       

  wkBodyText    = %subst(inBodyText:1:%len(%trim(inBodyText)));              

                                                                            

  if %parms >= 5;                                                           

     wkCCAddresses = %subst(inCCAddresses:1:%len(%trim(inCCAddresses)));    

  endif;                                                                     

                                                                            

  if %parms >= 6;                                                           

     wkBCCAddresses = %subst(inBCCAddresses:1:%len(%trim(inBCCAddresses))); 

  endif;                                                                    

                                                                            

  cmdString = 'QSH CMD(' + SINGLE_QUOTE +                                    

            DBL_QUOTE + PHP_CLI_PATH + DBL_QUOTE + ' ' +                    

            DBL_QUOTE + ZEND_MAIL_SCRIPT_PATH   + DBL_QUOTE + ' ' +         

            DBL_QUOTE + %trim(inSubject)        + DBL_QUOTE + ' ' +         

            DBL_QUOTE + %trim(wkToAddresses)    + DBL_QUOTE + ' ' +         

            DBL_QUOTE + %trim(wkAttachFiles)    + DBL_QUOTE + ' ' +   

            DBL_QUOTE + %trim(wkBodyText)       + DBL_QUOTE;          

                                                                       

  if %parms >= 5;                                                     

     cmdString += ' ' + DBL_QUOTE + %trim(wkCCAddresses) + DBL_QUOTE; 

  endif;                                                              

                                                                       

  if %parms >= 6;                                                     

     cmdString += ' ' + DBL_QUOTE + %trim(wkBCCAddresses) + DBL_QUOTE;

  endif;                                                               

                                                                      

  cmdString += SINGLE_QUOTE + ')';                                    

                                                                      

  callp runCommand ( cmdString : %len(%trim(cmdString)) );            

                                                                      

  *inlr = *on;                                                        

  return;                                                              

 /end-free                                                            

 

Since I am guessing that you are already an RPG master (you are, aren't you?), I am not going to review the code in detail. I encourage you to pick it apart yourself.

 

So with this RPG code in place, all you need to do is format your parameters as described in the RPG program (or the PHPDoc comments in the PHP script) and call the EMAILSRVC program. Please note that you cannot call the EMAILSRVC program from the command line because of the size of the parameters. It can only be called from another program. You could, however, code a command to run it as the command processing program (CPP). The PHP script will attach the specified attachments from the IFS, converting them to BASE64 as necessary, and then send the email. Anyone who has ever spent the time to decipher and use QTMMSENDMAIL will join me in celebrating the simplicity of this solution.

 

Last but not least, I'll leave you with a CLLE program that actually calls the EMAILSRVC program with parameters specified.

 

PGM                                                             

                                                                

DCL        VAR(&SUBJECT) TYPE(*CHAR) LEN(125) +                 

             VALUE('Test subject')                              

                                                                

DCL        VAR(&ToAddrs) TYPE(*CHAR) LEN(32767) +               

             VALUE(This email address is being protected from spambots. You need JavaScript enabled to view it.')                           

                                                                

DCL        VAR(&CCAddrs) TYPE(*CHAR) LEN(32767) +               

             VALUE(This email address is being protected from spambots. You need JavaScript enabled to view it.')                         

                                                                 

DCL        VAR(&BCCAddrs) TYPE(*CHAR) LEN(32767) +              

             VALUE(This email address is being protected from spambots. You need JavaScript enabled to view it.')                     

                                                                

DCL        VAR(&Attach) TYPE(*CHAR) LEN(32767) +                

         VALUE('/home/jolen/DNC_2009_05_1.txt.zip, +

                /home/jolen/TechTip_2009_05.doc') 

                                                                

DCL        VAR(&Body)   TYPE(*CHAR) LEN(32767) +                 

.+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+...

             VALUE('Here's your DNC zip file and the +

                Techtip you asked for.')                       

                                                      

CALL       PGM(EMAILSRVC) parm(&subject &ToAddrs  +   

            &Attach &Body &ccAddrs &bccAddrs)         

                                                      

ENDPGM                                                

 

Enjoy!

 

 

Jeff Olen

Jeff Olen is a super-spy now but keeps his cover identity intact by working for video game studios on the East Coast. So when he’s not out killing members of ISIS or rescuing refugees, you can find him playing Wolfenstein II or testing the new Fallout 76 releases at his beach house in Costa Rica. In any case, he can’t be reached. You can email his cat at This email address is being protected from spambots. You need JavaScript enabled to view it.. She will pass on your message…if she feels like it.


MC Press books written by Jeff Olen available now on the MC Press Bookstore.

The IBM i Programmer’s Guide to PHP The IBM i Programmer’s Guide to PHP
Get the scoop on how PHP can—and should—be deployed on IBM systems.
List Price $79.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: