【问题标题】:Send Multiple e-mails From File Using PHP Form使用 PHP 表单从文件发送多封电子邮件
【发布时间】:2013-07-11 12:07:57
【问题描述】:

我有一个页面,允许用户通过输入他们的电子邮件地址来订阅我的邮件列表。在他们单击 SUBMIT 按钮后,电子邮件地址会 POST 到一个 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>
<? } ?>

【问题讨论】:

  • 你为什么要关闭&lt;/body&gt;&lt;/html&gt;然后然后显示表单?
  • foreach ($emails_array[0] as $email) 你为什么使用$emails_array[0]?当然应该是$emails_array
  • body 和 html 标签只是拼写错误。 $emails_array 的 [0] 是为了读取文件中的所有电子邮件。

标签: php arrays forms email contact-form


【解决方案1】:

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

这种方式应该可行:

 $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);

【讨论】:

  • @JosanIracheta 这可能是因为他在这里使用$emails_file 作为文件位置的字符串。在您的代码中,您将其用作文件本身的内容。
  • 没错。如果 $emails_file 包含包含电子邮件地址列表的文件的完整路径,那么这应该可以工作(只要没有权限限制)。
  • 我尝试添加完整路径,但仍然无法打开它。不知道这是怎么回事。
  • 可能是它试图打开的文件的权限问题吗?该文件需要为所有人授予读取权限。
【解决方案2】:

foreach 循环需要这种形式:

foreach ($array as $value) { ... }

因此,您不应该在 $emails_array 中为其提供单个键,而是提供整个数组。
意思是这个

foreach ($emails_array[0] as $email){ ... }

应该变成

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

阅读PHP's foreach loop

【讨论】:

    猜你喜欢
    • 2017-09-26
    • 2013-02-17
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    相关资源
    最近更新 更多