5 Replies - 313 Views - Last Post: 17 November 2017 - 07:09 AM Rate Topic: -----

#1 mercy_angel  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 340
  • Joined: 28-February 13

swiftmailer why my php sent two mail instead of one

Posted 17 November 2017 - 02:51 AM

i have small php script that send a email if some variable is more than some number, for example 25.
Buit problem is that sent it 2 times instead of one, and cant figure why.

<?php
require_once 'vendor/autoload.php';
function sendMail($temp = "")
{
$transport = (new Swift_SmtpTransport('smtp.example.com', 587))
  ->setUsername('user')
  ->setPassword('pass');

$mailer = new Swift_Mailer($transport);

$message = (new Swift_Message('Something else'))
  ->setFrom(['[email protected]' => 'Subject'])
  ->setTo(['[email protected]' => 'Something'])
  ->setBody("<h1>This is a variable and value is " .  $temp . " percentage. </h1>", 'text/html');
$result = $mailer->send($message);

if (!$mailer->send($message, $failures))
{
  print_r($failures);
}
}




my php code is

<head>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
        <link rel="stylesheet" type="text/css" href="css/main_style.css" />
	<script src="js/jquery-1.11.1.min.js" ></script>
	<script src="js/bootstrap.js" ></script>
        <meta charset="UTF-8">
        <title></title>
<?php include 'mail1.php';?>
    </head> 
<body>
<?php
//File to read
$file = '/sys/file';

//Read the file line by line
$lines = file($file);

//Get the temp from second line 
$temp = explode('=', $lines[1]);

//Setup some nice formatting (i.e. 21,3)
$temp = number_format($temp[1] / 1000, 1, ',', '');

//And echo that temp

//<div class="alert alert-success">

echo '<div class="alert alert-success">' . $temp  . '</div>';


if ((int)$temp > 25)
{
sendMail($temp);
}


?>

</body>



This post has been edited by mercy_angel: 17 November 2017 - 02:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: swiftmailer why my php sent two mail instead of one

#2 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6444
  • View blog
  • Posts: 26,065
  • Joined: 12-December 12

Re: swiftmailer why my php sent two mail instead of one

Posted 17 November 2017 - 04:09 AM

I know zero about swiftmailer but, just looking at your code, you have two send calls, which is why the message is sent twice.

The $failures argument is an optional argument that provides a list of failed recipients. So, essentially, skip line 15.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6444
  • View blog
  • Posts: 26,065
  • Joined: 12-December 12

Re: swiftmailer why my php sent two mail instead of one

Posted 17 November 2017 - 04:16 AM

Question moved to PHP forum.
Was This Post Helpful? 0
  • +
  • -

#4 mercy_angel  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 340
  • Joined: 28-February 13

Re: swiftmailer why my php sent two mail instead of one

Posted 17 November 2017 - 04:18 AM

View Postandrewsw, on 17 November 2017 - 11:16 AM, said:

Question moved to PHP forum.



so you propose to remove
$result = $mailer->send($message);



because i have

if (!$mailer->send($message, $failures))

{

  print_r($failures);

}

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6444
  • View blog
  • Posts: 26,065
  • Joined: 12-December 12

Re: swiftmailer why my php sent two mail instead of one

Posted 17 November 2017 - 05:53 AM

More important than following mechanical steps is that you understand the problem and solution(s).

Problem
Two emails are being sent

Explanation
You call a send() method twice

Resolution
Only call send() once

Which version to call?
If you only want to send a message then send($message);
If you want details of failed recipients then send($message, $failures).
Was This Post Helpful? 1
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3721
  • View blog
  • Posts: 13,512
  • Joined: 08-August 08

Re: swiftmailer why my php sent two mail instead of one

Posted 17 November 2017 - 07:09 AM

View Postandrewsw, on 17 November 2017 - 07:53 AM, said:

More important than following mechanical steps is that you understand the problem and solution(s).

Quoted for emphasis.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1