Mails optional with BCC

Hi there,

small Question, I want that the BCC of the Mail sending is optional, but can’t find out how to manage.

$kirby->email([
      'from' => "$mail_from",
      'replyTo' => $mail_reply_to,
      'to' => "$mail_to"
      
      // I know this is completely wrong, but how to manage such a case?

      if ( $page->mail_bcc()->isNotEmpty ) {
        'bcc' => $page->mail_bcc(),
      }

      'subject' => "$mail_subject",
      'body'=> "$mail_text"
]);

The logic muss be part of the value, you can’t just put it inside the array. This should work:

'bcc' =>  $page->mail_bcc()->isNotEmpty() ? $page->mail_bcc()->html() : null,

An alternative way to accomplish this would be to add an item conditionally to the array:

$email = [
      'from'      => $mail_from,
      'replyTo'   => $mail_reply_to,
      'to'        => $mail_to,
      'subject'   => $mail_subject,
      'body'      => $mail_text
];

if ($page->mail_bcc()->isNotEmpty()) {
     $email['bcc'] = $page->mail_bcc()->html(),
}

$kirby->email($email);

You don’t want those quotes around your variables, either.

Very strange, I have tried with null but it hasn’t worked. But now, very fine! Now I can place the logic outside … I personally don’t like the if/else Shortcode

if ( $page->mail_bcc()->isNotEmpty ) {
  $bcc = $page->mail_bcc(),
} else {
  $bcc = null;
}

$kirby->email([
      'from' => "$mail_from",
      'replyTo' => $mail_reply_to,
      'to' => "$mail_to"
      'bcc' => $bcc,
      'subject' => "$mail_subject",
      'body'=> "$mail_text"
]);

Another small Question, you wrote that I don’t need the quotes around my variables. This was also my thought. But without, it doesn’t work. Any idea why?

It’s not really wrong, just unnecessary. And I don’t understand why you use them around some variables but not the others.

And no, I don’t know why it doesn’t work or what makes you think it doesn’t work

On a side note, I’d prefer the ternary operator I suggested in the first example to the if-else-statement…

Cause only these fields make problems when I do not wrap them. Example:

$mail_from = $page->mail_from();

An when I use in the email array without quotes it doesn’t work.

13

Yes, but the reason is that you pass a field object instead of a string, the better way to do that would be to explicitly store a string in your variable.

$mail_from = $page->mail_from()->html(); // or ->value() if you don't want to escape

That was also my idea, but with $page->mail_from()->html() it also doesn’t work! But I have tested with “value” and this is working :thinking:

Oh, right, sorry, the html method also returns the field, not a string, to enable syntax stringing…

Ahh ok! But generally it’s not wrong to wrap the Variables in Quotes?

Another small Question. I tried to create a function in this template (to replace the word {url} with the current page url)

function url_replace($word) {
  echo str_replace('{url}',$page->url(),$word);
}

But theres a PHP error that the variable page is not available? You know why?

Yes, it’s because $page variable is not known in the scope of your function. You either have to define it within your function or pass it as a parameter.

https://www.php.net/manual/de/language.variables.scope.php

Sorry that I have to annoy you so much but do you have an example of this? I always have massive problems with the PHP Docs :frowning:

No problem. Example for what? How to pass the page as parameter?

Exactly :slight_smile: That this function works within that template

Here you go:

function url_replace($word, $page) {
  echo str_replace('{url}',$page->url(),$word);
}

$word = 'This example demos  some string replacement {url}';
echo url_replace($word, $page);

Or defining your variable inside the function using the page() helper (without a parameter it refers to the current page)

function url_replace($word) {
  $page = page();
  echo str_replace('{url}',$page->url(),$word);
}

$word = 'This example demos  some string replacement {url}';
echo url_replace($word);

Hmmm, it’s very similar to my own try, but this doesn’t work … I thought I can save the time to enter everytime the $page to the function. Of course, yours is working :smiley:

function url_replace($word, $page = $page) {
  echo str_replace('{url}',$page->url(),$word);
}

$word = 'This example demos  some string replacement {url}';
echo url_replace($word);

See my second example.

Well, your example doesn’t work because you are trying to set the default of a parameter to a variable that is not defined.

So simple :slight_smile: thanks so much Sonja, you’re my Kirby Hero :smiley:
Have a nice weekend!

Thanks, you too!