2013-07-12 152 views
0

我有一個頁面,允許用戶通過輸入他們的電子郵件地址訂閱我的郵件列表。在他們點擊SUBMIT按鈕後,電子郵件地址發佈到一個txt文件,每次有人訂閱時都會自動填充該文件。這個頁面工作得很好。使用PHP表單從文件發送多個電子郵件

我的問題是發送一封電子郵件成功地在該txt文件中的電子郵件地址列表。我測試了代碼,但它卡在「發送消息...」,但沒有任何經歷。

我該如何解決這個問題?

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

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

$from_email="Name <[email protected]>"; 
// Your replay to email (whatever you want). 
$replyto="[email protected]"; 
// 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> 
<? } ?> 
+0

你爲什麼要關閉''和_then_顯示錶單? –

+0

'foreach($ emails_array [0]爲$ email)',爲什麼你使用'$ emails_array [0]'?當然應該是'$ emails_array'? –

+0

body和html標籤只是拼寫錯誤。 $ emails_array的[0]是爲了讀取文件中的所有電子郵件。 –

回答

-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); 
+0

這不起作用。它寫道:「無法打開文件!」 –

+0

@JosanIracheta這可能是因爲他在這裏使用'$ emails_file'作爲文件位置的字符串。在你的代碼中,你將它用作文件本身的內容。 –

+0

沒錯。如果$ emails_file包含包含電子郵件地址列表的文件的完整路徑,那麼這應該起作用(假設沒有權限限制)。 – mti2935

0

foreach循環需要這種形式:

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

所以你不應該提供它在$emails_array一個鍵,但整個陣列。
意味着此

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

應該成爲

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

閱讀上PHP's foreach loop