多封电子邮件输入无效

时间:2014-03-21 14:59:02

标签: php email implode

我有一个推荐页面,人们可以在其中输入电子邮件地址以发送推介。

<form action="referemails.php" method="post">
Insert your friend's email below to refer them!<br><br>
Email: <input type="email" id="referemail" name="referemail[]" size="50" autofocus="autofocus" placeholder="example@email.com"><br>
Email: <input type="email" id="referemail" name="referemail[]" size="50" autofocus="autofocus" placeholder="example@email.com"><br>
Email: <input type="email" id="referemail" name="referemail[]" size="50" autofocus="autofocus" placeholder="example@email.com"><br>
  <input type="submit" value="Submit"></form>

发送电子邮件的代码如下:

<?php

if (isset($_POST['referemail'])) {
    $username= $_SESSION['username'];
    $options = array("options" => array(FILTER_FORCE_ARRAY));
    $email = filter_input(INPUT_POST, 'referemail', FILTER_SANITIZE_EMAIL, $options);
    $email = filter_var($email, FILTER_VALIDATE_EMAIL);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Not a valid email
        $error_msg = '<p class="error">The email address you entered is not valid</p>'; };

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: example@email.com' . "\r\n";
                        $to  = implode(',', $email);
                        $subject = 'Email!';
                        $message ='...';}

                           if (mail($to, $subject, $message, $headers)) { 
                               header('Location: ../includes/referfriends.php');
}
?>

我尝试了implode(','命令的几个变体。但要么我不是把它放在正确的位置,或者我的理论是,我在$ email上的过滤器不会发送多封电子邮件。如果只输入一个电子邮件地址,它会发送电子邮件,但是多个电子邮件的行为就像发送的那样,虽然没有,但我的日志中没有错误。 我错过了什么或错误地阻止了多个地址?提前感谢任何知识或有用的链接。我已经尝试搜索webz并尝试了多种选择但总是达到这一点。

2 个答案:

答案 0 :(得分:1)

问题是您的变量$email,而扩展名$to是一个字符串。因此,内爆不会像数组那样工作。

您已在输入中使用multiple标记,但这是用于文件,而不是文本输入。您需要重新考虑如何获取电子邮件地址。

如果您想获得多个电子邮件地址,则需要多个输入:

<input type="email" name="email[]">
<input type="email" name="email[]">

注意方括号,它允许多个输入具有相同的名称并作为数组提交。

然后,您可以使用以下内容获取发送页面上的数据:

$options = array("options" => array(FILTER_FORCE_ARRAY));
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL, $options);
然后

$email将保存一组提交的电子邮件地址。

答案 1 :(得分:0)

所以我改变了我的PHP代码,完全删除了过滤器。代替

$options = array("options" => array(FILTER_FORCE_ARRAY));
$email = filter_input(INPUT_POST, 'referemail', FILTER_SANITIZE_EMAIL, $options);
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Not a valid email
    $error_msg = '<p class="error">The email address you entered is not valid</p>'; };

我只是说:

$email = implode(",",$_REQUEST['referemail']);

它有效。

相关问题