如何使用用户名向用户发送电子邮件

时间:2014-08-11 13:07:05

标签: php email content-management-system

你好我正在创建一个项目管理系统,它存储新的工作,为团队增加员工,我有一个存储用户记录的数据库, 在我的表格(gui)我有一个用户列表,我可以从中选择添加到我想发送的项目,现在我正在尝试根据我选择并分配给我的工作的用户发送电子邮件发送作业,作为通知。

我怎么能这样做呢。我知道有可能因为用户的电子邮件地址存储在一起作为用户名,密码等,但我不知道如何去做它

3 个答案:

答案 0 :(得分:2)

来自this发布的php中的SQL参数和mail()的php手册页:

$stmt = $dbConnection->prepare('SELECT * FROM users WHERE username = ?');
$stmt->bind_param('s', $username);

$stmt->execute();

$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {

    $to      = $row['email']; 
    $subject = 'the subject';
    $message = 'A wizard is never late!';
    $headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .

   mail($to, $subject, $message, $headers);
}

您希望参数化SQL查询,以便没有SQL injections

答案 1 :(得分:1)

示例:

<form>
    <input type="text" name="user">
    <input type="submit" value = "Submit">
</form>

处理程序:

$user = $_POST['user'];
$conn = //DB CONNECTION STATEMENT  
$stmt = $conn->prepare("select * as EMAIL from TABLE_NAME where USERNAME_COLUMN = :user");
$stmt->bindparam(':user', $user);
$stmt->execute();

//do stuff here with data collected//

答案 2 :(得分:-1)

它应该是这样的:

Select email address of users by their username:

$check = $db->prepare("SELECT email FROM table WHERE username = :username"); 
$check->bindValue(':username', 'username');
$check->execute();
$check = $check->fetchAll(PDO::FETCH_ASSOC);

if (count($check) > 0){
  while ($row = $check->fetch_assoc()) {

  $to = $row['email']; //email 
  $subject = 'the subject';
  $message = 'hello';
  $headers = 'From: webmaster@example.com' . "\r\n" .
  'Reply-To: webmaster@example.com' . "\r\n" .

  mail($to, $subject, $message, $headers); 
  }

}