1 of 3
1
Send an email to user-selected recipient using “freeform_module_insert_begin” hook
Posted: 17 July 2008 09:51 AM   [ Ignore ]
Member
Avatar
RankRankRank
Total Posts:  153
Joined  2006-09-11

I need to be able to send an email to someone who is selected from a dropdown list. I made a new extension that uses the “freeform_module_insert_begin” hook. I’m able to see all the contents of the submitted form using the extension. What I would like to do is take the contents of one field (which is an email) and send that person an email using the correct template. I know I can’t dynamically populate the “notify” parameter, so I thought I might be able to put a generic address in that parameter, and then send the email again to the “real” address I need the form to go to using the hook.

Here is the function in my hook:

function freeform_module_insert_begin ($data)
  
{
   
echo '<pre>';
   
print_r($data);
   echo 
'</pre>';
   exit;

That shows me the contents of all my fields then exits. I was using that for testing. Here is an abbreviated version of the output from that:

Array
(
 
[author_id] => 1
 [group_id] 
=> 1
 [ip_address] 
=> 90.100.100.100
 [entry_date] 
=> 1216326548
 [edit_date] 
=> 1216326548
 [status] 
=> open
 [form_name] 
=> contactForm
 [salescontact] 
=> salesguy@mycompany.com
 [fullname] 
=> John Schmo
 [company] 
=> SchmoInc.
 
[email] => john@schmo.com
 [phone1] 
=> 000 123 4578
 [comments] 
=> Call me asapplease.

I want to change it to this:

function freeform_module_insert_begin ($data)
  
{
 
if ($data['form_name']=='contactForm'{
  
var $salesEmail $data['salescontact'];
 
// the sales email in this case is salesguy@mycompany.com
 // how the heck do i email them the contents of $data, preferably using a certain template?
 
}

I want to email the salescontact (which was selected from a dropdown) before inserting the data into the DB. Can I just rip out the email parts from mod.freeform.php and put them here? (Approximately line 1042 or 1236).

Any help with this greatly appreciated.

 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
@masuga | @masugadesign | @devot_ee

Profile
 
 
Posted: 18 July 2008 06:47 AM   [ Ignore ]   [ # 1 ]
Newbie
Rank
Total Posts:  5
Joined  2006-11-04

How about PHPMailer?

Cheers
David

Profile
 
 
Posted: 18 July 2008 07:00 AM   [ Ignore ]   [ # 2 ]
Member
Avatar
RankRankRank
Total Posts:  153
Joined  2006-09-11

I was even thinking just the basic PHP Mail() function here, but didn’t want to go too far down that road as it’s probably not too secure. It looked to me like there were a bunch of ready-made functions ready to go inside the mod.freeform.php file, so I thought I might be able to have access to those to just send another mail. I was hoping I could do that because I already have a custom-formatted template I’m using to send the mail. If I had access to that, I wouldn’t (possibly) have to change formatting in two places later if anything about the notification email changed. I’m just not clear if I have access to those functions to send another mail.

This is really a pain in the butt not being able to select someone to notify on the fly!

 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
@masuga | @masugadesign | @devot_ee

Profile
 
 
Posted: 10 November 2008 08:30 PM   [ Ignore ]   [ # 3 ]
Newbie
Rank
Total Posts:  8
Joined  2007-08-27

mdesign, did you ever get this to work?

Profile
 
 
Posted: 10 November 2008 08:39 PM   [ Ignore ]   [ # 4 ]
Member
Avatar
RankRankRank
Total Posts:  153
Joined  2006-09-11

This is what I did.

I have a dropdown field called “salescontact” that has a list of email addresses. I created a super-basic extension that only relies on the one Freeform hook. Here is the function for that hook:

/* 
Remember, there are two hacks needed in Freeform 2.6.5 to get the $msg variable to work
here. You have to comment out both places where unset($msg) exists.
*/

function freeform_module_insert_end ($fields$entry_id$msg)
{
  
global $DB$EXT$REGX;

 
// find the correct salesperson to email  
 
$query $DB->query("SELECT * FROM exp_freeform_entries WHERE entry_id = '".$entry_id."' LIMIT 1");
 if ( 
$query->num_rows == )
 
{
  
return;
 
}
  $recipient 
$query->row['salescontact'];

 
// Send email
 
if ( ! class_exists('EEmail'))
 
{
  
require PATH_CORE.'core.email'.EXT;
 
}
 
 $email 
= new EEmail;
 
$email->wordwrap FALSE;
 
$email->mailtype 'html';
 
 
$email->initialize();
 
$email->from($msg['from_email']$msg['from_name']); 
 
$email->to($recipient); 
 
$email->subject($msg['subject']); 
 
$email->message($REGX->entities_to_ascii($msg['msg']));  
 
$email->Send();
 
 unset(
$msg);
 
 
$EXT->end_script FALSE;

That will send an email to the person selected in the dropdown. Note the comment about commenting out two things in Freeform. That may have been fixed since I wrote this.

This also can probably be improved on, but it is working.

 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
@masuga | @masugadesign | @devot_ee

Profile
 
 
Posted: 12 November 2008 12:16 PM   [ Ignore ]   [ # 5 ]
Newbie
Rank
Total Posts:  8
Joined  2007-08-27

Thanks, man. I ended up creating a weblog to manage the different email addresses the client wanted to use. Then I set up a dropdown on the contact us page, where a user could select who they wanted to email. The form redirected to the actual contact us freeform, with a couple of $_POST values. The notify parameter also got the post email, etc.

Like yours, it could probably be improved upon, but it’s working. grin

Profile
 
 
Posted: 12 November 2008 12:22 PM   [ Ignore ]   [ # 6 ]
Member
Avatar
RankRankRank
Total Posts:  153
Joined  2006-09-11

Your method is exactly what I did at first - but I thought it was a bit clunky, so I used this extension to eliminate that extra step.

 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
@masuga | @masugadesign | @devot_ee

Profile
 
 
Posted: 09 December 2008 10:34 PM   [ Ignore ]   [ # 7 ]
Jr. Member
RankRank
Total Posts:  37
Joined  2007-06-21

Hmm… Wow… I actually haven’t tried it yet but know that this is something I will need to do and find it surprising that a drop-down select box won’t ‘work’ ... to select who to mail to. Considerable amount of custom coding required ..

Any news from anyone?

Profile
 
 
Posted: 16 December 2008 08:11 PM   [ Ignore ]   [ # 8 ]
Jr. Member
RankRank
Total Posts:  31
Joined  2006-12-28

Just out of curiosity, would this work if you needed to give users the ability to select multiple recipients via checkboxes?

Profile
 
 
Posted: 06 April 2009 11:11 AM   [ Ignore ]   [ # 9 ]
Newbie
Rank
Total Posts:  20
Joined  2009-03-25

@mdesign you are a knight in shining EE armor. But you knew that. Thanks.

Profile
 
 
Posted: 06 April 2009 11:51 AM   [ Ignore ]   [ # 10 ]
Newbie
Rank
Total Posts:  20
Joined  2009-03-25

Also, if you want to be able to send multiple pipe delimited emails, you’ll need to stick this in there:

$recipient    preg_split("/,|\|/" $recipient ); 
Profile
 
 
Posted: 06 April 2009 11:57 AM   [ Ignore ]   [ # 11 ]
Member
Avatar
RankRankRank
Total Posts:  153
Joined  2006-09-11

Note that I have this extension at GitHub: md.freeform_send_another.ee_addon

 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
@masuga | @masugadesign | @devot_ee

Profile
 
 
Posted: 16 April 2009 03:00 PM   [ Ignore ]   [ # 12 ]
Jr. Member
RankRank
Total Posts:  27
Joined  2008-02-18

mdesign, i know you’re a knight in shining armor, but i’m sort of not following how to implement this - where is the drop-down of the email addys? um, i assume you have a dropdown in your contact form with an id of the contact you want to pull but i’m not sure where i set up that field that contains the actual contact email ... sorry, i haven’t done too much with freeform and apologies if i’m being a total dunce. i’m just after the same thing so many EE folks want - a select-recipient email form that doesn’t have the email addys in plain html daylight! many thanks.

Profile
 
 
Posted: 16 April 2009 07:26 PM   [ Ignore ]   [ # 13 ]
Member
Avatar
RankRankRank
Total Posts:  153
Joined  2006-09-11

The actual working form is on this page: http://www.hexarmor.com/where-to-buy/

I have a weblog called “salespeople” that I’m looping through to make all the options for the dropdown. Whoever the user selects gets emailed and stored in the appropriate freeform field. So I’m constructing the options from a weblog, and storing that value in a freeform text field, but also emailing whomever was selected with the extension/extra hook. Does that make sense?

 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
@masuga | @masugadesign | @devot_ee

Profile
 
 
Posted: 16 April 2009 08:46 PM   [ Ignore ]   [ # 14 ]
Jr. Member
RankRank
Total Posts:  27
Joined  2008-02-18

thanks mdesign. i looked under the ‘hood, and isn’t having the emails in the drop down vulnerable to spam? (of course, unpublished emails of mine still manage to get spam lol). that’s what i am trying to avoid… i want my cake (the user-select recipient), i want to eat it too (“hidden” email addys). onward, ho!

Profile
 
 
Posted: 16 April 2009 08:51 PM   [ Ignore ]   [ # 15 ]
Newbie
Rank
Total Posts:  20
Joined  2009-03-25
littlered - 16 April 2009 08:46 PM

thanks mdesign. i looked under the ‘hood, and isn’t having the emails in the drop down vulnerable to spam? (of course, unpublished emails of mine still manage to get spam lol). that’s what i am trying to avoid… i want my cake (the user-select recipient), i want to eat it too (“hidden” email addys). onward, ho!

Yep, here’s what I did. The value for the select options in the markup was just an array key that got the whole email from within my extension:

function freeform_module_insert_end ($fields$entry_id$msg)
  
{
    
global $DB$EXT$REGX;

   
// find the correct person to email  
   
$query $DB->query("SELECT * FROM exp_freeform_entries WHERE entry_id = '".$entry_id."' LIMIT 1");
   if ( 
$query->num_rows == )
   
{
    
return;
   
}
   $recipientList 
= array(
     
'ted' => 'gmail.com',
     
'foo' => 'hotmail.com',
     
'bar' => 'thisplace.com',
     
'larry' => 'yahoo.com',
    );
    
$recipient = array();
    
$recipients $query->row['inquiry_subject'];
    
$recipients    preg_split("/,|\|/" $recipients );        
    foreach (
$recipients as $address{
      $recipient[] 
$address '@' $recipientList[$address];
    
}
    
   
// Send email
   
if ( ! class_exists('EEmail'))
   
{
    
require PATH_CORE.'core.email'.EXT;
   
}

   $email 
= new EEmail;
   
$email->wordwrap FALSE;
   
$email->mailtype 'html';

   
$email->initialize();
   
$email->from($msg['from_email']$msg['from_name']); 
   
$email->to($recipient); 
   
$email->subject($msg['subject']); 
   
$email->message($REGX->entities_to_ascii($msg['msg']));  
   
$email->Send();

   unset(
$msg);

   
$EXT->end_script FALSE;
  
Profile
 
 
   
1 of 3
1