从MySQL获取电子邮件并向其发送电子邮件

时间:2014-08-29 10:10:14

标签: php mysql email

我正在尝试从MySQL数据库表中获取“selectedoption”列下“全部”的电子邮件,然后向他们发送电子邮件。

这是我的PHP代码:

<?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>");

?>

从MySQL表中选择列字段并将电子邮件发送给与该列关联的收件人的正确方法是什么?

2 个答案:

答案 0 :(得分: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>");

?>

答案 1 :(得分:0)

试试这个:

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) ;
    }
}