简单的mailto集成查询

时间:2014-01-23 17:31:10

标签: php mysql post input mailto

好的,我有一个简单的邮件,我试图通过一个简单的查询发送给我的所有成员。 问题是我必须遗漏一些东西,因为表格只是重新加载而且不会发送任何东西。

email.php

<form method="post" action="massemail.php">

        Subject:</br>
        <input type="text" name="subject" /><br />

        Message:</br>
        <textarea name="details"></textarea><br />

        <input type="submit" value="Send"/>
    </form>

massemail.php

require 'core/init.php';
require_once "email.php"; 

$message = check_input($_POST['details']);
$subject = check_input($_POST['subject']);

$results = mysql_query('SELECT email from users');

while(list($email) = mysql_fetch_row($results))
{
  mail($email, '$subject', '$message');
}

已尝试

    <?php
require 'core/init.php';
require_once "email.php"; 

$message = "test";
$subject = "123";

if ($results = mysql_query('SELECT email from users')) {

    while(list($email) = mysql_fetch_row($results)) {

  mail($email, '$subject', '$message');
}
?>

2 个答案:

答案 0 :(得分:1)

首先,您需要从每行中提取电子邮件地址,而不是使用行本身。在您的情况下可以使用$row[0]轻松实现这一点,因为您的查询总是有一列,但您应该尝试使用mysqli而不是弃用的mysql。

其次,我建议只拨打mail() 一次,向所有人发送一条消息,而不是每人发送一封电子邮件!为什么要像这样一次又一次地发送同一封电子邮件?可以使用以逗号分隔的电子邮件列表,如PHP: mail - Manual上所示。

如果您想使用mysql,您的代码可能看起来像PHP Arrays和方便的implode函数:

require 'core/init.php'; // calls mysql_connect(...)
require_once "email.php";

$message = check_input($_POST['details']);
$subject = check_input($_POST['subject']);

if (mysql_ping()) { echo 'Ping worked'; } else { echo 'Ping failed'; } // DEBUG
$results = mysql_query('SELECT email from users');

$emails = array();
while($row = mysql_fetch_row($results))
{
    array_push($emails, $row[0]);
}
echo implode(", ", $emails); // DEBUG
if (!empty($emails)) {
    mail(implode(", ", $emails), $subject, $message);
}

在我看来(与你聊天),HTML Forms + PHP(GET和/或POST)的整个机制对你来说并不清楚。您可能应该阅读所有这些内容,希望以下代码可以帮助您:

<?php
// Put requires here
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>Demo form methods</h1>
<?php
if ((isset($_POST['subject']) && isset($_POST['body'])) || (isset($_GET['subject']) && isset($_GET['body']))) { 

    // Your code for sending an email could be here
    ?>

<h2>RESULTs</h2>
<ul>
    <li>POST
        <ul>
            <li>suject: <?php echo $_POST['subject']; ?></li>
            <li>suject: <?php echo $_POST['body']; ?></li>
        </ul>
    </li>
    <li>GET
        <ul>
            <li>suject: <?php echo $_GET['subject']; ?></li>
            <li>suject: <?php echo $_GET['body']; ?></li>
        </ul>
    </li>
</ul>

    <?php
    // The code above is just to show you how the variables in GET/POST work

 } else { 

    // Below are two formulaires, you can keep one only of course!
    ?>

<h2>FORMs</h2>
<h3>GET method</h3>
<form method="get" action="email.php">
    Subject: <input name="subject" type="text"><br>
    Body: <input name="body" type="text"><br>
    <input type="submit" value="Send with GET">
</form>
<h3>POST method</h3>
<form method="post" action="email.php">
    Subject: <input name="subject" type="text"><br>
    Body: <input name="body" type="text"><br>
    <input type="submit" value="Send with POST">

</form>
<?php } ?>
</body>
</html>

答案 1 :(得分:0)

它没有发送电子邮件,因为您没有正确解析查询结果。

//connect to MySQL using following line. This is the NEW and better way of doing it.
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

if ($result = $mysqli->query('SELECT email from users')) { 
        while($row = $result->fetch_object()){ 
            mail($row->email, $subject, $message);
        } 
    } 
    $result->close();