使用PHP将电子邮件发送到mysql db中的一个收件人

时间:2014-02-12 19:06:24

标签: php

我有一个表用户,用户有EmailAddress,我想只向一个用户,任何用户发送电子邮件,我有一个代码但由于用户没有收到电子邮件而无法正常工作, 有什么建议吗?

代码是:

<?php include "base.php"; ?>

<h2>send mail to only one user</h2>
<?php
ini_set('display_errors',1); 
error_reporting(E_ALL);
// display form if user has not clicked submit
if (!isset($_POST["submit"]))
  {
  ?>
  <form method="post" action="emailtosingur.php">
  To: <input type="text" name="to"><br>
  Subject: <input type="text" name="subject"><br>
  Message: <textarea rows="10" cols="40" name="message"></textarea><br>
  <input type="submit" name="submit" value="send">
  </form>
  <?php
  }
else
  // the user has submitted the form
  {
  // Check if the "from" input field is filled out
  if (isset($_POST["from"]))
    {
$email = $_POST['to'];
$query="SELECT * FROM users  WHERE EmailAddress ='$email'";
$result=mysql_query($query) or die('Error, query failed');
$check2 = mysql_num_rows($result);

while($row = mysql_fetch_array($result)) {



if ($check2)  {


// To send HTML mail, the Content-type header must be set

$to = "$email";
$subject = $_POST["subject"];
$message = $_POST["message"];
$body = "E-mail body";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: info@mydomain.com' . "\r\n" ;
$headers .= 'Reply-To: info@mydomain.com' . "\r\n";


// Mail it
mail($to,$subject, $message,$headers);

}
}
}
}
?>

2 个答案:

答案 0 :(得分:0)

编辑:

更改:

$to=$row['email'];

为:

$to=$row['EmailAddress'];

试一试。(基本示例)并使用mysqli_*代替mysql_*个函数。

下面有两种方法对我有用。

第一种方法使用WHERE EmailAddress='".$to_recipient."',其中电子邮件来自表单输入。电子邮件必须存在于数据库中。

旁注:使用您自己的更改数据库凭据xxx等。并且要小心保持$dbc变量在:

中保持不变
mysqli_real_escape_string($dbc,$_POST['to_recipient']); // important

方法1

<?php
ini_set('display_errors',1); 
error_reporting(E_ALL);

$hostname_gpd = "xxx";
$database_gpd = "xxx";
$password_gpd = "xxx";
$username_gpd = "xxx";

$dbc = mysqli_connect($hostname_gpd, $username_gpd, $password_gpd, $database_gpd) or  trigger_error(mysqli_error(),E_USER_ERROR);

// display form if user has not clicked submit
if (isset($_POST["submit"]))
  {

$to_recipient = mysqli_real_escape_string($dbc,$_POST['to_recipient']);
$email = mysqli_query($dbc, "SELECT `EmailAddress` FROM users WHERE EmailAddress='".$to_recipient."'");

if(mysqli_num_rows($email) != 1)
    {
die("Sorry, that Email is not in our database.");
    }

while($row = mysqli_fetch_assoc($email))
    {
$to=$row['EmailAddress'];
    }

$subject = $_POST["subject"];
$message = $_POST["message"];
// $body = "E-mail body";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: info@mydomain.com' . "\r\n" ;
$headers .= 'Reply-To: info@mydomain.com' . "\r\n";


// Mail it
if(mail($to,$subject, $message,$headers))
    {
echo "Success! Email was sent to $to";
    }
 }
?>

<form method="post" action="">
To: <input type="text" name="to_recipient"><br>
Subject: <input type="text" name="subject"><br>
Message: <textarea rows="10" cols="40" name="message"></textarea><br>
<input type="submit" name="submit" value="Send">
</form>

方法2

只需更改以下内容:(在上面的代码中)

发件人:

$to_recipient = mysqli_real_escape_string($dbc,$_POST['to_recipient']);

$to_recipient = "email_in_db@website.com"; // must exist in your DB

答案 1 :(得分:-1)

您尚未在此页面中打开数据库连接。你能告诉我们你怎么能得到mysql_query的结果?如果你没有得到结果,你没有$ check2的值,这意味着它甚至不会进入电子邮件循环。确保您还配置了电子邮件服务器