为什么 SwiftMailer 会发送两封电子邮件? [英] Why is SwiftMailer sending two emails?

查看:38
本文介绍了为什么 SwiftMailer 会发送两封电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am sending emails via PHP's SwiftMailer library. I have this PHP code to send 1 email to 1 email recipient from 1 sender. Here is the code:

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

每次我运行此代码时,它都会将来自该发件人的电子邮件发送到带有该主题和正文的电子邮件.两封完全相同的电子邮件.知道为什么吗?

Every time I run this code it sends to emails from that sender to that email with that subject and body. Two identical emails right on top of each other. Any idea why?

更新 - 这是完整的代码:

UPDATE - here is the complete code:

这是整个页面:

<?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文件中:

And here is the function that is referenced in the page (that is located in the functions.php file:

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 的代码要求它发送两次的逻辑错误.

This is might be due to a logic error where the code using Swift Mailer is asking it to send twice.

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

Check for faulty loops, recursive function calls and multiple includes and initialization of variables etc. Something is telling Swift Mailer to send the email twice.

这篇关于为什么 SwiftMailer 会发送两封电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