Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么SwiftMailer要发送两封电子邮件?_Php_Swiftmailer - Fatal编程技术网

Php 为什么SwiftMailer要发送两封电子邮件?

Php 为什么SwiftMailer要发送两封电子邮件?,php,swiftmailer,Php,Swiftmailer,我通过PHP的SwiftMailer库发送电子邮件。我有这个PHP代码从一个发件人向一个电子邮件收件人发送一封电子邮件。代码如下: $email = /*some email recipient*/; $sendEmail = /*sender's email*/; $sendName = /*sender's name*/; $subject = /*email subject*/; $body = /*email body*/; //Create the message //Create

我通过PHP的SwiftMailer库发送电子邮件。我有这个PHP代码从一个发件人向一个电子邮件收件人发送一封电子邮件。代码如下:

$email = /*some email recipient*/;

$sendEmail = /*sender's email*/;
$sendName = /*sender's name*/;
$subject = /*email subject*/;
$body = /*email body*/;

//Create the message
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('/*mail host*/', /*port*/)
->setUsername('/*some username*/')
->setPassword('/*some password*/')
;

//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

//Create a message
$message = Swift_Message::newInstance($subject)
->setFrom(array($sendEmail => $sendName))
->setTo($email)
->setBody($body, 'text/html')
;

//Send the message
$result = $mailer->send($message);
每次我运行这段代码时,它都会将该发件人的电子邮件发送到带有该主题和正文的电子邮件。两封完全相同的电子邮件互相重叠。知道为什么吗

更新-以下是完整的代码:

以下是整个页面:

<?php

ob_start();
session_start();

require_once ('config.php');
require_once 'swiftmailer/lib/swift_required.php';
include ('functions.php');
require_once (MYSQL);

sendConfirmation(12,3,$dbc);

ob_end_flush();
?>
下面是位于functions.php文件中的页面中引用的函数:

function sendConfirmation($signup_id,$app_id,$dbc){

    //get signup email and ref code
    $q = "SELECT email, ref_code FROM sign_ups WHERE (signup_id='$signup_id')";
    $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

    $email;
    $ref;

    if (mysqli_num_rows($r) == 1){
        $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
        $email = $row['email'];
        $ref = $row['ref_code'];
    }

    //get app info (subject, email body, sender email, sender name)
    $q = "SELECT bsignupemail_subj, bsignup_email, email, name, bsignup_url FROM apps WHERE (app_id='$app_id')";
    $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

    $sendEmail;
    $sendName;
    $subject;
    $body;
    $url;

    if (mysqli_num_rows($r) == 1){
        $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
        $url = $row['bsignup_url'];
        $sendEmail = $row['email'];
        $sendName = $row['name'];
        $subject = $row['bsignupemail_subj'];
        $body = $row['bsignup_email'];
    }


    //Create the message
    //Create the Transport
    $transport = Swift_SmtpTransport::newInstance('/*host*/', /*port*/)
      ->setUsername('/*username*/')
      ->setPassword('/*password*/')
      ;

    //Create the Mailer using your created Transport
    $mailer = Swift_Mailer::newInstance($transport);

    //Create a message
    $message = Swift_Message::newInstance($subject)
      ->setFrom(array($sendEmail => $sendName))
      ->setTo(array($email))    
      ->setBody($body, 'text/html')
      ;

    //Send the message
    $result = $mailer->send($message);
}

这可能是由于使用Swift Mailer的代码要求它发送两次的逻辑错误造成的


检查有无错误的循环、递归函数调用和多个include以及变量的初始化等。有东西告诉Swift Mailer发送两次电子邮件

对于有相同问题的人,请使用:

    return $this->redirectToRoute('route', array('parameter'=>$parameter));
而不是:

    return $this->render(...);

你确定你的脚本不会被调用两次吗?在服务器上使用数据包嗅探器,或者非常仔细地检查日志。浏览器扩展通常单独访问页面,用于安全扫描等。来自Google AdSense或其他网站的外部点击也很常见。@Brad我目前只是在本地机器上自行运行脚本,所以我认为这不会影响它。@Brad我只是检查了日志,看起来脚本每次调用两次。。。奇怪-谢谢!这就是我的想法,但我已经检查了我的代码多次,寻找这些类型的错误。我刚刚在答案上贴出了完整的代码。你能看一下吗?与以前相比,这两封邮件内容相同?可能是由于ob_start;,ob_end_冲洗;检查一次没有这些