Why is SwiftMailer sending two letters?

I send emails through the SwiftMailer PHP library. I have this PHP code to send 1 email to one email recipient from 1 sender. Here is the code:

$email = /*some email recipient*/; $sendEmail = /*sender email*/; $sendName = /*sender 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); 

Each time I run this code, it sends letters from this sender to this letter with this object and body. Two identical letters directly on top of each other. Any idea why?

UPDATE is the complete code:

Here is the whole 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 the page refers to (which is 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); } 
+4
source share
2 answers

This may be due to a logical error when the code using Swift Mailer asks him to send twice.

Check for erroneous loops, recursive function calls, and multiple inclusion and initialization of variables, etc. Swift Mailer says something to send an email twice.

+4
source

For those who have the same problem, use:

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

instead:

  return $this->render(...); 
+1
source

Source: https://habr.com/ru/post/1391760/


All Articles