Email Form

4 posts by 3 authors in: Forums > CMS Builder
Last Post: April 27, 2015   (RSS)

By ross - April 24, 2015

Hi Tim

Thanks for posting!

Best place to start here is with the fact I am not actually that familiar with Emailform.php. Having said that though, if it's just a simple script that sends an email, there should be a way to hook into CMS Builders Outgoing Mail system.  

One option would be to include CMS Builder's library files into your emailform.php code and then just use the sendMail() function we built.

Another option would be to just manually enter a record in CMS Builder's Outgoing Mail table in the code as it sends your email.  

Of the two, option one is likely quicker as most of the functionality is built right in once you've included CMS Builder's library files.  

Do either options catch your eye?

Let me know what you think. Thanks!

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

By Toledoh - April 26, 2015

Hey Ross,

Option 1 would be fine - can you help?

The basic code I use is:

<?php

// process form
if (@$_REQUEST['submitForm']) {

// error checking
$errorsAndAlerts = "";
if (!@$_REQUEST['fullname']) { $errorsAndAlerts .= "You must enter your full name!<br/>\n"; }
if (!@$_REQUEST['edit']) { $errorsAndAlerts .= "You must enter your email!<br/>\n"; }
else if(!isValidEmail(@$_REQUEST['edit'])) { $errorsAndAlerts .= "Please enter a valid email (example: user@example.com)<br/>\n"; }
if (!@$_REQUEST['message']) { $errorsAndAlerts .= "You must enter a message!<br/>\n"; }

// send email user
if (!$errorsAndAlerts) {
$from = $_REQUEST['edit'];
$to = $snippetsRecord['send_to_email'];
$subject = "Contact form submitted from {$_SERVER['HTTP_HOST']}";
$message = <<<__TEXT__
You've received an email from the email form here:
http://{$_SERVER['HTTP_HOST']}{$_SERVER['SCRIPT_NAME']}

Full name: {$_REQUEST['fullname']}
Email: {$_REQUEST['edit']}
Rating: {$_REQUEST['rating']}
Message: {$_REQUEST['message']}

The user who sent this message had the IP address {$_SERVER['REMOTE_ADDR']}.
__TEXT__;
// Note: The above line must be flush left or you'll get an error
// This is a PHP heredoc. See: http://ca2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

// send message
$mailResult = @mail($to, $subject, $message, "From: $from");
if (!$mailResult) { die("Mail Error: $php_errormsg"); }

//
$errorsAndAlerts = "Thanks! We've sent your email.";
$_REQUEST = array(); // clear form values
}

}

?>

Cheers,

Tim (toledoh.com.au)

By Dave - April 27, 2015

Hi Tim, 

How it works is mail() is a PHP function that sends through the default server mail system that is configured.  

What we're doing now is calling our own function sendMessage() in /lib/common.php that sends mail with the configuration options from the CMSB.  

If you're into writing your own PHP code you can always look in the library functions for more info as we often have programmer notes and instructions in there.  Here's some examples from the header of sendMessage()

  // Minimal Example:
  $errors = sendMessage(array(
    'from'    => "from@example.com",
    'to'      => "to@example.com",
    'subject' => "Enter subject here, supports utf-8 content",
    'text'    => "Text message content",
  ));

  // Full Featured Example:
  $errors = sendMessage(array(
    'from'    => "from@example.com",
    'to'      => "to@example.com",
    'subject' => "Enter subject here, supports utf-8 content",
    'text'    => "Text message content",
    'html'    => "<b>HTML</b> message content",
    'headers' => array(
      "CC"          => "cc@example.com",
      "BCC"         => "bcc@example.com",
      "Reply-To"    => "rt@example.com",
      "Return-Path" => "rp@example.com",
    ),
    'attachments' => array(
      'simple.txt'  => 'A simple text file',
      'dynamic.csv' => $csvData,
      'archive.zip' => $binaryData,
      'image.jpg'   => file_get_contents($imagePath),
    ),
    //'disabled' => false, // set to true to disable sending of message
    //'logging' => false, // set to false to disable logging (if logging is already enabled) - used for background mailers
  ));

  // show errors
  if ($errors) { die($errors); }

So I'd try replacing this:

$mailResult = @mail($to, $subject, $message, "From: $from");
if (!$mailResult) { die("Mail Error: $php_errormsg"); }

With this:

  $errors = sendMessage(array(
    'from'    => $from,
    'to'      => $to,
    'subject' => $subject,
    'text'    => $message,
  ));
  if ($errors) { die($errors); }

Also make sure "From" is an email you are allowed to send from (*@yourdomain.com) or it might be blocked by some servers.  If you want to be able to click "Reply" to reply to client emails use the "Reply-To" header.

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com