2.52+ email tutorial?

7 posts by 2 authors in: Forums > CMS Builder
Last Post: May 6, 2013   (RSS)

By gkornbluth - April 25, 2013 - edited: April 25, 2013

Hi All,

I think that I’ve got the built in emails (like forgot password) being sent correctly by a SMTP server, and I might have missed it, but is there a tutorial about using the new email functionality built into 2.52+ to send emails created by viewers (web pages) by SMTP, like when an email needs to be sent to all users informing them of a particular event?

Also, I noticed that there is a check box in the “Outgoing Email” Log section called “Background Send” that indicates a Queued message to be sent by background mailer (for high-volume mail), but I don’t know how to set that up or where to insert the delayed mail parameters required I.E.: Message per hour, per day, messsage spacing required,etc.)

Here’s a pared down example of the type of message that I’d like to be able to send by SMTP, I just don’t know what changes I’ll have to make to the code, especially after the $mailArray code:

<?php $acceptance_email_message_1 = @$messagesRecord['acceptance_email_message_1'] ?>
      
<?php foreach($client_uploadsRecords as $record) : ?>
<?php $the_to = $record['email']; ?>
<?php $first_name = $record['first_name'] ; ?>
<?php $last_name = $record['last_name'] ; ?>
     
<?php
  $message = <<<EOF
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type content='text/html; charset=utf-8" />
<style type="text/css">
.special {font-family:Arial; color:  rgb(227,224,219); font-size: 1.0em; font-weight: bold; text-decoration: underline;}
.body-text {font-family:Arial; font-size: 1.0em;}
.body-text-yellow {font-family:Arial; color: rgb(255,255,0); font-size: 1.0em;}
.heading-text-yellow {font-family:Arial; color: rgb(255,255,0); font-size: 1.3em;}
</style>
</head>
<body bgcolor="#7C7164">
<table style='background-color: #7C7164;' width='100%' align='center' border='0' >
<tr>
<td align='left' >
<table style='background-color: #7C7164;' width='70%' align='center' border='0' >
<tr>
<td align='left' >
 <div align='left'>
<img src='http://www.artistsofpalmbeachcounty.org/exhibition_email_open.php?num=$recnum' height='1' width='1' alt='' />
<img src='http://www.artistsofpalmbeachcounty.org/images/APBC-LOGO.png' width='800' height='183' style='border:hidden'/></div>
<br /><br />
 <div align='left' class='body-text-yellow'>
Hello $first_name $last_name,
<br /><br />
 $acceptance_email_message_1
<br /><br />
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
EOF;
 $the_from  = "submissions@artistsofpalmbeachcounty.org";
 $the_subject = "Your entry has been accepted";

$mailArray = array(
'to' => $the_to,
'from' => $the_from,
'subject' => $the_subject,
'html'         => $message
);
$errors =  sendMessage($mailArray);
// delay execution by .2 sec
usleep(200000);
// reset max_execution_time to 30 sec
set_time_limit(30);
?>
<?php endforeach ?>

Thanks,
Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Dave - April 29, 2013

Hi Jerry,

There's no docs for that, but here's some steps.  Note these are all somewhat more advanced programming tasks.

- Create a new email template under: Admin > Email Templates
- For mine I'm using an Email ID: EMAIL-TEST
- And this for the content: Hello, favorite color is #color# and city is #city#. Bye

Then go to: Admin > Email Templates > Advanced Commands (bottom left on list page) > Developers: Show sendMessage() PHP
There will be some email code sending each email template, find the one you created and copy the code, here's mine:

  // EMAIL-TEST
  $emailHeaders = emailTemplate_loadFromDB(array(
    'template_id'  => 'EMAIL-TEST',
    'placeholders' => array(
    //'your_placeholder' => "your value", // add new placeholders here
    ),
  ));
  $mailErrors = sendMessage($emailHeaders);
  if ($mailErrors) { die("Mail Error: $mailErrors"); }

I'll add some placeholder for 'color' and 'city' by copying the placeholder line and add for code for this example: 

