2

I am using Joomla and SP Page Builder. I have back-end side and front-end side. I would like to add multiple recipients for sent emails from my contact form. I have found related codes for sending emails. What should be edited to add multiple email addresses on my back-end? I have tried to play with

$recipients = array(
  "[email protected]",
);
$email_to = implode(',', $recipients); 

with no success

Here is my front-end code:

$recipient_email = (isset($this->addon->settings->recipient_email) && $this->addon->settings->recipient_email) ? $this->addon->settings->recipient_email : '';

Here is what I have in back-end:

    'recipient_email'=>array(
        'type'=>'text',
        'title'=>JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_RECIPIENT_EMAIL'),
        'desc'=>JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_RECIPIENT_EMAIL_DESC'),
        'std'=>'[email protected]'
    ),

1 Answer 1

2

I've solved it this way...

1) Create an override folder (if not existing) in your template folder for the Contact Form add-on. Copy the content from

<joomla_root_path>/components/com_sppagebuilder/addons/ajax_contact

to

<joomla_root_path>/templates/sppagebuilder/addons/ajax_contact

2) In the admin.php file add the following code after 'recipient_email' array

'recipient_email_2' => array(
  'type' => 'text',
  'title' => JText::_( 'COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_RECIPIENT_EMAIL' ) . ' 2',
  'desc' => 'This is another recipient email address. Optional.',
  'std' => '[email protected]'
), 

3) In the site.php file add the following code.

In the public function render() Under the //Addon options add the following code:

$recipient_email_2 = ( isset( $this->addon->settings->recipient_email_2 ) && $this->addon->settings->recipient_email_2 ) ? $this->addon->settings->recipient_email_2 : '';

Then search for this line:

$output .= '<input type="hidden" name="recipient" value="' . base64_encode( $recipient_email ) . '">';

And add the following below:

$output .= '<input type="hidden" name="recipient-2" value="' . base64_encode( $recipient_email_2 ) . '">';

In the public function getAjax() under the //inputs search for this code:

  if ( $input[ 'name' ] == 'recipient' ) {
    $recipient = base64_decode( $input[ 'value' ] );
  }

Proceed to add this below:

  if ( $input[ 'name' ] == 'recipient-2' ) {
    $recipient_2 = base64_decode( $input[ 'value' ] );
  }

Then search for this line:

$mail->setSender( $sender );

And add this code below:

if ( isset( $recipient_2 ) && !empty( $recipient_2 ) ) {
  $recipientArray = array( $recipient, $recipient_2 );
  $mail->addRecipient( $recipientArray );
} else {
  $mail->addRecipient( $recipient );
}

That's it! You should see a new field in the administrator and it should works as expected with one recipient or two.

enter image description here

Hope it helps! Regards.

3
  • Some facts about checking if a variable is set and/or empty... $recipient_email_2 = ( isset( $this->addon->settings->recipient_email_2 ) && $this->addon->settings->recipient_email_2 ) ? $this->addon->settings->recipient_email_2 : ''; is written more succinctly as $recipient_email_2 = empty($this->addon->settings->recipient_email_2) ? $this->addon->settings->recipient_email_2 : ''; or even better with the null coalescing operator $recipient_email_2 = $this->addon->settings->recipient_email_2 ?? '';. Oct 4, 2019 at 20:27
  • Also, if ( isset( $recipient_2 ) && !empty( $recipient_2 ) ) { is performing a redundant check. empty() does 2 things: it checks if the variable is not set AND is contains a false-y/empty/zero-ish/null value. For this reason isset() can be safely omitted. if (!empty($recipient_2 )) { Read more on this topic... Oct 4, 2019 at 20:30
  • 1
    @mickmackusa thanks for your comments, they help us improve our coding practices!
    – Pablo
    Oct 9, 2019 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.