1

I've recently inherited some websites from the developer that was here before me, and I'm having trouble adding a simple IF statement to his code.

The page simply says that "an error has occurred" and there's nothing being written to the logs folder.

Here is the code:

protected function sendContactEmail($data)
{
    $enquiry = $data['enquiry'];
    $email = $data['email'];
    $name = $data['name'];

    if($enquiry=='General Enquiry'){
        $to = '********';
        $cc = '********';
    }
    else if($enquiry=='Product Feedback'){
        $to = '********';
        $cc = '********';
    }
    else { //Trade Enquiry
        $to = '********';
    } 

    //email to admin
    Mail::send('contact.email.contact-admin', $data, function ($message) use ($enquiry) {
        $message->from('no-reply@********.com.au', '********');
        $message->replyTo('no-reply@********.com.au', '********');
        $message->subject($enquiry);
        $message->to($to);
        $message->cc($cc);
    });

    //email to user
    Mail::send('contact.email.contact-user', $data, function ($message2) use ($email) {
        $message2->from('no-reply@********.com.au', '********');
        $message2->replyTo('no-reply@********.com.au', '********');
        $message2->subject('********');
        $message2->to($email);
    });
}

Anyone got any ideas on where to start?

4
  • What version of Laravel are you using? Is your environment set to DEV? Commented Feb 5, 2019 at 2:26
  • 1
    $cc isn't given a default value. When $enquiry is matches none of the conditions, $cc is never initialized. Commented Feb 5, 2019 at 2:28
  • @fubar v5.4.16 and no. Commented Feb 5, 2019 at 2:56
  • @OluwafemiSule - I didn't know they need to be initialised. When the script tries to use the variable, it will always have been defined by that point. However, I added defaults anyway and it hasn't fixed the issue. Commented Feb 5, 2019 at 2:58

1 Answer 1

5

Both $to and $cc variables are not inherited from the parent scope of the Closure passed in Mail::send.

Both variables have to be specified in the use language construct. 1

Mail::send('contact.email.contact-admin', $data, function ($message) use ($enquiry, $to, $cc) {
    $message->from('no-reply@********.com.au', '********');
    $message->replyTo('no-reply@********.com.au', '********');
    $message->subject($enquiry);
    $message->to($to);
    $message->cc($cc);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.