TechTip: Email Made Simple(r) with PHP and Pear PDF Print E-mail
Tips & Techniques - Web Languages
Written by Jeff Olen   
Thursday, 13 May 2010 23:00

Support MC Press - Visit Our Sponsors

Forums Sponsor

POPULAR FORUMS

Forums

 Search Sponsor

 

POPULAR SEARCHES

Search

Wouldn't it be great if we could bypass the IBM i SMTP and POP servers altogether? Well, we can!

 

I've written several TechTips related to improving your ability to send email effectively from your IBM i (See "TechTip: Use PHP and Zend_Mail to Send Email" and "TechTip: Using the QTMMSENDMAIL API"). This TechTip is the latest installment in that genre. It had to be done because the tools just keep getting better and easier to use.

 

The first piece of good news is for all of you who are not experts with the SMTP and POP server configuration on IBM i. You will not be using the IBM i SMTP and/or POP server! (Go back and read that last sentence again; I think it bears repeating.) That's right! We're going to bypass the IBM i servers altogether. The reason for this is twofold. First, it allows you to avoid the headaches of configuring them. Second, it allows you to use an external email server (Gmail, AIM, or Lavabit, for example) to forward all your outgoing emails from the IBM i. What we're going to do is write an email script that connects to your external email server the same way that Outlook does: using SSL ports.

 

The first thing you'll need is to have PHP installed on your IBM i. Next, you'll need to install a couple of Pear extensions (unless you already have them): the Mail extension and the Mail_Mime extension. These extend the normal PHP email functionality quite a bit. If you're familiar with Pear extensions and the Pear installer, then you can use the installer to install the extensions. However, if you aren't, just copy the files contained in the Mail-1.2.0.tgz and Mail_Mime-1.6.0.tgz files to the IFS directories shown below:

 

Mail-1.2.0.tgz                à  /usr/local/Zend/Core/share/pear/Mail

Mail_Mime-1.6.0            à  /usr/local/Zend/Core/share/pear/Mail_Mime

 

Once you have done this, we're ready to start coding our script to send email messages. The code is pretty easy to follow, but it does use classes and object-oriented methodologies. As always, if you are inexperienced with classes and OO, then I highly recommend that you take some time to familiarize yourself with them. (I'm stepping off my soapbox now.)

 

The code below will send a very simple email:

 

<?php

 

// setup error reporting for testing/development

ini_set("ERROR_REPORTING", E_ALL);

error_reporting(E_ALL);

 

// set include path to include PEAR directory

ini_set("include_path", ini_get("include_path").":/usr/local/Zend/Core/share/pear");

 

// require the PEAR Mail and Mail_Mime classes

require_once "Mail/Mail.php";

require_once "Mail_Mime/mime.php";

 

// setup external mail connection parameters

$host = "ssl://smtp.gmail.com";

$port = "465";

$username = "username";

$password = "password";

 

// load recipient and subject

$to = "jeff_olen@hotmail.com";

$subject = "test email";

 

// Text body

$bodyText = "This is the body text of the email.";           

 

// HTML body

//$bodyHTML = "<html><h3><font color='red'><i>This is the HTML text of the email</i></font></h3></html>";

 

 

$message = new Mail_Mime();

$message->setTXTBody($bodyText);

 

// $message->setHTMLBody($bodyHTML);

 

$body = $message->get();

 

$extraheaders = array("From"=>"Jeff Olen <jmo@olen-inc.com>", "Subject"=>$subject);

$headers = $message->headers($extraheaders);

 

$mail = Mail::factory('smtp',

  array ('host' => $host,

    'port' => $port,

    'auth' => true,

    'username' => $username,

    'password' => $password));

   

$rs = $mail->send($to, $headers, $body);

 

if (PEAR::isError($rs)) {

  echo("<p>" . $rs->getMessage() . "</p>");

 } else {

  echo("<p>Message successfully sent!</p>");

 }

 

Let's go through each step.

 

First, we're preparing for testing and development by setting up to report all errors.

 

<?php

 

// setup error reporting for testing/development

ini_set("ERROR_REPORTING", E_ALL);

error_reporting(E_ALL);

 

 

Next, we're adding the PEAR directory to the include path. This is a list of directories that will be searched for INCLUDEd or REQUIREd files. Then, we actually pull in the PEAR modules we need using the REQUIRE_ONCE function.

 

// set include path to include PEAR directory

ini_set("include_path", ini_get("include_path").":/usr/local/Zend/Core/share/pear");

 

// require the PEAR Mail and Mail_Mime classes

require_once "Mail/Mail.php";

require_once "Mail_Mime/mime.php";

 

Now, we set up the mail connection parameters. Note that the code shown is not the recommended way to do this. We are only including the username and password in the clear as an example. This information should be pulled in from a secured script using a REQUIRE function.

 

// setup external mail connection parameters

$host = "ssl://smtp.gmail.com";

$port = "465";

$username = "username";

$password = "password";

 

