Forums

Sendig different emails

GMiranda 23 Oct, 2009
Hello,

I have a form with a drop down list field in which the user chooses a Department.

Acording with the choosed Department, the Form should sent an email to a different email address (getting the email address from a table).

I'm trying this in the "On Submit code - before sending email" section of the Form Code tab.
But I don't know how to get the name of the Department choosed by the user.

I guess it's simple but...

Any idea? Any code?

Thanks in advance,
Gonzalo
GreyHead 23 Oct, 2009
Hi Gonzalo,

There's a FAQ about this #36 if I remember correctly.

Bob
GMiranda 23 Oct, 2009
Hi Bob,

Excelent! It's in FAQ 31.
That's what I need, also if I have to work on it a little more, as I want to get the emails from a table and not write them fisically in the code.

Again, Thanks.
Gonzalo
GMiranda 23 Oct, 2009
Hi Bob,

I have worked a lot on this, but I can't get the final result.
The FAQ you suggested has this code for the On submit section:

<?php
$emails_2 = array('em1'=>'sample1@email.com', 'em2'=>'sample2@email.com', 'em3'=>'name_3@example.com', . . .);
$MyForm =& CFChronoForm::getInstance('form_name_here');
$MyFormEmails =& CFEMails::getInstance($MyForm->formrow->id);
$MyFormEmails->setEmailData(1, 'to', $emails_2[$_POST['recipients']]);
?>

If I use it putting the emails manually in the code it works. But I would like to get the emails from a table (in order that they can be changed by the Secretary).

I'm trying putting the emails from the table in a string variable with, a While, and using that variable in the Array. But I get no email address to send.

This is the string I get in the variable $arraq: 'Masters'=>'masters@uprait.org', 'Informazioni Generali'=>'info@uprait.org', 'Diploma'=>'diploma@uprait.org', 'Filosofia'=>'filosofia@uprait.org'

Then I have this:
$emails_2 = array($arraq);

And then the rest of the code:
$MyForm =& CFChronoForm::getInstance('A_test');
$MyFormEmails =& CFEMails::getInstance($MyForm->formrow->id);
$MyFormEmails->setEmailData(1, 'to', $emails_2[$_POST['recipients']]);

I don't know how to put the string into the Array in a correct way.

Any idea?

Thank you,
Gonzalo
GreyHead 25 Oct, 2009
Hi Gmiranda,

I'm going to assume here that the drop-down returns a department id e.g. 'Masters' (I'd probably use a dept ID instead of the name but the code is very similar.

And I'll also assume that you have a table 'dept_table' with columns 'dept_name' and 'dept_email'
<?php
$db =& JFactory::getDBO();
$dept_name =& JRequest::getString('recipients', '', 'post');
if ( $dept_name ) {
  $query = "
    SELECT `dept_email`
      FROM `#__dept_table`
      WHERE `dept_name` = '$dept_name' ;
  ";
  $db->setQuery($query);
  $dept_email = $db->loadResult();
  if ( $dept_email ) {
    $MyForm =& CFChronoForm::getInstance('form_name_here');
    $MyFormEmails =& CFEMails::getInstance($MyForm->formrow->id);
    $MyFormEmails->setEmailData(1, 'to', $dept_email);
  }
}
?>
You'll see that the db query replaces the array so we no longer need that.
Bob

Later: fixed typo in the code
GMiranda 25 Oct, 2009
Hi Bob,

Once again, thaks for your answer.
It's exactly the logic of what I need.
I have tryied the code but I get this error for the 3th line:

Fatal error: Call to undefined method JRequest::gstString() in /web/htdocs/www.uprait.org/home/components/com_chronocontact/libraries/customcode.php(64) : eval()'d code on line 3

I get it also if I put just this code:

<?php
$db =& JFactory::getDBO();
$dept_name =& JRequest::gstString('recipients', '', 'post');
echo $dept_name;
?>

Is there something wrong?
GMiranda 25 Oct, 2009
Hi Bob,

I've noticed a type error: gstString()
I have changed it into getString() and the error disappeared.

I must try yet the rest of the code.

Thanks,

Gonzalo
GMiranda 25 Oct, 2009
Hi Bob,

Just great!

Thank you very very much.

