email sent twice

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
dac6000
Forum Newbie
Posts: 2
Joined: Thu Sep 27, 2007 3:14 pm

email sent twice

Post by dac6000 »

Hi!

I'm using SwiftMailer 3.3.1 and PHP4. I use it to send email for a home-made mailing-list. My problem is that every message is send twice! The website use a homemade too content management, and I use SwiftMailer in it. If I use SwiftMailer outside it, there is no problem. Please help! I'm adding part of the code and the log. Thanks.

Code: Select all

$log =& Swift_LogContainer::getLog();
$log->setLogLevel(4);
                            
/* On ajoute au message un fichier s'il y en a eu un de sélectionné */
if ($_POST['attachment'] != "none")
  {
  $message =& new Swift_Message($subject);
  $message->headers->set("Reply-To",$reply);
  $message_text_part = new Swift_Message_Part($text);
  if ($_POST['html_email'] == "on")
    $message_text_part->setContentType("text/html");
  $message->attach($message_text_part);
  $fileattname = $_POST['attachment'];
  $fileatt = "../data/" . $this->liste['attach']['dir'] . "/" . $fileattname;
  $mimetype = new mimetype();
  $fileattype = $mimetype->getType($fileatt);
  $message->attach(new Swift_Message_Attachment(new Swift_File("../data/" . $this->liste['attach']['dir'] . "/" . $fileattname), $fileattname, $fileattype));
  }
else
  {
  $message =& new Swift_Message($subject,$text);
  $message->headers->set("Reply-To",$reply);
  if ($_POST['html_email'] == "on")
    $message->setContentType("text/html");
  }
// Connection au serveur SMTP
$swift =& new Swift(new Swift_Connection_SMTP("localhost"));
echo "options" . $swift->getOptions() . "<br/>\n";

// On envoit le message
echo "before send<br/>\n";
$message_test =& new Swift_Message("un autre putain de test","on vas-tu le régler ce prob???");
$sent = $swift->send($message_test, "me@me.me", $from);
echo "after send " . $sent . "<br/>\n";

if ($sent > 0)
{echo "Success "; echo $log->dump(true);}
else
echo "Failure ";
Here's the log:

options0
before send
after send 1
Success ++ Log level changed to 4 ++ Trying to connect... ++ Trying to connect to SMTP server at 'localhost:25 << 220 box3.rapidenet.ca ESMTP Exim 4.60 Thu, 27 Sep 2007 13:04:30 -0400 >> EHLO [72.10.166.34] << 250-box3.rapidenet.ca Hello localhost [127.0.0.1] 250-SIZE 20971520 250-PIPELINING 250-AUTH PLAIN LOGIN 250-STARTTLS 250 HELP ++ SMTP extension 'SIZE' reported with attributes [20971520]. ++ SMTP extension 'PIPELINING' reported with attributes []. ++ SMTP extension 'AUTH' reported with attributes [PLAIN, LOGIN]. ++ SMTP extension 'STARTTLS' reported with attributes []. ++ SMTP extension 'HELP' reported with attributes []. >> MAIL FROM: << 250 OK >> RCPT TO: << 250 Accepted >> DATA << 354 Enter message, ending with "." on a line by itself >> >> . << 250 OK id=1Iawms-0008WN-5L ++ Message sent to 1/1 recipients ++ Log level changed to 4 ++ Trying to connect... ++ Trying to connect to SMTP server at 'localhost:25 << 220 box3.rapidenet.ca ESMTP Exim 4.60 Thu, 27 Sep 2007 13:04:30 -0400 >> EHLO [72.10.166.34] << 250-box3.rapidenet.ca Hello localhost [127.0.0.1] 250-SIZE 20971520 250-PIPELINING 250-AUTH PLAIN LOGIN 250-STARTTLS 250 HELP ++ SMTP extension 'SIZE' reported with attributes [20971520]. ++ SMTP extension 'PIPELINING' reported with attributes []. ++ SMTP extension 'AUTH' reported with attributes [PLAIN, LOGIN]. ++ SMTP extension 'STARTTLS' reported with attributes []. ++ SMTP extension 'HELP' reported with attributes []. >> MAIL FROM: << 250 OK >> RCPT TO: << 250 Accepted >> DATA << 354 Enter message, ending with "." on a line by itself >> >> . << 250 OK id=1Iawms-000051-M2 ++ Message sent to 1/1 recipients
john_d
Forum Newbie
Posts: 3
Joined: Fri Jan 11, 2008 3:39 pm

