db_query to send emails

Discussion in 'PHP' started by georgehowell, Apr 24, 2010.

  1. #1
    dear fine fellows,
    saving email content to a database seems common, but how about compiling emails from database content??
    Here's my attempt, which isn't working unfortunately (but i'm still at school, sorry). Any hints?

    <?php
    $database = "skyzone";
    $username = "root";
    $password = "";
    // connect to database
    $db = new PDO("mysql:host=localhost;dbname=$database",$username,$password);

    $result = db_query("SELECT email FROM users WHERE subscribe = '1'", $emails);
    while ($row = db_fetch_object($result)) {
    $to = $emails;
    $subject = "SkyZone Newsletter";
    $body = "Newsletter.pdf";
    if (mail($to, $subject, $body)) {
    echo("<p>Message successfully sent!</p>");
    } else {
    echo("<p>Message delivery failed...</p>");
    }
    }




    cheers, george
     
    georgehowell, Apr 24, 2010 IP
  2. Brad33

    Brad33 Peon

    Messages:
    69
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This line

    $to = $emails;
    PHP:
    May not be correct. You may need to use something like

    $to = $row['email'];
    PHP:
     
    Brad33, Apr 24, 2010 IP
  3. georgehowell

    georgehowell Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanx Brad
    I keep getting this: "Fatal error: Call to undefined function db_query() in C:\wamp\www\SkyZone\emailNewsletter.php on line 8"

    $result = db_query("SELECT email FROM users WHERE subscribe = '1'", ?); //line_08

    any clues?
     
    georgehowell, Apr 24, 2010 IP
  4. Brad33

    Brad33 Peon

    Messages:
    69
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    To be honest, I didn't understand some of your code because I usually do things differently. I rewrote your code, test it and let me know how it goes...

    <?php
    $database = "skyzone";
    $username = "root";
    $password = "";
    
    // connect to database
    mysql_connect( 'localhost', $username, $password );
    mysql_select_db( $database );
    
    $result = mysql_query("SELECT email FROM users WHERE subscribe = '1'");
    while ($row = mysql_fetch_row($result)) {
    	$to = $row[0];
    	$subject = "SkyZone Newsletter";
    	$body = "Newsletter.pdf";
    	if (mail($to, $subject, $body)) {
    		echo "<p>Message successfully sent!</p>";
    	} else {
    		echo "<p>Message delivery failed...</p>";
    	}
    }
    ?>
    PHP:
     
    Brad33, Apr 24, 2010 IP
  5. georgehowell

    georgehowell Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    GOT IT!!!
    cheers Brad, thanx a mil

    only a small prob with my smtp/outgoing mail settings now - but i'll try it out on my mac now, which'll by-pass this prob
    your a top bloke! thankx
    regards, george
     
    georgehowell, Apr 24, 2010 IP
  6. Brad33

    Brad33 Peon

    Messages:
    69
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yeah, you usually needs some additional config before you can send emails out if its just a home pc.

    Best of luck with your scripts, if you need anything more im available via PM. Don't mind helping a few people out :)
     
    Brad33, Apr 24, 2010 IP
  7. mukeshvariya34

    mukeshvariya34 Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You are using object method. So you can get database fields like $row->email (email is your database field)

    Please check this reference URL
    http://php.net/manual/en/function.mysql-fetch-object.php

    For mail, I use phpmailer class that you can download from www.phpclasses.org
     
    mukeshvariya34, Apr 25, 2010 IP