Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Php/MySQL mail problem??

Options
  • 16-05-2006 11:27am
    #1
    Closed Accounts Posts: 8,866 ✭✭✭


    Hey guys, lookin for a bit of help with this code:

    [PHP]$sql="SELECT * FROM tbl_mail_list";
    //debug($sql);
    $result = mysql_query($sql) or die("Query failed : " . mysql_error());
    $num_rows = mysql_num_rows($result);
    while($row=mysql_fetch_assoc($result)){



    $recipient=$row;
    $name=$row;

    }


    $message1= "This is message number 1 and it is intended for $name";
    $message2= "This is message number 2 and it is intended for $name";
    $message3= "This is message number 3 and it is intended for $name";


    $to=$recipient;
    $subject="email";
    $headers = "From: blah@blah.com\n";


    for($i=0;$i<$num_rows;$i++) {
    $row = mysql_fetch_array($result, MYSQL_ASSOC);


    if($row== 0){

    $body=$message1;

    mail($to, $subject, $body, $headers);

    }


    if($row== 1){

    $body=$message2;

    mail($to, $subject, $body, $headers);

    }


    if($row== 2){

    $body=$message3;

    mail($to, $subject, $body, $headers);

    }
    }

    ?>[/PHP]

    I think its fairly easy to understand but basically whats its meant to do is select a bunch of people's emails from the sql table and email them. But there are a sequence of emails from 1 to 3, and if they have received email 1 they should next receive email 2, not email 1 again. And I cant see why its not working at the moment, though to be honest that doesnt stand for much! :o Any suggestions? Cheers!


Comments

  • Banned (with Prison Access) Posts: 5,154 ✭✭✭Oriel


    Could you not increment the postcount after each e-mail has been sent, then just e-mail the appropriate message number for each e-mail address?

    S.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    I love when people say things I can understand in theory but have no idea where to start with in practice! :confused:

    Anyone else got any help? Alas, sinecurea never came back!


  • Registered Users Posts: 683 ✭✭✭Gosh


    This should work ...
    <?
    $sql="SELECT * FROM tbl_mail_list";
                //debug($sql);
    $result = mysql_query($sql) or die("Query failed : " . mysql_error());
    $num_rows = mysql_num_rows($result);
    while($row=mysql_fetch_assoc($result)){
          
       $recipient=$row['ml_recipient'];
       $name=$row['ml_name'];
    
       $message1= "This is message number 1 and it is intended for $name";
       $message2= "This is message number 2 and it is intended for $name";
       $message3= "This is message number 3 and it is intended for $name";
    
    
       $to=$recipient;
       $subject="email";
       $headers = "From: blah@blah.com\n";
       ini_set ("SMTP", "your_SMTP_server");
    
        if($row['ml_message']==0){
    
            $body=$message1;
    
            mail($to, $subject, $body, $headers);
    
        }
    
    
    
        if($row['ml_message']== 1){
    
            $body=$message2;
            
            mail($to, $subject, $body, $headers);
    
        }
    
    
        if($row['ml_message']== 2){
    
            $body=$message3;
    
            mail($to, $subject, $body, $headers);
    
        }
    }
    
    ?>
    


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    No, that didn't make a difference. However my problem isn't with using the mail function, I can work that out later. Its the actual script to send the emails to the addresses.

    What I mean is, I have a sql table, tbl_mail_list, with a few fields, and emaple of a row would be:

    ml_id ml_name ml_recipient ml_message

    1 John john@server.net 2

    So John should receive email number 3 when the script runs, as ml_message = 2 so he has received email number 2. Follow me?

    At the moment it doesn't work.


  • Registered Users Posts: 683 ✭✭✭Gosh


    When you say it doesn't work can you say what results you get?


  • Advertisement
  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Yeah I have four test rows in my sql table in the format shown above. But when the scripts runs with the right mail settings it sends four emails, which is right, but it uses just one email address, the one on the fourth row in the table. And it sends the first email message when it should use the 3rd, as the value of ml_message in the fourth row is 2.


  • Registered Users Posts: 683 ✭✭✭Gosh


    But when the scripts runs with the right mail settings it sends four emails, which is right, but it uses just one email address, the one on the fourth row in the table.

    In your original code you would have got this because you had a closed WHILE after the SQL fetch that would have stopped you at the last (fourth) row and then you followed this by a FOR loop that sent an email but you did not set the name or recipient again in this loop.

    I presume you replaced your original code with my posted code?

    Try adding the echo statements in bold below - I got this code to work on a test table with no problems
    <?php
    $sql="SELECT * FROM tbl_mail_list";
                //debug($sql);
                $result = mysql_query($sql) or die("Query failed : " . mysql_error());
                $num_rows = mysql_num_rows($result);
                while($row=mysql_fetch_assoc($result)){
            
                
    
    $recipient=$row['ml_recipient'];
    $name=$row['ml_name'];
    
    
    $message1= "This is message number 1 and it is intended for $name";
    $message2= "This is message number 2 and it is intended for $name";
    $message3= "This is message number 3 and it is intended for $name";
    
    
    $to=$recipient;
    $subject="email";
    $headers = "From: blah@blah.com\n";
    
    [B]echo $recipient . " " . $name . " " . $row['ml_message'] ;[/B]
    
        if($row['ml_message']==0){
            $body=$message1;
            [B]echo "sending email_1 to " . $to;[/B]
            //mail($to, $subject, $body, $headers);
    
        }
    
    
        if($row['ml_message']==1){
    
            $body=$message2;
            [B]echo "sending email_2 to " . $to;[/B]
            //mail($to, $subject, $body, $headers);
    
        }
    
    
        if($row['ml_message']==2){
            [B]echo "sending email_3 to " . $to;[/B]
            $body=$message3;
    
            //mail($to, $subject, $body, $headers);
    
        }
    }
    
    ?>
    


  • Registered Users Posts: 683 ✭✭✭Gosh


    BTW - I've commented out the mail statements in the code this time


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Thats the trick, cheers Gosh!


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    just a quick question,
    is it possible to use a phpsecret from a remote server, like lets say have the .php script on tripod.com and have your html page etc on some other page which uses the .php script?


  • Advertisement
  • Registered Users Posts: 683 ✭✭✭Gosh


    PHP is server-side and HTML-embedded. This means it exists within the HTML page and the PHP code is executed on the server as it builds the page to send back to your browser.


  • Closed Accounts Posts: 169 ✭✭akari no ryu


    Gosh wrote:
    PHP is server-side and HTML-embedded.
    PHP is not HTML embedded.
    PHP can embed HTML.
    It does not exist within the html page.
    PHP returns html, sgml, xml, png, gif, jpeg, tiff, pdf... well the list is fairly extensive.. as decided by the content-type. It may default to html but you can change even that behaviour.
    Gosh wrote:
    This means it exists within the HTML page
    Urm... no.
    HTML is just a HyperText Markup Language. It does not exist within the HTML page. The HTML page is the result. Boards.ie runs on php, the results are html pages. There is no php in the html.
    There is, however, html in the php. And, at that, that's only assuming that invision doesn't adhere to some sort of MVC approach, I wouldn't know... I've never seen the source code.
    That, and you're ignoring the existance of things like the Smarty Templating engine.
    Gosh wrote:
    and the PHP code is executed
    The PHP is not executed, it is parsed.


  • Registered Users Posts: 683 ✭✭✭Gosh


    Originally posted by akari no ryu
    PHP is not HTML embedded.
    Originally posted by akari no ryu
    It does not exist within the html page.

    See www.php.net
    Google define : PHP
    Originally posted by akari no ryu
    The PHP is not executed, it is parsed.

    Google define : parse

    It is parsed (analysed for syntax) and the results of this are actions that are executed by PHP on the server.


  • Closed Accounts Posts: 169 ✭✭akari no ryu


    Gosh wrote:
    Wouldn't you know it?
    PHP.net agree with me.

    Even the front page, which states that PHP CAN be embedded within HTML, isn't fully agreeing with you and it was that correction that I was making.
    Hence the sgml, xml, png, gif, pdf, etc reference.
    Since none of the above use html anywhere in their creation, stating that PHP is embedded in HTML is factually incorrect.
    Gosh wrote:
    Google define : parse

    It is parsed (analysed for syntax) and the results of this are actions that are executed by PHP on the server.
    I see your google and raise you a wikipedia


  • Closed Accounts Posts: 113 ✭✭bartificer


    Gosh wrote:
    PHP is server-side and HTML-embedded. This means it exists within the HTML page and the PHP code is executed on the server as it builds the page to send back to your browser.
    I'll just step in here and agree with Akari here.

    It is a true fact that there is a shed load of really bad PHP code out there that is just a HTML page with some PHP lobbed into it. That does not mean that php is embeded in a HTML page.

    Have a look at the php source for a major PHP app like phpBB and you'll notice that there is not a single HTML tag in the php source. All the HTML is in separate template files and what PHP does it is does it's work, populates some data structures, then fetches the template file and combines it with the datastructures to generate a response for the browser.

    That's not HTML embeded code. It's not quite MVC either but that's another argument. Saying PHP is HTML embeded is quite simply false. It CAN be but that is not at all the same thing. A fish can be a macrel but a fish is not a macrel!


  • Registered Users Posts: 683 ✭✭✭Gosh


    Originally posted by akari no ryu

    PHP is not HTML embedded.

    PHP.net agree with me.
    <?php
      if ("PHP is not HTML embedded." == "PHP  is a widely-used general-purpose scripting language that is  especially suited for Web development and can be embedded into HTML") {
       echo "PHP.net agrees with akari no ryu";
     }else{
       echo "PHP.net does NOT agree with akari no ryu";
    }
    ?>
    
    Originally posted by akari no ryu
    I see your google and raise you a wikipedia
    :confused: So once the PHP code is parsed what happens?


  • Registered Users Posts: 683 ✭✭✭Gosh


    Gosh wrote:
    PHP is server-side and HTML-embedded. This means it exists within the HTML page and the PHP code is executed on the server as it builds the page to send back to your browser.

    PHP is server-side and CAN be HTML-embedded. This means it can exist within the HTML page (as held on the server) and the PHP code is executed on the server as it builds the HTML page to send back to your browser (minus the PHP code).


  • Closed Accounts Posts: 169 ✭✭akari no ryu


    Gosh wrote:
    PHP is server-side and CAN be HTML-embedded. This means it can exist within the HTML page (as held on the server) and the PHP code is executed on the server as it builds the HTML page to send back to your browser (minus the PHP code).
    I'm still gonna call bull****.
    A html page contains html only.


  • Registered Users Posts: 683 ✭✭✭Gosh


    A html page contains html only.

    When it arrives back at the browser from the server.

    Actually, that's not 100% correct, either - it can contain javascript code.


  • Closed Accounts Posts: 169 ✭✭akari no ryu


    Gosh, unless you're willing to show me the <?php element declaration within the W3C specification for HTML, I suggest you drop this, because you're wrong.

    There is a [strike]javascript[/strike] <edit>script</edit> element within the HTML declaration. So, stating that a html page contains html only is 100% correct.
    The javascript element is declared such that it is a CDATA node <edit>and such that it's attributes are "type" and an optional "src"</edit>.


  • Advertisement
  • Registered Users Posts: 683 ✭✭✭Gosh


    Dropped.


Advertisement