4

Create optional message parts to Laravel's Mail function?

E.g. either adding attachment or having bcc.

Mail::send('emails.welcome', $data, function($message)
{
    $message->from('us@example.com', 'Laravel');

    $message->to('foo@example.com')->cc('bar@example.com');

    $message->attach($pathToFile); <========= OPTIONAL =========
});

I REALLY want to avoid ugly code like this

if (isset($data['attachment'])) {
    // Attachment
    Mail::queue('emails.raw', ['content' => $content], function($message) use ($data) {
        $message
            ->subject($data['subject'])
            ->attach($data['attachment'])
            ->to($data['email'])
            ->bcc($data['bcc']);
        });
}
else{
    // No attachment
    Mail::queue('emails.raw', ['content' => $content], function($message) use ($data) {
        $message
            ->subject($data['subject'])
            ->to($data['email'])
            ->bcc($data['bcc']);
        });
}
Peder Wessel
  • 646
  • 1
  • 9
  • 23

2 Answers2

6

You can just move the if condition into the closure:

Mail::queue('emails.raw', ['content' => $content], function($message) use ($data) {
    $message
        ->subject($data['subject'])
        ->to($data['email'])
        ->bcc($data['bcc']);

    if (isset($data['attachment'])) {
        $message->attach($data['attachment']);
    }
});

Note that the chaining of methods just syntactical sugar. The above is equivalent to:

Mail::queue('emails.raw', ['content' => $content], function($message) use ($data) {
    $message->subject($data['subject']);
    $message->to($data['email']);
    $message->bcc($data['bcc']);

    if (isset($data['attachment'])) {
        $message->attach($data['attachment']);
    }
});

So write it however you like.

Jeroen Noten
  • 3,574
  • 1
  • 17
  • 25
0

Remember that the part where you are currently adding the from/to etc is simply just a function like any other, it's just that it's referred to as an anonymous function, as it has no defined name.

Therefore, you can just place your conditional logic within the function:

$body = 'YOUR_BODY_HERE';
$attachment = 'YOUR_ATTACHMENT_FILEPATH_HERE';

Mail::send('emails.welcome', ['content' => $body], function($message) use ($attachment)
{
    $message->from('us@example.com', 'Laravel');

    $message->to('foo@example.com')->cc('bar@example.com');

    if ($attachment) {
        $message->attach($attachment);
    }
}
Tom Folk
  • 123
  • 7
  • Thanks Tom. Out of curiosity would not your 'if ($attachment)' need to be TRUE to work? Should it not be 'if (isset($attachment))'? – Peder Wessel Apr 15 '16 at 15:23
  • Remember that in PHP it can evaluate all variables as "truthy/falsey" (see the accepted answer here: http://stackoverflow.com/questions/2382490/how-does-true-false-work-in-php). My code is assuming that if you didn't have an attachment, then you would have set the `$attachment` variable to be either null or an empty string - and therefore it will have been defined – Tom Folk Apr 19 '16 at 14:05
  • If you are happy with the answer, would you mind up-voting please! – Tom Folk Apr 21 '16 at 10:25