<?php
  require_once "C:/wamp/www/sb/CMS Builder/cmsAdmin/lib/viewer_functions.php";

  // show send link
  if (!@$_REQUEST['send']) {
    print "<a href='?send=1'>Send email</a>";
    exit;
  }

  // send email
  print "Sending email...<br/>\n";

  // EMAIL-TEST
  $emailHeaders = emailTemplate_loadFromDB(array(
    'template_id'  => 'EMAIL-TEST',
    'placeholders' => array(
      'color' => "red", // add new placeholders here
      'city'  => "Vancouver", // add new placeholders here
    ),
  ));
  $mailErrors = sendMessage($emailHeaders);
  if ($mailErrors) { die("Mail Error: $mailErrors"); }

  //
  print "Done";
  exit;

?>

Then if you go back to the Email Templates, you'll see it also updated your list of available placeholders in the bottom field.

>... like when an email needs to be sent to all users informing them of a particular event?

Sending to thousands of users can require some extra considerations, but in the simplest implementation you could loop over the user emails after calling emailTemplate_loadFromDB() like this: 

$mailErrors = '';
foreach ($emails as $email) {
  $emailHeaders['to'] = $email; 
  $mailErrors .= sendMessage($emailHeaders);
}
if ($mailErrors) { die("Mail Error: $mailErrors"); }

If you need to use user details as placeholders, loop over emailTemplate_loadFromDB() instead and set 'override-to' with the to address.

>Also, I noticed that there is a check box in the “Outgoing Email” Log section called “Background Send”

That's more of a hook for future use, there's no background mailer at this point (although we were starting to build one for some of the plugins).

If you look in the code for the emailTemplate_loadFromDB and sendMessage functions they also both have more examples above the functions.

Hope that helps, let me know any questions.

Dave Edis - Senior Developer
interactivetools.com

Thanks Dave,

I'm still a bit (a lot) confused about this, but willing to plug through it to get it to work...

That said, should I understand that:

A) I need to put any PHP including the EMAIL-TEST code into a new templates in order to use the functionality,

or,

B) I can to put that EMAIL-TEST code along with any other PHP code into a viewer?

or

C) I don't  understand this at all? "%$#@*"

Also, I'll probably have to send more than an allowable quantity of emails in one session, so to meet ISP requirements, I'll need to be able break the list down to predetermined quantities and schedule sending of each group.

If you could suggest the modifications required for the sample code above, I think I'll be able to get a better handle on this, and write up some docs that we can all follow.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Dave - May 2, 2013

Hi Jerry, 

I'd start by putting the code above into a separate file and seeing if you can get it to send an email using a template.  If you can get it to do that you can then try adding the code to other php pages or viewers.  

>... so to meet ISP requirements, I'll need to be able break the list down to predetermined quantities and schedule sending of each group.

There's actually quite a bit of custom code required to do all of that.  I'm not really sure of a simple way to do it.

We were previously working on the "Background Send" feature where you could just write the emails to the "Outgoing Mail" table.  An early version of Newsletter Builder worked like that, but we'd end up using a lot of disk space and having things go slower then we wanted.

How many emails do you think you'd be sending in a batch and what's it for?  I'll see if I can think of any other ideas for you.

Thanks

Dave Edis - Senior Developer
interactivetools.com

By Dave - May 6, 2013

Hi Jerry, 

I'm sure that many of us (developers) would like to be able to have, (and would be willing to pay for) a robust mail plugin that handled the scheduling and sending of emails separate from the formatting and creation of those emails.

I hope that you'll decide to revisit the project soon.

Thanks for the feedback.  I'll add making a plugin to do basic background mail sending to the list for the next release.  There's lots of issues that come up with mail sending, but it will let users try the background send functionality.

Have you had any luck sending a message from an email template yet? 

Cheers!

Dave Edis - Senior Developer
interactivetools.com

By gkornbluth - May 6, 2013 - edited: May 6, 2013

Thanks Dave,

No, not yet.

Life and work getting in the way. In addition to web design

I do some Audio and Audio for Video Post Production.

That's been my main focus for a lot of years, (before web design) and one of my client's has a deadline.

Back to it soon though.

Thanks for staying on top of this.

Jerry

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php