KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Using PHP for email with SMTP Authentication & SSL
PRODUCT: 4D | VERSION: 12 | PLATFORM: Mac & Win
Published On: May 14, 2010

Here is an approach for using PHP in 4D v12 to send SMTP emails that require authentication, both with and without SSL.

First, Install the supporting files in "/Resources/php-pear-mail/" directory (relative to your database structure):

PEAR.php
PEAR5.php
Mail.php
MAIL/mail.php
MAIL/mock.php
MAIL/null.php
MAIL/RFC822.php
MAIL/sendmail.php
MAIL/smtp.php
MAIL/smtpmx.php
Net/SMTP.php
Net/Socket.php


All of the above files are avilable from https://pear.php.net;
Pear: https://pear.php.net/manual/en/installation.php
Mail: https://pear.php.net/package/Mail/
NET_SMTP: https://pear.php.net/package/Net_SMTP/
NET_SOCKET: https://pear.php.net/package/Net_Socket/

They have also been packaged together in this zip: ftp://ftp.4d.com/ACI_TECHNICAL_TIPS/76081/php-pear-mail.zip

Once the supporting files are in place, create a PHP Wrapper method as shown below and save it in a file "/Resources/php-pear-mail/myMailWrapper.php" which is relative to the database structure. The PHP code follows:

<?php

require_once "Mail.php";

function myMailWrapper($from, $to, $subject, $body, $host, $port, $username, $password){

  $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);

  $smtp = Mail::factory('smtp',
    array ('host' => $host,
      'port' => $port,
      'auth' => true,
      'username' => $username,
      'password' => $password));

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

  if (PEAR::isError($mail)) {
    return $mail->getMessage();
  } else {
    return "Message successfully sent!";
  }
}
?>


Then create a 4D method which will call your PHP script, an example is shown here:

// --[HEADERSTART]-------------------------------------
// Method Name: php_pear_smtp_send
// ----------------------------------------------------
// Description:
// This method uses pear's MAIL library to send an
// email message using the following wrapper:
//
// myMailWrapper($from, $to, $subject, $body, $host, $port, $username, $password)
//
// Parameters: 8
//
// Return Value: text
//
// --[HEADEREND]---------------------------------------


C_LONGINT($params)
$params:=Count parameters

If ($params=8)
   //required parameters
  C_TEXT($1) //from
  C_TEXT($2) //to
  C_TEXT($3) //subject
  C_TEXT($4) //body
  C_TEXT($5) //host
  C_TEXT($6) //port
  C_TEXT($7) //username
  C_TEXT($8) //password

   // get platform specific delimiter
  C_TEXT($delim)
  $delim:=(Get 4D folder(Database Folder)[[Length(Get 4D folder(Database Folder))]])

   // location of wrapper method
  C_TEXT($phpfile)
  $phpfile:=Get 4D folder(Current Resources folder)+"php-pear-mail"
  $phpfile:=$phpfile+$delim+"myMailWrapper.php"

   // send the message
  C_BOOLEAN($success)
  C_TEXT($0)
  $success:=PHP Execute($phpfile;"myMailWrapper";$0;$1;$2;$3;$4;$5;$6;$7;$8)

End if


The following code shows an example of how to call the php_pear_smtp_send method:

C_TEXT($from)
C_TEXT($to)
C_TEXT($subject)
C_TEXT($body)
C_TEXT($host)
C_TEXT($port)
C_TEXT($username)
C_TEXT($password)

$from:="someone@mymailserver.com"
$to:="someone@somewhere.com"
$subject:="This is the subject"
$body:="Hi\n\nThis is a test message!!!\nDid you get it?"
$host:="smtp.mymailserver.com"
$port:="25"
$username:="someone@mymailserver.com"
$password:="somepassword"

C_TEXT($sent)
$sent:=php_pear_smtp_send ($from;$to;$subject;$body;$host;$port;$username;$password)


The same methods can be used for sending SMTP messages with SSL encryption. The $host parameter must be changed to include a "ssl://" prefix and the $port parameter must be changed. An example is shown below:

C_TEXT($from)
C_TEXT($to)
C_TEXT($subject)
C_TEXT($body)
C_TEXT($host)
C_TEXT($port)
C_TEXT($username)
C_TEXT($password)

$from:="someone@mymailserver.com"
$to:="someone@somewhere.com"
$subject:="This is the subject"
$body:="Hi\n\nThis is a test message!!!\nDid you get it?"
$host:="ssl://smtp.mymailserver.com"
$port:="465"
$username:="someone@mymailserver.com"
$password:="somepassword"

C_TEXT($sent)
$sent:=php_pear_smtp_send ($from;$to;$subject;$body;$host;$port;$username;$password)


If the message is sent successfully, the return value is "Message successfully sent!" otherwise, the error code will be returned from the php script.

Commented by Worldwide 4D Partner on June 4, 2010 at 5:12 AM
You may want to take a look at other PHP frameworks, such as http://ezcomponents.org/docs/tutorials/Mail. ezcomponents is much bigger (needs 100 MB to install), but provides a huge set of features. More details for SMTP, interesting chart features (to replace 4D Chart) and much more. It is under the "New BSD Licence", but as it is so big it may not be the best for deployment, still worthfull a look. Usage is similar. Create a PHP wrapper following their example and call it from 4D.
Commented by Peter Schumacher on May 31, 2010 at 9:28 AM
Maybe I have overseen it, but it does not handle CRAM-MD5.