Re: email sent twice

Post by john_d »

I think I have this problem too.
I think that Swift Mailer calls the calling page again somehow, I know this because when it calls it it does so without the POST variables ie its just a bare page call.
Two distinct messages are created with different (Postfix) ID's, but in my case I know somethings going on because the POST variables disappear in the second call.
Again, I am using Swift within my own framework.

PS. I'm pretty sure that this is nothing to do with Swift. It turns out that I'm calling my own page again somehow. Oh lawd - sorry to bother you!
john_d
Forum Newbie
Posts: 3
Joined: Fri Jan 11, 2008 3:39 pm

Re: email sent twice

Post by john_d »

Well, I'll post the answer but its nothing to do with Swift Mailer - but it might help someone.
It is quite easy to set up a form so that it submits twice, once with and once without its POST variables.
One is to put a javascript 'onclick' submit statement on a submit button (pretty daft, but easily done).
Another is to put a blank IMG src within a form, where apparently correct behaviour is to call the parent page, which Firefox does but IE doesn't.
This problem often seems to surface with forms that drive email or database operations.
Mine was even more obsure: a blank javascript file source <script type="text/javascript" src=""></script>) that seem to cause the page to reload. The moral of the story is to make sure that you have no blank URL's in anything and be very careful about javascript submit()s.
A couple of links:
http://forums.mozillazine.org/viewtopic.php?p=3022038&
http://codingforums.com/showthread.php?t=54495
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: email sent twice

Post by Chris Corbyn »

john_d wrote:I think that Swift Mailer calls the calling page again somehow, I know this because when it calls it it does so without the POST variables ie its just a bare page call.
Just to clear up any confusion Swift would never mess around with any functionality on your website, change any settings, reload the page etc... These "send twice" problems always turn out to be down to an error in the logic of the code which is using swift (e.g. a loop running a batchSend(), or a conditional being logically incorrect).

~john_d, glad you figured out what was going on to cause this :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: email sent twice

Post by Chris Corbyn »

~ dac6000, when posting log output, please use "View Source" in your browser so you get one entry per line and can see characters < and >. It's very difficult to read that log and I can't see any addresses, however I *can* see that Swift has already been run *before* that whole block of code you posted.

The call to setLogLevel(4) is happening halfway through that entire log output, yet it happens right at the start of your code. I'd suggest checking any includes, and/or looking at the code earlier in the script compared to what you have posted.
john_d
Forum Newbie
Posts: 3
Joined: Fri Jan 11, 2008 3:39 pm

Re: email sent twice

Post by john_d »

Yeah, sorry Chris for ever having suspected your wonderful mailer!
dac6000
Forum Newbie
Posts: 2
Joined: Thu Sep 27, 2007 3:14 pm

Re: email sent twice

Post by dac6000 »

Chris Corbyn wrote:~ dac6000, when posting log output, please use "View Source" in your browser so you get one entry per line and can see characters < and >. It's very difficult to read that log and I can't see any addresses, however I *can* see that Swift has already been run *before* that whole block of code you posted.

The call to setLogLevel(4) is happening halfway through that entire log output, yet it happens right at the start of your code. I'd suggest checking any includes, and/or looking at the code earlier in the script compared to what you have posted.
Hi!

Thanks for your answer.

The problem was finally solved by changing server. The new server execute the code without a problem, which let me think that it was the old server the problem. Your help was appreciated. Next time I'll post, I'll make sure to be as clear and easy to read as possible.

dac6000
Post Reply