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

时间:2012-01-19 04:11:06

标签: php swiftmailer

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

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

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:1)

对于那些使用相同问题的人:

    return $this->redirectToRoute('route', array('parameter'=>$parameter));

而不是:

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