If you're using Gmail, these are the actual host and port that you'll need. If you're using a different email provider, you'll need to check with that provider for the actual settings. Or if you have the correct setup in Outlook, you can get the information from there.

 

Now, we load the recipient email address and subject line.

 

Then, we have two options for the body of our email; either plain text or HTML. If you want to use HTML for your message body, you'll need to use the setHTMLBody method instead of the setTXTBody method. I have included that code as well, but it is commented out.

 

The last thing we do in this section of code is use the Mail_Mime method "get" to load the mail message body in the variable called $body.

 

// load recipient and subject

$to = "jeff_olen@hotmail.com";

$subject = "test email";

 

// Text body

$bodyText = "This is the body text of the email.";           

 

// HTML body

//$bodyHTML = "<html><h3><font color='red'><i>This is the HTML text of the email</i></font></h3></html>";

 

 

$message = new Mail_Mime();

$message->setTXTBody($bodyText);

// $message->setHTMLBody($bodyHTML);

 

$body = $message->get();

 

In the next section of code, we put together some header information. This is similar to the Zend_Mail header information from the previous article. We're just setting up the "From" line and "Subject" line for the email to be sent. Then, we use the Mail_Mime function "headers" to output the actual mime headers and place them in the $headers variable.

 

Now, we create a Mail object called $mail. To do this, we are using the scope resolution operator (the double-colon in Mail::factory) to access and override properties in the Mail class. In this case, we want to use the SMTP mail driver, and we want to use the parameter values listed in the array that follows. This returns the $mail object, which we then use to execute the actual mail send.

 

Lastly, we check to make sure the email was sent successfully and echo an appropriate response.

 

$extraheaders = array("From"=>"Jeff Olen <jmo@olen-inc.com>", "Subject"=>$subject);

$headers = $message->headers($extraheaders);

 

$mail = Mail::factory('smtp',

  array ('host' => $host,

    'port' => $port,

    'auth' => true,

    'username' => $username,

    'password' => $password));

   

$rs = $mail->send($to, $headers, $body);

 

if (PEAR::isError($rs)) {

  echo("<p>" . $rs->getMessage() . "</p>");

 } else {

  echo("<p>Message successfully sent!</p>");

 }

 

The Mail object method "send" has three parameters, which are described below:

 

Parmeter 1: Mail recipient(s)—In our example, we had just one recipient, so this parameter was a simple string. We could have just as easily loaded an array with email addresses and used that as parameter 1. For example: 

 

$recipients[] = "scott@aol.com";

$recipients[] = "jeff@gmail.com";

$recipients[] = "catherine@yahoo.com";

...

...

...

$rs = $mail->send($recipients, $headers, $body);

 

Parameter 2: The headers—This is the additional header information including the "From" email address and Subject line.

 

Parameter 3: The actual body of the email.

 

That's it for our example script. But let me offer you a few tips on ways to extend the functionality of this script. First, as I mentioned in the section about the Mail "send" parameters, you can send to multiple recipients. You can also carbon copy emails as shown below, but you must add the extra headers to determine which email addresses are just normal "to" and which are "cc."

 

$recipients[] = "scott@aol.com";

$recipients[] = "jeff@gmail.com";

$recipients[] = "catherine@yahoo.com";

...

$extraheaders = array("From"=>"Jeff Olen <jmo@olen-inc.com>", "Subject"=>$subject, "To"=>"scott@aol.com",

"Cc"=>"jeff@gmail.com,catherine@yahoo.com");

...

...

 

$rs = $mail->send($recipients, $headers, $body);

 

Also, you can include attachments easily.

 

$file = "/yourfilepath/filename.ext";

...

...

...

$message->setTXTBody($bodyText);

$message->addAttachment($file);

$body = $message->get();

 

But if you're really going to use this script to send email from your IBM I, you'll need to write a program that executes the script using the PHP command line interface (PHP-CLI). This done using QSH (refer to "TechTip: Use PHP and Zend_Mail to Send Email"  for an example). You'll also probably want to add some parameters so you can send generic emails. Parameters to pass might include a list of recipients, a Subject line, body text, a list of email addresses to CC, and a list of files to attach. Good luck. Now…you've got mail. 

 

 


Jeff Olen
About the Author:
Jeff Olen is co-founder of Olen Business Consulting Inc. and co-author of the best-selling book The IBM i Programmer's Guide to PHP. He has over 20 years of experience working with IBM midrange systems, including all the incarnations of the System i. In addition to extensive development experience using RPG, he also has expertise with PHP, JavaScript, C, C++, MySQL, and HTML.

 

Jeff is an accomplished author with numerous technical articles to his credit. Jeff continues to develop custom applications and even writes code (not as much as he'd like). Jeff can be reached at jmo@olen-inc.com.

 

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

This is the first book to comprehensively address PHP and how it can—and should—be deployed on the IBM i.

List Price $79.95
Now On Sale
 
Read More >>
Last Updated on Friday, 14 May 2010 07:44
 
User Rating: / 3
PoorBest 
   MC-STORE.COM