问题
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:
Here is the entire page:
<?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();
?>
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);
}
回答1:
This is might be due to a logic error where the code using Swift Mailer is asking it to send twice.
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.
回答2:
For those who have the same issue use:
return $this->redirectToRoute('route', array('parameter'=>$parameter));
instead of:
return $this->render(...);
来源:https://stackoverflow.com/questions/8920920/why-is-swiftmailer-sending-two-emails