-1

I'm trying to get the emails which have "all" under "selectedoption" column from MySQL database table and then sending emails to them.

Here is my PHP code:

<?php

  include("config.php");

  $con = mysql_connect($opt_host, $opt_user, $opt_pw);
  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }

  $db_selected = mysql_select_db("databasename_",$con);

  $result = mysqli_query($con,"SELECT * FROM optins");

  if(mysql_num_rows($selectedoption == "all")
  {
    while($selectedoption_result = mysql_fetch_array($selectedoption))
    { 
      $opt_name = "Name: Advisor\n"
      $opt_header = "From: $MYEMAILADDRESS\n"
      $opt_subject = "Test";
      $opt_email_to = "$email";
      $opt_message = "Hey $name\n"
          . "\n"
          . "Message.\n"
          . "\n"
          . "\n"
          . "Click Here To Unsubscribe.";

      @mail($opt_email_to, $opt_subject ,$opt_message ,$opt_header, $opt_name) ;
    }
  }
  echo("<p align='center'><font face='Arial' size='3' color='#FF0000'>Email Sent Out</font></p>");

?>

What is the right way to select a column field from MySQL table and send emails to the recipients associated with that column?

msrd0
  • 7,816
  • 9
  • 47
  • 82
Matt J
  • 99
  • 2
  • 12
  • you're missing a parenthesis in `mysql_num_rows` line. And the parameter is also wrong. Is your table even named `optins`? –  Aug 29 '14 at 10:31
  • Yes, I'm quite new to PHP please bare with me. – Matt J Aug 29 '14 at 10:55
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Aug 29 '14 at 10:58
  • My PDO knowledge is very limited, I think I'll have to use AWeber now. – Matt J Aug 29 '14 at 11:21
  • @MattJ The mysqli API is almost identical to the mysql API. Like PDO, it also supports prepared statements. Look, you've even snuck it in there in one instance! – Strawberry Aug 29 '14 at 12:16
  • Your query is failing. You are mixing `mysql_` with `mysqli_` functions; one of the main reason why your code is failing; they do not mix together. Choose "one" API, not a mix of both. – Funk Forty Niner Aug 29 '14 at 12:32

2 Answers2

1
<?php
include("config.php");

$mysqli = new mysqli($opt_host, $opt_user, $opt_pw, "databasename_");
if ($mysqli->connect_errno)
{
    die('Could not connect: ' . $mysqli->connect_error);
}

$result = $mysqli->query("SELECT * FROM options WHERE selectedoption == 'all'");

$opt_name = "Name: Advisor \n";
$youremail = "foo@bar.com";

while ($row = $result->fetch_assoc()) {
    $opt_header = "From: " . $opt_name . "<" . $youremail . ">" . "\r\n";
    $opt_subject = "Test";
    $opt_email_to = $row['email_column'];
    $opt_message = "Hey " . $row['name_column'] . "\n"
        . "\n"
        . Message . "\n"
        . "\n"
        . "\n"
        . "Click Here To Unsubscribe.";

    @mail($opt_email_to, $opt_subject, $opt_message, $opt_header) ;
}

echo("<p align='center'><font face='Arial' size='3' color='#FF0000'>Emails Sent Out</font></p>");

?>
Franz Holzinger
  • 913
  • 10
  • 20
0

Try this:

if(mysql_num_rows($result)>0){
    while($row = mysql_fetch_array($result)){ 
        $opt_name = "Name: ".$row['name_column']."\n";
        $opt_header = "From: $opt_name <$YOUREMAIL>";
        $opt_subject = "Test";
        $opt_email_to = $row['email_column'];
        $opt_message = "Hey $opt_name\n"
        . "\n"
        . "Message.\n"
        . "\n"
        . "\n"
        . "Click Here To Unsubscribe.";

        @mail($opt_email_to, $opt_subject ,$opt_message ,$opt_header) ;
    }
}
younis
  • 357
  • 1
  • 7
  • Unfortunately that didn't work. The opt_name is meant to be the name of the email who is sending the email. – Matt J Aug 29 '14 at 10:52
  • Sorry, I was just focusing on the mysql part. Try the edited version now. Though I am confused why the sender and the receiver's name is same in `Hey $opt_name\n` – younis Aug 29 '14 at 10:58
  • That didn't work as well unfortunately. I think I should just use AWeber fix everything. Thank you for your time and help. – Matt J Aug 29 '14 at 11:20