简体   繁体   中英

outbound emails via mailgun are being sent twice

I'm sure this must be a stupid mistake I'm making, but I've been trying to figure it out all day with no success.

I have a helper function in codeigniter (so it can be made available to multiple controllers) whose sole purpose is to dispatch an email to the user using Mailgun. The code works this way:

Controller:

// the variables that are gathered inside $params are defined earlier in the code. This is just an example

// create a view from the template and store it in a variable
$html_text = $this->load->view('email_template', $params, TRUE);

$payload = array(
'email' => $user_email,
'name' => $user_name,
'html_text' => $html_text,
'plaintext' => $other_variable_with_plaintext,
'other_variable' => $other_variable,
);


$result = mailgun_send($payload);

// handle errors depending if $result becomes true or false

Helper:

function mailgun_send($payload)
{
$curl_post_data = array(
        'from'    => 'my FROM name <noreply@domain.com>',
        'to'      => $payload['email'],
        'subject' => 'whatever I use as subject',
        'text'    => $payload['plaintext'],
        'html'    => $payload['html_text'],   
    );

    $service_url = 'https://api.mailgun.net/v3/domain.com/messages';
    $curl = curl_init($service_url);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, "api:OBSCURED_API_KEY"); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 

    $curl_response = curl_exec($curl);
    $response = json_decode($curl_response, true);
    curl_close($curl);

// check mailgun response to handle exceptions later
    if (strpos($response['message'], 'Queued') !== false)
    {
        return true;
    }

    else
    {
        return false;
    }
}

View (email_template): Regular vanilla HTML markup with a couple <php> tags to include the data I'm passing in $params. There's no loops in neither the controller, the view or the helper

When I run this, the email is sent correctly, but the recipient gets two emails.

If I output the view to the screen instead of a variable, it displays correctly.

If I put the mailgun_send function in the same controller where the code that originates the email lives, the mail is sent only once

I can't figure out what's causing the email to be sent twice. Could anyone give me a hand?

First and foremost, I'd like to apologize for posting this as I figured it out for myself.

Even though I checked it many many times, I found that the bug that was causing this was that a debugging function that was buried deep within the code which should have been commented was not, so under a very specific set of conditions, the call to the mailgun_send() function was been made twice.

If a mod was kind enough to kill this question, I'd appreciate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM