Forums

Mail sending code in two dropdown at many adresse with conditions

dieudo 14 Mar, 2014
Hi Bob,

I wonder if we can simplify the code below because if I send for many variables (em2, 3,4,5,6,7,8,9) at the same address. And most importantly, I want to know how to create conditions among its variables. Example: if there is (EM4 + EM10) or (+ EM6 EM10) then send exemple@exemple.com.

Again thank you.

Eric

<?php
$recipient = JRequest::getString('recipients', '', 'post');
$emails = array (
'em1' => 'exempleA@hotmail.com',
'em2' => 'exempleB@orange.fr',
'em3' => 'exempleB@orange.fr',
'em4' => 'exempleC@orange.fr',
'em5' => 'exempleC@orange.fr',
'em6' => 'exempleB@orange.fr',
);
$email_to_use = $emails[$recipient];
$form->data['email_to_use'] = $email_to_use;
 
$recipient2 = JRequest::getString('recipients2', '', 'post');
$emails2 = array (
'em7' => 'exempleB@orange.fr',
'em8' => 'exempleC@orange.fr',
'em9' => 'exempleB@orange.fr',
'em10' => 'esempleA@hotmail.com',
 
);
$email_to_use2 = $emails2[$recipient2];
$form->data['email_to_use2'] = $email_to_use2;
?>
GreyHead 17 Mar, 2014
Hi Eric,

Your specification is very vague so it's hard to give you a specific reply.What exactly is the logic that you want to use?

Here's my attempt at an answer - but it may not answer your question :-(

The first thing to notice is that the standard Email action in CFv4 will not let you send to more than one dynamic To Email address. If you need to do that you will want to get my Email [GH] action which will accept an array of email addresses.

Then your code will be like this:
<?php
$emails_array = array();

$emails = array (
  'em1' => 'exempleA@hotmail.com',
  'em2' => 'exempleB@orange.fr',
  'em3' => 'exempleB@orange.fr',
  'em4' => 'exempleC@orange.fr',
  'em5' => 'exempleC@orange.fr',
  'em6' => 'exempleB@orange.fr',
);
$recipients = $form->data['recipients'];
if ( is_array($recipients) ) {
  foreach ( $recipients as $r ) {
    $emails_array[] = $emails[$r];
  }
} else {
  $emails_array[] = $emails[$recipients];
}

$emails2 = array (
  'em7' => 'exempleB@orange.fr',
  'em8' => 'exempleC@orange.fr',
  'em9' => 'exempleB@orange.fr',
  'em10' => 'exempleA@hotmail.com',
);

$recipients2 = $form->data['recipients2'];
if ( is_array($recipients2) ) {
  foreach ( $recipients2 as $r ) {
    $emails_array[] = $emails[$r];
  }
} else {
  $emails_array[] = $emails[$recipients2];
}

if ( ( $recipient == 'em4' || in_array('em4', $recipent) ) && ( $recipent2 == 'em10' || in_array('em10', $recipent2) ) ) {
  $emails_array[] = 'some_email@example.com';
}

if ( ( $recipient == 'em6' || in_array('em6', $recipent) ) && ( $recipent2 == 'em10' || in_array('em10', $recipent2) ) ) {
  $emails_array[] = 'some_email_b@example.com';
}

// remove any duplicate entries
$emails_array = array_unique($emails_array);

// 
make sure that there is at least one email
if ( count($emails_array) <= 0 ) {
  $emails_array[] = 'default_email@example.com';
}
$form->data['email_to_use'] = $emails_array;
?>

Bob
dieudo 18 Mar, 2014
I'll try to clarify my question.
I have two dropdown:
The first (containers) offers skill levels:
em1 = Official Commissioner Track
em2 = Official Steward
em3 = Official Race Director
EM4 = Official Scrutineer
EM5 = Official Commissioner Zone
EM6 = Official Timekeeper

The second (recipients2) Disciplines:
em7 = Enduro
EM8 = Motocross
EM9 = Trial
EM10 = Speed

Depending on the choices made, an email must be sent to different addresses:
When "Speed" is selected the mail must always be sent to: exempleA@exemple.com. Except when "Official Timekeeper" or "Scrutineer" are selected, then the mail is to be sent to exempleB@exemple.com. For all other choices the mail must be sent to exempleC@exemple.com.

I have undestand that I must use your Email [GH] action, and i do that.


I'm working on the code that you suggested me to get the desired result, but for now I get: No valid To Email address found in the report of "debug data".

Thanks
Erick
GreyHead 18 Mar, 2014
Hi Eric,

And what is the value of 'email_to_use' in the debug output?

Bob
dieudo 18 Mar, 2014
Hi Bob,
My debug data are :

Data Array:

Array
(
[option] => com_chronoforms
[tmpl] => component
[chronoform] => Multimails
[event] => submit
[Itemid] =>
[input_text_1] =>
[input_text_2] =>
[recipients] => em4
[recipients2] => em10
[input_submit_4] => Envoyer
[32c763b6cb29c5fd4e2733940e324e4a] => 1
[IPADDRESS] => 82.XX.XX;XX
)

Validation Errors:

Array
(
)

Merci pour votre inscription
Debug Data

Email errors
No valid To Email address found
Email info
Email send stopped.
From: (admin) info@www.exemple.org
To:
Subject: multimails
Email body
Nom
email
service em4


Eric
GreyHead 18 Mar, 2014
Hi Eric,

As you see 'email_to-use' isn't there at all so you need to debug your code and work out what is happening.

Bob
dieudo 18 Mar, 2014
Hi bob,
I don't know why, but when i erased the conditions :

if ( ( $recipient == 'em4' || in_array('em4', $recipent) ) && ( $recipent2 == 'em10' || in_array('em10', $recipent2) ) { $emails_array[] = 'exemple@hotmail.com'; }
if ( ( $recipient == 'em6' || in_array('em6', $recipent) ) && ( $recipent2 == 'em10' || in_array('em10', $recipent2) ) { $emails_array[] = 'exemple@orange.fr'; }

I have :
[email_to_use] =>
and :
Email sent successfully


Perhaps one part of code is not valide for joomla 2.5?
dieudo 18 Mar, 2014
Hi Bob,

I have find ! The probleme is : two ")" missing for closed de code at the end of the "if" betwen $recipent2) ) ")" { $email...

Thank you again Bob.
Eric
GreyHead 19 Mar, 2014
Hi Eric,

Well found - sorry about my typos. That's a problem with typing code directly into the forum :-(

Bob
This topic is locked and no more replies can be posted.