Code being executed twice and emails turning up in HTML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sephirangel
Forum Commoner
Posts: 45
Joined: Tue Jul 15, 2008 1:37 pm

Code being executed twice and emails turning up in HTML

Post 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.
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

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

Post by divito »

Would need to see some code to attempt to help you.
Sephirangel
Forum Commoner
Posts: 45
Joined: Tue Jul 15, 2008 1:37 pm

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

Post 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!
Last edited by Benjamin on Thu May 14, 2009 12:17 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Sephirangel
Forum Commoner
Posts: 45
Joined: Tue Jul 15, 2008 1:37 pm

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

Post by Sephirangel »

Any suggestions?
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

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

Post 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!
Sephirangel
Forum Commoner
Posts: 45
Joined: Tue Jul 15, 2008 1:37 pm

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

Post 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>
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

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

Post by crazycoders »

What is that <button> tag? Is this some kind of framework code that gets replaced into a submit button?
Sephirangel
Forum Commoner
Posts: 45
Joined: Tue Jul 15, 2008 1:37 pm

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

Post by Sephirangel »

Its like a submit button that isnt used explicitly for submitting forms. Like a blank button.
Sephirangel
Forum Commoner
Posts: 45
Joined: Tue Jul 15, 2008 1:37 pm

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

Post 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?
Post Reply