PDF='MAIN' { ATTRIBUTES='*' { PLATFORM='php.txt'; REPTYPE='1'; ATTACHED='0'; PROJECT='36'; OBJECTNAME='wsphpemail.php'; LIBLOBJ='WEBDEMO'; TITLE='Send an HTML Email'; WEBPATH='http://esdi.excelsystems.com/wsexmpph/'; TARGETPATH='/www/zendcore/htdocs/examples/'; FTPSITE='WS Examples'; SET='149'; VERSION='4.00'; CRTBNUM='Revision 1811'; CRTTEMPLT='C:\\Program Files\\ESDI\\WebSmart\\Temp\\PHP-A Simple Page.tpl'; SAVBNUM='Revision 3270'; LASTLOC='1'; VHTML='N'; LASTSAVE='7/11/2008 14:55:23'; OBJLIBRARY='XL_WEBEXMP'; SOURCELIBR='XL_WEBEXMP'; SOURCEFILE='QRPGLESRC'; REPSYSTEM='Excel270'; } FTPREFINFO='*' { URL='192.168.0.100'; INITIALDIR='/www/zendcore/htdocs/examples/'; FTPPORT='21'; PASSIVEMODE='1'; WEBPATH='http://esdi.excelsystems.com/wsexmp/'; } PANELS='*' { PANEL='MainSeg' { DESC='Main Segment'; DETAILS=' Send an HTML Email

Send an HTML Email

Description:   This WebSmart PHP example creates and sends an HTML email to the entered address. Any HTML code and text can be combined and inserted into an email for more dynamic presentation of content. In this example what you enter into the Body Text will be merged with some program-generated HTML to complete the message.

The main PHP function that is used is mail().

Note that this example only does basic email address format validation. Refer to Example 140 for more details on email validation.

Instructions: Enter a From Address, a valid To Address, a Subject, replace some Body Text and click Send Email to generate and send a sample HTML-based email.

Note that you can use any valid HTML tags within the Body Text (as shown):

From Address:
To Address:
Subject:
Body Text:
$val_msg
  1. Open a new \'Simple Page\' template.

  2. Add this code just below the \'require\' line in the PHP source-code:
    // define global variables
    global $val_msg, $valid, $custom_msg;
                          
  3. Add this task to the PHP source-code below the if($pf_task == \'default\') generic(); code:
    if ($pf_task == \'sendEmail\')
    	sendEmail();
                          
  4. Add this function to the PHP source-code below the generic function:
                  
    function sendEmail()
    {
    	
    	// Make all global variables available here
    	foreach($GLOBALS as $arraykey=>$arrayvalue) 
    	{
    		if($arraykey[0]!=\'_\' && $arraykey != \'GLOBALS\')
    			global $$arraykey;
    	}
    	
    	//check for email validity
    	if(validateEmail())
    	{
    		//retrieve the user-entered text
    		$custom_msg = $_REQUEST["emailText"];
    		
    		if($custom_msg == "")
    			$custom_msg = "The body text was blank.";
    		
    		//ob_start is the command to begin buffering
    		ob_start();
    		
    		//wrtseg in this instance writes the segment to the buffer
    		wrtseg("EmailSeg");
    		
    		//assign the value of the buffer to a variable
    		$message .= ob_get_contents();
    		
    		//clear the buffer
    		ob_end_clean();
    		
    		//retrieve the emails entered by the user
    		$to = $_REQUEST["email"];
    		$from = $_REQUEST["fromEmail"];
    		
    		if($from == "")
    			$from = "websmart@sample.com";
    		
    		// for multiple recipients
    		//$to  = \'test1@sample.com\' . \', \'; // note the comma
    		//$to .= \'test2@sample.com\';
    		
    		// subject
    		$subject = $_REQUEST["subject"];
    		
    		if($subject == "")
    			$subject = "Blank subject";
    		
    		//to send HTML mail, the Content-type header must be set
    		$headers  = \'MIME-Version: 1.0\' . "\\\\r\\\\n";
    		$headers .= \'Content-type: text/html; charset=iso-8859-1\' . "\\\\r\\\\n";
    		
    		//additional headers
    		$headers .= "From: " . $from . "\\\\r\\\\n";
    		$headers .= "Reply-To: " . $from . "\\\\r\\\\n";
    		
    		//optional headers for Cc: and Bcc: options
    		//$headers .= \'Cc: cc@sample.com\' . "\\\\r\\\\n";
    		//$headers .= \'Bcc: bcc@sample.com\' . "\\\\r\\\\n";
    		
    		// send the compiled email
    		mail($to, $subject, $message, $headers);	
    	}
    	//re-write the main screen
    	generic();	
    }
    
  5. Add this function to the PHP source-code below the previous step:
                       
    function validateEmail()
    {
      // Make all global variables available here
      foreach($GLOBALS as $arraykey=>$arrayvalue) 
      {
        if($arraykey[0]!=\'_\' && $arraykey != \'GLOBALS\')
        global $$arraykey;
      }
    
      //retrieve email to check formatting
      $email = $_REQUEST["email"];
    
      //check that email matches the basic email format
      if(!eregi("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})$",$email))
      {
        $val_msg = "Please verify that your email address is correctly formatted.";
        $valid = FALSE;
      }
      else
      {
        $val_msg = "Your email has been sent.";
        $valid = TRUE;
      }
      return $valid;
    }
                          
  6. Add this html to the main segment somewhere between the <body> and </body> tags:
                        
    <div id="contents"> 
      <strong>Enter a valid email address to send a sample HTML-based email:</strong>
      <br><br>
      <form name="emailform" action="$pf_scriptname" method="POST">
        <input type="hidden" name="task" value="sendEmail">
        <table>
          <tr>
            <td>Email Address:</td><td><input type="text" name="email" size="30">
              <input type="submit" value="Send Email">
            </td>
          </tr>
        </table> 
      </form>
      <div align="left" style="color:red; margin-left: 100px; text-style:bold;">
        <b>$val_msg</b>
      </div>      
    </div>
                          
  7. Create a new segment. From the menu select Attributes-->Segments and select Add. Name the Segment EmailSeg and the description Email Segment.

  8. Paste the following code into EmailSeg:
  9. <html>
    <head>
    </head>
    <body style="font-family: Arial, Verdana, Sans-serif;font-size: 10px;background:#ffffff">
      <table>
        <tr>
          <td align="center">
            <img align="center" 
            src="http://esdi.excelsystems.com/websmart/Images/websmart-sample-app-255x96_80.gif" 
            alt="WebSmart Sample Application Image - If you are seeing this text 
            it is likely because your email client does not support HTML or is blocking 
            remote images." border="0">
          </td>
        </tr>
    	<tr>
    	  <td><h3>Everything between the lines is your custom text.</h3></td>
    	</tr>
    	<tr>
    	  <td><hr size="2"></td>
    	</tr>
    	<tr>
    	  <td>$custom_msg</td>
    	</tr>
    	<tr>
    	  <td><hr size="2"></td>
    	</tr>
    	<tr>
          <td>
            <div>
              BCD Software Support and Customer Service<br>
    	        
              <a href="http://www.bcdsoftware.com/">http://www.bcdsoftware.com</a><br>
              <a href="mailto:excel@excelsystems.com">excel@excelsystems.com</a><br>
              (250) 655-1766<br>
              <br>
              Have you checked out our <a
              href="http://esdi.excelsystems.com/iseries400apps/KB_docs.pgm">Knowledge
              Base</a> yet?
            </div>
          </td>
        </tr>
      </table>
    </body>
    </html>
                          
  10. Save, generate and launch the program.

  11. Enter valid information and click Send Email.

  12. Check your email client to see the email that you have created.

Program Definition:   wsphpemail.phw

Rate This Example

Did this knowledge base article help you to achieve your goal?  Yes  No  Don\'t Know

Enter additional comments below.   If you want to hear back from us, include your contact information.

'; } PANEL='EmailSeg' { DESC='Email Segment'; DETAILS='
WebSmart Sample Application Image - If you are seeing this text it is likely because your email client does not support HTML or is blocking remote images.

Everything between the lines is your custom text.


$custom_msg

BCD Software Support and Customer Service
http://www.bcdsoftware.com
excel@excelsystems.com
(250) 655-1766

Have you checked out our Knowledge Base yet?
'; } } PREVIEWS='*' { DEFAULT='MainSeg'; PREVIEW='MainSeg' { SEG='MainSeg'; } } ACTIONS=' // Program Name: wsphpemail.php // Program Title: Sending an HTML Email // Created by: GMP // Template family: Idaho // Template name: Simple Page // Purpose: To demonstrate how to generate and send an HTML Email using WebSmart PHP. // Program Modifications: require(\'/esdi/websmart/v6.6/include/xl_functions001.php\'); // define global variables global $val_msg, $valid, $custom_msg; // Add this code to the \'Simple Page Template\' // This code is for the Rate this page section if (isset($_REQUEST[\'wsnum\'])) { $wsnum = $_REQUEST[\'wsnum\']; } // Check the cookie if(isset($_COOKIE[\'wsnumbc\'])) { $wsnum = $_COOKIE[\'wsnumbc\']; } else { setcookie(\'wsnumbc\', $wsnum); } // Retrieve the task (default to "default") if ($pf_task == \'default\') generic(); if ($pf_task == \'sendEmail\') sendEmail(); function generic() { wrtseg(\'MainSeg\'); } function sendEmail() { // Make all global variables available here foreach($GLOBALS as $arraykey=>$arrayvalue) { if($arraykey[0]!=\'_\' && $arraykey != \'GLOBALS\') global $$arraykey; } //check for email validity if(validateEmail()) { //retrieve the user-entered text $custom_msg = $_REQUEST["emailText"]; if($custom_msg == "") $custom_msg = "The body text was blank."; //ob_start is the command to begin buffering ob_start(); //wrtseg in this instance writes the segment to the buffer wrtseg("EmailSeg"); //assign the value of the buffer to a variable $message .= ob_get_contents(); //clear the buffer ob_end_clean(); //retrieve the emails entered by the user $to = $_REQUEST["email"]; $from = $_REQUEST["fromEmail"]; if($from == "") $from = "websmart@sample.com"; // for multiple recipients //$to = \'test1@sample.com\' . \', \'; // note the comma //$to .= \'test2@sample.com\'; // subject $subject = $_REQUEST["subject"]; if($subject == "") $subject = "Blank subject"; //to send HTML mail, the Content-type header must be set $headers = \'MIME-Version: 1.0\' . "\\r\\n"; $headers .= \'Content-type: text/html; charset=iso-8859-1\' . "\\r\\n"; //additional headers $headers .= "From: " . $from . "\\r\\n"; $headers .= "Reply-To: " . $from . "\\r\\n"; //optional headers for Cc: and Bcc: options //$headers .= \'Cc: cc@sample.com\' . "\\r\\n"; //$headers .= \'Bcc: bcc@sample.com\' . "\\r\\n"; // send the compiled email mail($to, $subject, $message, $headers); } //re-write the main screen generic(); } // basic email format validation // referenced from BCD Example #140 function validateEmail() { // Make all global variables available here foreach($GLOBALS as $arraykey=>$arrayvalue) { if($arraykey[0]!=\'_\' && $arraykey != \'GLOBALS\') global $$arraykey; } //retrieve email to check formatting $email = $_REQUEST["email"]; //check that email matches the basic email format if(!eregi("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})$", $email)) { $val_msg = "Please verify that your email address is correctly formatted."; $valid = FALSE; } else { $val_msg = "Your email has been sent."; $valid = TRUE; } return $valid; }'; }