Send An Email from within A Page

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Send An Email from within A Page

Post by calguy1000 »

Here's a simple example of an advanced topic:  Accessing CMSMailer from within your page content.

Here's my code.... for reference:

Code: Select all

{if isset($smarty.post.submit)}
  {cmsmailer} {* udt *}
  {$cmsmailer->reset()}
  {$cmsmailer->SetSubject('Test Message')}
  {$cmsmailer->AddAddress('calguy1000@cmsmadesimple.org')}
  {$cmsmailer->SetBody('This is a test message')}
  {$cmsmailer->Send()}
  Message Sent
{else}
  <form name="test" method="post">
  <input type="submit" name="submit" value="Send Test Message">
  </form>
{/if}
This code creates a simple form with a single submit button on it.  When the button is clicked, the result is returned to the same page, and the smarty logic kicks in.

First the {cmsmailer} udt is invoked which simply assigns the cmsmailer object to smarty for use in our page
Then I call the various cmsmailer methods to send an email.

This example kinds blurs the lines between programming and layout and design, but only a little bit :)
I'm sure you can find many uses for code like this to send messages from within your page.

Here's the code for the cmsmailer UDT:

Code: Select all

global $gCms;

$smarty =& $gCms->GetSmarty();
$cmsmailer =& $gCms->modules['CMSMailer']['object'];

$smarty->assign_by_ref('cmsmailer',$cmsmailer);
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
vaughnt
Forum Members
Forum Members
Posts: 82
Joined: Tue Jun 13, 2006 2:05 pm

Re: Send An Email from within A Page

Post by vaughnt »

that's a good building block, thanks.
--
My photography: http://vaughnsphotoart.com
Festiva Resorts: http://www.festiva.travel
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Send An Email from within A Page

Post by calguy1000 »

Continuing on the above note.... Here's a simple contact form that uses the udt I created above.  Essentially, the {contact_form} plugin, and the contact_form form in formbuilder are not really needed.

Though this example doesn't have captcha... I'm pretty sure that if I applied myself for a few minutes, I could get that to work in this example too.

Code: Select all

{* a simple contact form writtten entirely in smarty *}

{if isset($smarty.post.submit)}
   {* process the form *}
   {if empty($smarty.post.name)}
        <p><font color="red">Error: You must specify A name</font></p>
   {elseif empty($smarty.post.email)}
        <p><font color="red">Error: You must specify An Email Address</font></p>
   {elseif empty($smarty.post.subject)}
        <p><font color="red">Error: You must specify A message subject</font></p>
   {elseif empty($smarty.post.message)}
        <p><font color="red">Error: You must specify A message body</font></p>
   {else}
       {* prepare the message body *}
{capture assign='body'}
Message From: {$smarty.post.name} ({$smarty.post.email})
Message Sent:  {$smarty.now|cms_date_format}
Message Text:
-------------------
{$smarty.post.message}
{/capture}

       {* send it *}
       {cmsmailer}
       {$cmsmailer->reset()}
       {$cmsmailer->AddAddress('calguy1000@cmsmadesimple.org')}
       {$cmsmailer->SetSubject($smarty.post.subject)}
       {$cmsmailer->SetBody($body)}
       {capture assign='junk'}{$cmsmailer->Send()}{/capture}
   {/if}
{/if}

   {* display the form *}
<form name="contactform" method="post">
<table>
   <tr> 
      <td>Your Name:</td>
      <td><input name="name" type="text" size="50" maxlength="50"></td>
   </tr>
   <tr> 
      <td>Your Email Address:</td>
      <td><input name="email" type="text" size="50" maxlength="50"></td>
   </tr>
   <tr> 
      <td>Subject:</td>
      <td><input name="subject" type="text" size="50" maxlength="50"></td>
   </tr>
   <tr> 
      <td>Mesage:</td>
      <td><textarea name="message" cols="50" rows="8">Enter Your Message Here</textarea></td>
   </tr>
   <tr> 
      <td> </td>
      <td><input name="submit" type="submit" value="Send It"></td>
   </tr>
</table>
</form>
Edit:  I cleaned up the code a bit and did the error checking a bit better.... I also capture the output from the email sending so that it doesn't get output to the page....
Last edited by calguy1000 on Mon Feb 18, 2008 4:12 pm, edited 1 time in total.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
User avatar
pixelita
Power Poster
Power Poster
Posts: 388
Joined: Sun Sep 16, 2007 3:07 am

Re: Send An Email from within A Page

Post by pixelita »

Would this be what I'd use for something like "email this page to a friend"?  Because that's what I'm looking for right now. :)
Submit your site to the We Love CMSMS showcase
vaughnt
Forum Members
Forum Members
Posts: 82
Joined: Tue Jun 13, 2006 2:05 pm

Re: Send An Email from within A Page

Post by vaughnt »

pixelita wrote: Would this be what I'd use for something like "email this page to a friend"?  Because that's what I'm looking for right now. :)
Pixelita-

Try this tag/plugin I made:
http://dev.cmsmadesimple.org/projects/tellafriend

See it in action here:
http://www.vaughnsphotoart.com/index.ph ... et-on-moss
(the button is under the photo)

The hide/show is not part of the functionality, I added that for my site. But the captcha support is included.
--
My photography: http://vaughnsphotoart.com
Festiva Resorts: http://www.festiva.travel
Post Reply

Return to “Tips and Tricks”