向从文本输入中获取的电子邮件地址发送邀请

Sending invitations to email addresses taken from text inputs

我正在尝试找出一个 html/php 代码,使用户能够向他输入的电子邮件地址发送邀请。用户只需输入电子邮件地址,然后单击按钮 "invite",之后就会发送邀请。我有五个输入字段,我需要通过它们编写一个 php 代码来获取插入的电子邮件地址并发送

的邀请

这是我的 index.php 文件:

<form method="post" action="test.php">
 <input name="email" type="email" size="30" placeholder="email address of friend 1"><br> 
<input name="email2" type="email" size="30" placeholder="email address of friend 2"><br>
<input name="email3" type="email" size="30" placeholder="email address of friend 3"><br>
<input name="email4" type="email" size="30" placeholder="email address of friend 4"><br>
<input name="email5" type="email" size="30" placeholder="email address of friend 5"><br><br>

<input name="sendername" type="text" size="30" placeholder="Your Name"><br>

<input type="submit" name="invite" value="Invite"> </form>

我在 test.php 文件中编写了这段代码来处理第一封电子邮件的代码,但它给了我 return 消息“消息无法发送”,有什么建议吗?

<?php 

if(isset($_POST['invite'])) {

    // CHANGE THE TWO LINES BELOW

 $to = $_POST['email'];;

   $subject = "This is subject";
   $message = "This is simple text message.";
   $header = "From:abc@somedomain.com\r\n";
   $retval=@mail ($to,$subject,$message,$header);

   if( $retval == true )  
   {  echo "Message sent successfully...";
   }
   else
   { echo "Message could not be sent...";
   }


}


?>

您需要像这样将 array 创建为 input:.

<input name="email[]" type="email" size="30" placeholder="email address of friend 1"><br> 
<input name="email[]" type="email" size="30" placeholder="email address of friend 2"><br>
<input name="email[]" type="email" size="30" placeholder="email address of friend 3"><br>
<input name="email[]" type="email" size="30" placeholder="email address of friend 4"><br>
<input name="email[]" type="email" size="30" placeholder="email address of friend 5"><br>

并且在 PHP 中,您可以像这样使用 foreach 访问创建的数组:

<?php
if(isset($_POST['invite'])) {
    // CHANGE THE TWO LINES BELOW
    $subject="This is subject";
    $message="This is simple text message.";
    $email='abc@somedomain.com';
    $header='From: '.$email."\r\n".'Reply-To: '.$email."\r\n".'X-Mailer: PHP/'.phpversion();
    foreach($_POST['email'] as $value) {
        $retval=@mail($value,$subject,$message,$header);
    }

    if($retval==true) {  echo "Message sent successfully...";
    } else { echo "Message could not be sent...";
    }

}
?>

在沉迷于这个问题并尝试解决它之后,我想出了如何完成这项工作。简而言之,我不得不使用带有 if 语句的 foreach() 来遍历电子邮件数组。我的最终代码如下所示:

<?php
error_reporting(E_ALL); 
ini_set('display_errors', 1);
    if(isset($_POST['invite'])) {

        // CHANGE THE TWO LINES BELOW

        $to=$_POST['email'];
        $subject="This is subject";
        $message="This is simple text message.";
        $email='abc@somedomain.com';
        $header='From: '.$email."\r\n".'Reply-To: '.$email."\r\n".'X-Mailer: PHP/'.phpversion();

        if (is_array($to )){
        foreach($to as $value) {
         @mail($value,$subject,$message,$header);
    }

        }
           {  
                  echo "Message sent successfully...";
        } 


    }
?>