Contact Form - SendMessage

8 posts by 3 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: June 26, 2020   (RSS)

By gregThomas - May 11, 2020

Hey Petrogus,

Thanks for posting your code, I've done a quick review of it and I can't see anything wrong, it looks like you're using the sendMessage function correctly.

Could you give me a few more details on what's going wrong? When you run the code, does it return a mail error, or does the email appear to send correctly but then nothing is received at the email address used?

A couple of things I'd recommend checking:

  1. Log into your CMS and click Admin Menu -> Email Settings and ensure that Outgoing Mail is set to Send and Log.
  2. Try sending the email again and then check out the CMS Outgoing Mail log (which is also under the Email Settings menu) and see if the email appears there. 

If the email does appear in the log then you know the CMS did attempt to send it and something went wrong at the server level (most likely the email settings are not configured correctly). If the email does not appear in the log then it's most likely an issue with the code that attempted to send it.

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By petrogus - May 12, 2020 - edited: May 12, 2020

Thank you Greg for your response,

I wasn't clear enough with my previews post. At the first time script send mails in the way that email template was structured
but the placeholders was empty. Finally we find the correct sequence to work.

I copy the way it work to me below (partial copy)

.
.
.
.

    ### INSERT NEW RECORD
    if (!$errorsAndAlerts) {
      $colsToValues = array(
        'createdDate='     => "NOW()",
        'createdByUserNum' => (int) @$CURRENT_USER['num'],
        'updatedDate='     => "NOW()",
        'updatedByUserNum' => (int) @$CURRENT_USER['num'],
        'dragSortOrder'    => time(),
        'name'             => @$_REQUEST['name'],
        'surname'          => @$_REQUEST['surname'],
        'phone'            => @$_REQUEST['phone'],
        'email'            => @$_REQUEST['email'],
        'message'          => @$_REQUEST['message'],
        'agree'            => @$_REQUEST['agree'],
      );

      // insert or update record (and adopt uploads [for future use])
      $recordNum = fg_util_insertOrUpdateRecord($FORM_TABLE, $FORM_RECORD_NUM, $colsToValues);
      adoptUploads($FORM_TABLE, $FORM_PRESAVETEMPID, $recordNum);
      removeExpiredUploads(); // erase old expired uploads
	  
		$email    = isset( $_REQUEST['email'] ) ? $_REQUEST['email'] : '';
		$fullname = isset( $_REQUEST['name'] ) ? $_REQUEST['name']." ".$_REQUEST['surname'] : '';
		$phone    = isset( $_REQUEST['phone'] ) ? $_REQUEST['phone'] : '';
		$message  = isset( $_REQUEST['message'] ) ? $_REQUEST['message'] : '';
		if(@$_POST['agree'] == "1") { $agree = "Yes!"; } else { $agree = "No?"; }


	  $emailHeaders = emailTemplate_loadFromDB(array(
		'template_id'        => 'TEST-MAIL-ME',
		'addHeaderAndFooter' => false,
		'placeholders'       => array(
		  'visitor.name'      => $fullname,
		  'visitor.email'     => $email,
		  'visitor.phone'     => $phone,
		  'visitor.message'   => $message,
		  'visitor.agree'     => $agree,
		),
	  ));

    //Get the subject to use in the title tag.
    $htmlTitle  = htmlencode($emailHeaders['subject']);
    //Custom Header
    $header = <<<__HTML__
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>$htmlTitle</title>
</head>

<body>

<style type="text/css">p { margin-bottom: 1em; color:#000; }</style>

__HTML__;
// ***NOTE*** style tag is for Yahoo Mail which otherwise drops paragraph spacing - http://www.email-standards.org/blog/entry/yahoo-drops-paragraph-spacing/
// ... having a defined <title></title> helps get by spam filters

  //Generate the footer
  $footer = <<<__HTML__
</body>
</html>
__HTML__;
  
  //Add teh footer and header to the email headers.
  $emailHeaders['html'] = $header . $emailHeaders['html'] . $footer;

  //Send the email
  $mailErrors = sendMessage($emailHeaders);
  if ($mailErrors) { die("Mail Error: $mailErrors"); }

      // on success
      $_REQUEST        = []; // clear form fields
      $errorsAndAlerts = "Thank you! The form has been submitted successfully.<br />\n";
      $success         = true;		  
    }
  }
 

.
.
.
.

Thank you for your support !!!

Could you help me in case I would like to send email with values from checkboxes (multi value)

PetroGus

By gregThomas - May 12, 2020

Hey petrogus,

Good to hear the issue is resolved.

When sending the values from a set of multi-select checkbox fields, the simplest solution is to send the values as a comma-separated string using the implode function.

For example, if you had a set of multi-select checkboxes like this:

<input name="cities[]" value="London" />
<input name="cities[]" value="New York" />
<input name="cities[]" value="Tokyo" />

You could add the values as a placeholder to your email using the following method:

  $cities = "";
  if( !empty($_REQUEST['cities']) && is_array($_REQUEST['cities']) ) {
    $cities = implode(", ", $_REQUEST['cities']);
  }

  $placeholders = [
    'visitor.name'      => $fullname,
    'visitor.email'     => $email,
    'visitor.phone'     => $phone,
    'visitor.message'   => $message,
    'visitor.agree'     => $agree,
    'visitor.cities'    => $cities
  ];

This is just sample code, so you'll probably need to make a few changes to get it working with your codebase, but hopefully, it will point you in the right direction.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By petrogus - May 12, 2020

Great !! Many Thanks Greg

PetroGus

By AlanAlonso - June 22, 2020

I made some changes to the code and worked perfectly to me, but now I need to send two emails at the same time with diferent templates, can you please help me.

if (!$errorsAndAlerts) {
  $emailHeaders = emailTemplate_loadFromDB(array(
    'template_id'        => 'TEST-MAIL-ME',
    'addHeaderAndFooter' => false,
    'placeholders'       => array(
      'visitor.name'      => $fullname,
      'visitor.email'     => $email,
      'visitor.phone'     => $phone,
      'visitor.message'   => $message,
      'visitor.agree'     => $agree,
    ),
  ));

By gregThomas - June 23, 2020

Hey AlanAlonso, 

You can call the emailTemplate_loadFromDB and sendMessage functions for each email that you need to send. For example, if you have two templates called TEST-MAIL-ME and TEST-MAIL-ME-2, you could send both using the following code:

if (!$errorsAndAlerts) {
  
  //Create the placeholders that will be used in both emails
  $placeholders = [
    'visitor.name'      => $fullname,
    'visitor.email'     => $email,
    'visitor.phone'     => $phone,
    'visitor.message'   => $message,
    'visitor.agree'     => $agree,
  ];

  //Send the first email
  $emailHeaders = emailTemplate_loadFromDB(array(
    'template_id'        => 'TEST-MAIL-ME',
    'addHeaderAndFooter' => false,
    'placeholders'       => $placeholders,
  ));
  $mailErrors = sendMessage($emailHeaders);


  //Send the second
  $emailHeaders = emailTemplate_loadFromDB(array(
    'template_id'        => 'TEST-MAIL-ME-2',
    'addHeaderAndFooter' => false,
    'placeholders'       => $placeholders,
  ));
  $mailErrors = sendMessage($emailHeaders);

This just example code, so you'll have to make a few changes to get it working (for example; updating the email template ID's), but it should point you in the right direction.

Cheers,

Greg Thomas







PHP Programmer - interactivetools.com

By AlanAlonso - June 26, 2020

Thank you Greg, I will try to update form generator to include  this.