发送来自文本输入的电子邮件地址的邀请

时间:2022-10-23 19:19:12

I'm trying to figure out a html/php code that enables the user to send invitations for email addresses that he enters. The user has to only type in the email address then click the button "invite", after that the invitation will be sent. I have five input fields by which I need to write a php code to take the inserted email addresses and send invitations for

我正在试图找出一个html / php代码,使用户能够发送他输入的电子邮件地址的邀请。用户只需键入电子邮件地址,然后单击“邀请”按钮,之后将发送邀请。我有五个输入字段,我需要编写一个PHP代码来获取插入的电子邮件地址并发送邀请

here is my index.php file:

这是我的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>

I wrote this code in the test.php file to process the code for the first email, but it gives me the return message " message couldn't be sent" any advice ?

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

<?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...";
   }


}


?>

2 个解决方案

#1


You need to create array as input like this:.

您需要创建数组作为输入,如下所示:

<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>

And in PHP you can access created array with foreach like this:

在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...";
    }

}
?>

#2


After indulging more into this issue and attempting to resolve it, I figured out how to do the job. Briefly I had to use foreach() supplied with an if statement to loop over the email array. my final code looks like this :

在沉溺于这个问题并试图解决它之后,我想出了如何完成这项工作。简单地说,我必须使用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...";
        } 


    }
?>

#1


You need to create array as input like this:.

您需要创建数组作为输入,如下所示:

<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>

And in PHP you can access created array with foreach like this:

在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...";
    }

}
?>

#2


After indulging more into this issue and attempting to resolve it, I figured out how to do the job. Briefly I had to use foreach() supplied with an if statement to loop over the email array. my final code looks like this :

在沉溺于这个问题并试图解决它之后,我想出了如何完成这项工作。简单地说,我必须使用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...";
        } 


    }
?>