繁体   English   中英

通过mailgun发送出站电子邮件两次

[英]outbound emails via mailgun are being sent twice

我确定这肯定是我犯的一个愚蠢的错误,但是我一直试图整天弄清楚这一点,但没有成功。

我在codeigniter中有一个辅助功能(因此它可以供多个控制器使用),其唯一目的是使用Mailgun向用户发送电子邮件。 代码以这种方式工作:

控制器:

// 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

帮手:

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;
    }
}

查看(email_template):带有两个<php>标记的常规普通HTML标记,以包含我在$ params中传递的数据。 控制器,视图或帮助器中都没有循环

当我运行此命令时,电子邮件已正确发送,但是收件人收到了两封电子邮件。

如果我将视图而不是变量输出到屏幕,它将正确显示。

如果我将mailgun_send函数放在发出电子邮件的代码所在的同一控制器中,则该邮件仅发送一次

我不知道是什么原因导致两次发送电子邮件。 有人可以帮我吗?

首先,我想为此道歉,为此我深表歉意。

即使我检查了很多次,我仍然发现导致该错误的错误是,应将其深层隐藏在应该注释的代码中,因此在一组非常特定的条件下,对mailgun_send()函数进行了两次。

如果一个mod足以解决这个问题,我将不胜感激

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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