Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP表单从文件发送多封电子邮件_Php_Arrays_Forms_Email_Contact Form - Fatal编程技术网

使用PHP表单从文件发送多封电子邮件

使用PHP表单从文件发送多封电子邮件,php,arrays,forms,email,contact-form,Php,Arrays,Forms,Email,Contact Form,我有一个页面,允许用户通过输入他们的电子邮件地址来订阅我的邮件列表。在他们单击SUBMIT按钮后,电子邮件地址将发布到一个txt文件,该文件在每次有人订阅时自动填充。这一页很好用 我的问题是成功地将电子邮件发送到该txt文件中的电子邮件地址列表。我测试了代码,但它被卡在了“发送消息…”上,但什么都没有通过 我该如何解决这个问题 <? error_reporting(1); //#########################################################

我有一个页面,允许用户通过输入他们的电子邮件地址来订阅我的邮件列表。在他们单击SUBMIT按钮后,电子邮件地址将发布到一个txt文件,该文件在每次有人订阅时自动填充。这一页很好用

我的问题是成功地将电子邮件发送到该txt文件中的电子邮件地址列表。我测试了代码,但它被卡在了“发送消息…”上,但什么都没有通过

我该如何解决这个问题

<?
error_reporting(1);
//#############################################################
//#################   CONFIGURATION  ##########################
//#############################################################

// choose a password
$my_password="1234";
// the email from which emails are sent

$from_email="Name <name@gmail.com>";
// Your replay to email (whatever you want).
$replyto="name@gmail.com";
// A message to be attached to the bottom of the message
//   We recommend to add a link to subscription page
$message_at_bottom="
------------------------------------------------------------
P.D.: To remove from this list go to... 
http://www.domain.com/Dating/dwh-emaillist.php/
";
$emails_file="emaillist-XXXXXXXXX.txt"; 

//#############################################################
//###############   END CONFIGURATION  ########################
//#############################################################

// IF INFO IS NOT POSTED, PRINT THE FORM AND DIE
if (!$_POST["message"]){ 
    print_form();
    die();
}

// IF INFO IS POSTED YOU WILL BE HERE
// Check whether the password is correct
//  (only webmaster is supposed to know the password, which has been specified above)
if ($_POST["p"]!=$my_password){die("Incorrect password");}

// Get the subject of message 
$subject =$_POST["subject"];
// Get the body of message 
$message=$_POST["message"];
// Add to body of message the bottom
$message.=$message_at_bottom; 
// Read the file with emails to variable $emails_file        
$emails_file=file_get_contents($emails_file);
// Extract list of emails to array $emails_array 
preg_match_all("/<.{0,100}?>/", $emails_file, $emails_array); 

// Start output
print "<b>Sending messages...</b>";

// Send email to each email
foreach ($emails_array[0] as $email){
    // remove "<" and ">" from each email
    $email=substr($email,1,strlen($email)-2);
    // Next line is the one sending the email: the key command of this script 
    mail ($email, $subject, $message,"From: $from_email\nReply-To: $replyto\nContent-    Type: text/plain");
    // Each time an email is send, output it
    print "<br>$email\n";
    // After sending each email, send previous line (the email) to the browser 
    flush();
}

?>



<?php
// THIS FUNCTION WILL SHOW THE FORM
// MODIFY IT AS REQUIRED
function print_form(){
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"  http-equiv="content-type">
  <title>My email list</title>
</head>
<body style="background-color: rgb(255, 255, 255);">
<center>
<h2>Form to send email to the mailing list</h2>
<table style="font-family: times new roman;" border="0" cellpadding="0"  cellspacing="0">
  <tbody>
    <tr>
      <td style="vertical-align: top;">
<form method=POST action="dwh-emaillist-sendmessage.php">
Subject
<br><input type=text name=subject size=40>
<br>Message
<br><textarea name=message cols=50 rows=8></textarea>
<br>Password <input type=password name=p size=10>
<br><input type=submit value=Send>
</form>
      </td>
    </tr>
  </tbody>
</table>
</center>
</body>
</html>
<? } ?>

我的电子邮件列表
向邮件列表发送电子邮件的表单
主题


信息

密码

不确定您是否正确读取了文件-我从未见过这样做

这种方法应该有效:

 $i=1;
 $file = fopen($emails_file, "r") or exit("Unable to open file!");
 while(!feof($file)) {
   $email=fgets($file);
   $email=trim($email);

   // remove "<" and ">" from each email
   $email=substr($email,1,strlen($email)-2);
   // Next line is the one sending the email: the key command of this script 
   mail ($email, $subject, $message,"From: $from_email\nReply-To: $replyto\nContent-    Type: text/plain");
   // Each time an email is send, output it




   print $i . ") sending message to: " . $email . "\n";
   if( ($i % 10)==0 ) { sleep(1); }
   $i++;
 }
 fclose($file);
$i=1;
$file=fopen($emails_file,“r”)或exit(“无法打开文件!”);
而(!feof($file)){
$email=fgets($file);
$email=trim($email);
//从每封电子邮件中删除“”
$email=substr($email,1,strlen($email)-2);
//下一行是发送电子邮件的行:此脚本的关键命令
邮件($email,$subject,$message,“From:$From_email\n回复至:$replyto\n内容-类型:text/plain”);
//每次发送电子邮件时,将其输出
打印$i。“)将邮件发送到:“.$email”。\n”;
如果($i%10)=0{sleep(1);}
$i++;
}
fclose($文件);

foreach循环需要以下表单:

foreach ($array as $value) { ... }
因此,您不应在
$emails\u数组中为其提供单个键,而应提供整个数组。
这是什么意思

foreach ($emails_array[0] as $email){ ... }
应该成为

foreach ($emails_array as $email){ ... }

请继续阅读。

为什么要关闭
然后显示表单?
foreach($emails\u array[0]作为$email)
以及为什么要使用
$emails\u array[0]
?当然应该是
$emails\u array
?body和html标记只是打字错误。$emails\u数组的[0]可以读取文件中的所有电子邮件。@JosanIracheta这可能是因为他在这里使用
$emails\u file
作为指向文件位置的字符串。在代码中,您将其用作文件本身的内容。没错。如果$emails\u文件包含包含电子邮件地址列表的文件的完整路径,那么这应该可以工作(前提是没有权限限制)。我尝试添加完整路径,但它仍然无法打开它。不确定这是怎么回事。它试图打开的文件是否存在权限问题?该文件需要为所有用户授予读取权限。