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:  51
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.

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:  51
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!

Profile
 
 
Posted: 10 November 2008 08:30 PM   [ Ignore ]   [ # 3 ]
Newbie
Rank
Total Posts:  7
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:  51
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.

Profile
 
 
Posted: 12 November 2008 12:16 PM   [ Ignore ]   [ # 5 ]
Newbie
Rank
Total Posts:  7
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. LOL

Profile
 
 
Posted: 12 November 2008 12:22 PM   [ Ignore ]   [ # 6 ]
Member
Avatar
RankRankRank
Total Posts:  51
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.

Profile