Page 1 of 1

Code being executed twice and emails turning up in HTML

Posted: Thu May 14, 2009 11:22 am
by Sephirangel
Hello everyone,

Ive been having a problem in my code which has been a constant problem; code is being executed twice.

I believe this may be because the page is loading twice or refreshing once it has loaded. For example; when my email function is called, 2 emails are sent instead of 1. Im using WAMP server 2.0 and the latest version of Firefox.

Another problem, is that the email body is in HTML. A MySQL function obtains data from the local database and places it into a variable e.g. $body but all of the HTML tags used to format the email show up instead of the email beign formatted the way it should be.

Any ideas or help will be greatly appreciated.

Re: Code being executed twice and emails turning up in HTML

Posted: Thu May 14, 2009 11:26 am
by divito
Would need to see some code to attempt to help you.

Re: Code being executed twice and emails turning up in HTML

Posted: Thu May 14, 2009 11:38 am
by Sephirangel
Well for example;

Code: Select all

<?php
    saleEmail();
    echo " Sales have been sent! Redirecting to homepage in 5 seconds...";
?>
This code is being called on a php page, but looks like it is being called twice as the email is being sent twice.
As for the email itself;

Code: Select all

function saleEmail(){
        
        require_once('SwiftMailer/lib/swift_required.php');
        
        $getSalesCont = "SELECT * FROM sales WHERE Sent = '0'";
        $q = mysql_query($getSalesCont);
        
        if($q == ''){
            echo "All Sales Have Already Been Sent!";
        }else{
            $body = "Carolyn, <br /> <br />";
            $body .= "Here are the most recent sales: <br /><br />";
            while($r = mysql_fetch_array($q)){
                $saleN = $r['Sales_Num'];
                $body .= "<b>Sale Number: " . $saleN . " </b><br />";
                $body .= "Date: " . $r['DateTime'] . "<br />";
                $body .= "Buyer ID: " . $r['BuyerID'] . "<br /><br />";
                    $getSaleItems = "SELECT * FROM saleitems WHERE Sales_Num = '" . $saleN . "'";
                    $q2 = mysql_query($getSaleItems);
                
                    while($r2 = mysql_fetch_array($q2)){
                        
                        $body .= "Item Code: " . $r2['ItemCode'] . "<br />";
                        $body .= "Quantity: " . $r2['SaleQuantity'] . "<br />";
                        $body .= "Price Per Item: " . $r2['PricePerItem'] . "<br /><br />";
                        
                    }                   
                    
                $body .= "P&P: " . $r['PostPacking'] . "<br />";
                $body .= "Payment Method: " . $r['Payment_Method'] . "<br />";
                $body .= "Paypal Fee: " . $r['PaypalFee'] . "<br />";
                $body .= "Additional Info: ".$r2['Add_Info']. "<br /><br />";
                $body .= "------------------- <br /><br />";
            }
            
        $transport = Swift_SmtpTransport::newInstance('SMTP.blueyonder.co.uk', 25)
        ->setUsername('xxxxx')
        ->setPassword('xxxxx')
        ;
        
        $mailer = Swift_Mailer::newInstance($transport);
            
        $message = Swift_Message::newInstance('New Knitterbabes Sales')
        ->setFrom(array('xxxxx@blueyonder.co.uk' => 'Joe Bloggs'))
        ->setTo(array('xxxxx@blueyonder.co.uk' => 'John doe'))
        ->setBody($body)
        ;
        
        $result = $mailer->send($message);
        
        if($result){
            changeToSent();
        }else{
            echo "An Error Occured during Email. Please try again!";
        }
            
        }
    }
           
That is the function I am using to construct and send my email.

Hope that helps!

Re: Code being executed twice and emails turning up in HTML

Posted: Thu May 14, 2009 1:03 pm
by Sephirangel
Any suggestions?

Re: Code being executed twice and emails turning up in HTML

Posted: Thu May 14, 2009 1:19 pm
by crazycoders
I don't think it's your email code... could you be using a <input type="submit"> to submit your page but have put some javascript code to validate? Is so, are you doing document.submit() after validation. If so this is why you are getting two emails sent.

1) From the submit button
2) From the document.submit()

Please confirm!

Re: Code being executed twice and emails turning up in HTML

Posted: Thu May 14, 2009 1:38 pm
by Sephirangel
I don't think it's your email code... could you be using a <input type="submit"> to submit your page but have put some javascript code to validate? Is so, are you doing document.submit() after validation. If so this is why you are getting two emails sent.

1) From the submit button
2) From the document.submit()

Please confirm!
I am not using javascript to validate it. The form uses a standalone button which is linked to the page which executes the PHP mail function.

Code: Select all

<center><a href = 'sendMail.php'><button name = 'sendMail'>Send Sales</button></a></center>

Re: Code being executed twice and emails turning up in HTML

Posted: Thu May 14, 2009 1:40 pm
by crazycoders
What is that <button> tag? Is this some kind of framework code that gets replaced into a submit button?

Re: Code being executed twice and emails turning up in HTML

Posted: Thu May 14, 2009 5:50 pm
by Sephirangel
Its like a submit button that isnt used explicitly for submitting forms. Like a blank button.

Re: Code being executed twice and emails turning up in HTML

Posted: Fri May 15, 2009 5:32 am
by Sephirangel
I believe this may have happened due to the screen refreshing itself after loading, like loading twice and therefore executing the code twice. Any full proof effective way of getting round it? Ive tried placing the code in a wrapper with a variable set to 0 on the page before, and incremented at the top of the page where the code is being executed so it will only be carried out if the variable = 2 (so on the second time) but it doesnt always work

Any ideas?