Gonzalo
GreyHead 25 Oct, 2009
Hi Gmiranda,

Great, sorry about the typo :-(

Bob
GMiranda 15 Nov, 2009
Hy Bob,

I must be honest: the typo was mine! You put the correct code, but copying it I made the error. đŸ˜¶

Thanks again,
Gonzalo
jfgarcia 20 Nov, 2009
Hi,
I think I did as you say, but no email is sent...
I enclose a creenshot to give an idea of what I did.

Thanks for your help.[attachment=0]screen.gif[/attachment]
GreyHead 20 Nov, 2009
Hi jfgarcia,

The $emails_2 array must match up exactly to the values in your select box. Yours clearly don't - and you can't have the . . . at the end either.
$emails_2 = array('maire'=>'sample1@email.com', 'Bénédict Rolland'=>'sample2@email.com', 'Bernard Legal'=>'name_3@example.com');

Replace the correct emails and add the extra values.

I also suggest that you don't use accented characters in the values as they can cause problems, better to use 'maire', 'benedict_rolland', ''bernard_legal, etc.

Bob
jfgarcia 20 Nov, 2009
Thank you for that.
I put the right details (see below). However, no email is sent.
Do I have to fill in the "setup emails" header ?

thanks.

[attachment=0]screen.gif[/attachment]
GMiranda 23 Nov, 2009
Hello jfgarcia,

If course you put the option "Email the results ?" into Yes !?
You could try using the Debug (Debug: ON) to see what happens.

Gonzalo
GMiranda 23 Nov, 2009
Hi Bob,

Going back to my code for sending different emails, I would need to send to several email addresses when the user chooses a Department.
In the email table I have them separated with commas, but the code sends de mail only to the first one.
Is it possible to do the trick whit that code?

Thanks!
Gonzalo
GreyHead 23 Nov, 2009
Hi Gonzalo,

I think it should work OK if you have a comma separated list (if not then try with an array).

If you post the code you are using it's easier to comment.

Bob
GMiranda 23 Nov, 2009
Hi Bob,

I copy here the code.
Thanks,
Gonzalo

<?php
    $db =& JFactory::getDBO();
    $dept_name =& JRequest::getString('recipients', '', 'post');

   if ( $dept_name ) {
      $query = "
        SELECT email FROM a_dipartimenti WHERE sottocategoria = '$dept_name' ;
      ";
      $db->setQuery($query);
      $dept_email = $db->loadResult();
      if ( $dept_email ) {
        $MyForm =& CFChronoForm::getInstance('a_richiesta_info');
        $MyFormEmails =& CFEMails::getInstance($MyForm->formrow->id);
        $MyFormEmails->setEmailData(1, 'to', $dept_email);
      }
    }
?>
GreyHead 23 Nov, 2009
Hi Gonzales,

And what is in the database column?

Bob
jfgarcia 23 Nov, 2009

Hello jfgarcia,

If course you put the option "Email the results ?" into Yes !?
You could try using the Debug (Debug: ON) to see what happens.

Gonzalo



Hi GMiranda,

Email the results is on "YES".
I've tried with the debug mode activated and I get:

1. Form passed first SPAM check OK
2. Form passed the submissions limit (if enabled) OK
3. Form passed the Image verification (if enabled) OK
4. Form passed the server side validation (if enabled) OK
5. $_POST Array: Array ( [select_0] => maire [date_2] => 02/11/2009 [text_3] => 14h [text_7] => Garcia [text_6] => JF [text_8] => dfhbdf [text_9] => 35380 [text_10] => Plélan-le-Grand [text_12] => [text_11] => [text_13] => ghf [chrono_verification] => bKZ6x [button_14] => valider [c129b50cd90d2c485efcdc56ff21f660] => 1 [1cf1] => 8e20f62d43563f56a93d90319f0ef5e7 [chronoformname] => rdv_elu )
6. $_FILES Array: Array ( )
7. Form passed the plugins step (if enabled) OK
8. Debug End
9.
Redirect link set, click to test:
http://www.plelan-le-grand.fr/2009/index.php/rdvelu

I don't receive emails and don't quite see what's wrong...
GMiranda 23 Nov, 2009
Hi Bob,

I have a DB with these fields:
- SubCategory (the name of the Department)
- email (the email addresses, separated by comma)

The Email Setup sends an email to a fixed address: [email]segreteria@upra.org[/email]
Then, with the code in the "On Submit code - before sending email:" section the idea is to send different emails according to the field email in the DB.

This is the message from the Debug:

# An email has been SENT successfully from (Gonzalo)g.miranda@arcol.org to [email]segreteria@upra.org[/email]
# An email has been SENT successfully from (Segreteria Generale)segreteria@upra.org to [email]g.miranda@arcol.org[/email]



This is the code in the "On Submit code":
<?php
    $db =& JFactory::getDBO();
    $dept_name =& JRequest::getString('recipients', '', 'post');

   if ( $dept_name ) {
      $query = "
        SELECT email FROM a_dipartimenti WHERE sottocategoria = '$dept_name' ;
      ";
      $db->setQuery($query);
      $dept_email = $db->loadResult();
      if ( $dept_email ) {
        $MyForm =& CFChronoForm::getInstance('a_richiesta_info');
        $MyFormEmails =& CFEMails::getInstance($MyForm->formrow->id);
        $MyFormEmails->setEmailData(1, 'to', $dept_email);
      }
    }
?>


Thank you,
Gonzalo
jfgarcia 26 Nov, 2009
I still can't receive emails when choosing a name in my form.

I have tested on the debug mode and I get this:

   1. Form passed first SPAM check OK
   2. Form passed the submissions limit (if enabled) OK
   3. Form passed the Image verification (if enabled) OK
   4. Form passed the server side validation (if enabled) OK
   5. $_POST Array: Array ( [select_0] => maire [date_2] => 09/11/2009 [text_3] => 14h [text_7] => Garcia [text_6] => JF [text_8] => xwcv [text_9] => 35380 [text_10] => Plélan-le-Grand [text_12] => [text_11] => [text_13] => xcv [chrono_verification] => TCiSe [button_14] => valider [0b8655b951b3d7f1494a5526e3ec077a] => 1 [1cf1] => 74217dd6fbbdd501e19e03a440f47527 [chronoformname] => rdv_elu )
   6. $_FILES Array: Array ( )
   7. Form passed the plugins step (if enabled) OK
   8. Debug End
   9.
      Redirect link set, click to test:
      http://www.plelan-le-grand.fr/2009/index.php/rdvelu


My config is:
[attachment=0]screen.gif[/attachment]

Thanks for your help
jfgarcia 26 Nov, 2009
Do I have to put something in the Form tag attachment field?
[attachment=0]screen.gif[/attachment]

thanks.
GreyHead 26 Nov, 2009
Hi jfgarcia,

You shouldn't need anything there. Is the email enabled in the Email Setup Properties box?

If it is then you will see enabled:1/disabled:0 in the Email column of the Form Manager list.

Bob
jfgarcia 26 Nov, 2009
ok, but I can't turn the email to "enabled". As you can see, it can't be changed:
[attachment=0]screen.gif[/attachment]
jfgarcia 27 Nov, 2009

ok, but I can't turn the email to "enabled". As you can see, it can't be changed:
[attachment=0]screen.gif[/attachment]



Any idea how I could put it to enabled?
thanks.
nml375 27 Nov, 2009
Hi jfgarcia,
Both your email templates are lacking the mandatory 'From Email', 'From Name', and 'Subject' properties (or their dynamic counterpart). You will not be able to enable a email template without all these properties, and the underlying mailer will generally not be able to send the email without proper values assigned to them.

/Fredrik
GMiranda 28 Nov, 2009
Hi,

That's the trick!
You must put all those parameters in the email templates. When you put them, the box changes it's colour. Then you can enable the email.
I had the same problem some time ago, but I did not remember that.
I found the trick reading one of the pdf that you can download ("Adding emails to a form in ChronoForms"). Sometimes it's not a bad idea to read de manuals...

Gonzalo
jfgarcia 29 Nov, 2009
Thank you guys, I will try that and let you know...

Cheers.
JF
jfgarcia 30 Nov, 2009
Greeeaaat!
That was the thing, it works now.

thanks again.
Cheers.
JF
This topic is locked and no more replies can be posted.