0

i'm trying to sendemail using sendemail service; now i want to call the function using conditional:

function after_insert_mail($data)
{
    global $h;
    global $res;
    global $mailin;
    global $sendemail;

    $now = 1;

    function sendemail()
    {
        $mailin = new Mailin('main@mail.com', '***********************');
        $mailin->addTo($email, $name)
            ->setFrom('fromemail@mail.com', 'From this email')
            ->setReplyTo('replyto@mail.com','Reply to')
            ->setSubject('Subject mail')
            ->setHtml('Email Body');

        $res = $mailin->send();
    }

    do 
    {
        sendemail();
    } while ($now == 2);
}

But i get message error:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

Regards!

Resolved using swiftmailer function: https://swiftmailer.symfony.com/docs/introduction.html

c3media
  • 101
  • 1
  • 11
  • 1
    1) globals are hardly **ever** the solution. 2) have you looked into switch statements? – treyBake Jan 02 '20 at 12:15
  • I have done switch and get same error, but if execute directly non conditional works – c3media Jan 02 '20 at 12:26
  • 2
    oh, I've also noticed you declare a function within a function .. – treyBake Jan 02 '20 at 12:27
  • 1
    have you enabled errors? – treyBake Jan 02 '20 at 12:27
  • 1
    A 500 error is a generic error message and covers pretty much every single thing that can go wrong with a PHP script. Check your server error logs to find out the exact error message. – aynber Jan 02 '20 at 12:29
  • Just because you declare the globals in the first function doesn't mean that they'll exist in the scope of the second function. Move the sendemail function declaration outside of after_insert_email, and make sure you pass all necessary variables into the sendemail function – aynber Jan 02 '20 at 12:31
  • 3
    The code you've posted raises a lot of questions. Nested functions are technically valid PHP, but they're almost certainly not doing what you think they are. None of the globals you're importing or the `$data` parameter are used, your `sendemail` function uses two variables that don't exist, and doesn't return its result so nothing else can use it. The `$now` variable in your "conditional" is never modified, so the loop is only ever going to run once. What's your actual desired behaviour here? – iainn Jan 02 '20 at 12:31
  • The creenshot error: https://imgur.com/a/NVkFLg0 – c3media Jan 02 '20 at 12:46
  • @iainn Based in $now variable value, then would execute sendemail function (When $now == 2). – c3media Jan 02 '20 at 12:55
  • @aynber I see that when $now is evaluated as true, then get error, any other idea to resolve it? – c3media Jan 02 '20 at 13:11
  • Yes, completely fix your code. We've already pointed out the errors, such as an unnecessary function declaration inside of the other function, and undefined necessary variables. – aynber Jan 02 '20 at 16:05
  • Problem with Sendinblue Function, so i used swift mailer and resolved: https://swiftmailer.symfony.com/docs/introduction.html – c3media Jan 02 '20 at 16:18

0 Answers0