将表单信息发送到存储在数据库中的电子邮件

时间:2014-04-10 11:01:39

标签: php mysql database email

我一直在研究这个问题但是无法得到答案。 我有一个表单将信息发送到我的数据库,我也希望信息转到电子邮件地址,这些地址存储在数据库中我的表中的一列中。下面是代码,我只是在玩,看看我是否可以进行SELECT查询,但它不会工作,我也知道当我手动将地址输入我的" setTo"

时发送电子邮件是有效的。
require 'mail/class.simple_mail.php';
$mailer = new SimpleMail();

$comments = $_REQUEST['comments'];
$time = $_REQUEST['time'];
$date = $_REQUEST['date'];
$place = $_REQUEST['place'];
$email = "SELECT email FROM bridesmaids";

$message = "<strong>This is an email from the bride to you the bridesmaids, here is the information on the next dress fitting<br />".$date." ".$time." ".$place." ".$comments." </strong>";



$send   = $mailer->setTo('$email', 'Your Email')
                 ->setSubject('Dress Fitting')
                 ->setFrom('donaghy-e5@email.ulster.ac.uk', 'Domain.com')
                 ->addMailHeader('Reply-To', 'donaghy-e5@email.ulster.ac.uk', 'Domain.com')
                 ->addMailHeader('Cc', 'donaghy-e5@email.ulster.ac.uk', 'Bill Gates')
                 ->addGenericHeader('Content-Type', 'text/html; charset="utf-8"')
                 ->setMessage($message)
                 ->setWrap(100)
                 ->send();
echo ($send) ? 'Your Email has been sent to your bridesmaids' : 'Could not send email';

2 个答案:

答案 0 :(得分:1)

如果您还没有创建SQL连接,请先执行此操作:

1:从您的伴娘表中选择您的电子邮件 - 然后将结果返回到数组(PDO::FETCH_ASSOCfetch_array(MYSQLI_ASSOC))并将此数组绑定到变量,例如$mailingList

2:将您的邮件代码包裹在foreach循环(http://uk3.php.net/manual/en/control-structures.foreach.php)中,例如:

foreach($mailingList as $value) {

    // your mailer code here, replace $email with $value
    $send   = $mailer->setTo($value, 'Your Email')
    // $value will update for each iteration with your emails

}

3:确保任何不需要重复的代码或变量都在循环之外。

答案 1 :(得分:0)

您不会在数据库中查询实际的电子邮件地址。您只有包含SQL查询的字符串,但您实际上从未连接到数据库并使用查询获取数据。

$email = "SELECT email FROM bridesmaids";

这应该是这样的:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($result = $mysqli->query("SELECT email FROM bridesmaids")) {
    printf("Select returned %d rows.\n", $result->num_rows);
    $result->close();
}