2015-10-29 52 views
1

我正在閱讀來自文本文件的電子郵件,並且一次只能發送一封電子郵件。所有東西(發件人,主題,正文)都是恆定的,除了接收者。我有我的PHP郵件的工作,我使用額外的代碼是:PHP郵件羣發電子郵件一個接一個

<?php 
if ($_FILES) { 
    if ($_FILES['file']['name'] != "") { 
     if (isset($_FILES) && $_FILES['file']['type'] != 'text/plain') { 
      echo "<span>File could not be accepted ! Please upload any '*.txt' file. </span>"; 
      exit(); 
     } 

     echo "<center><span id='Content'>Contents of ".$_FILES['file']['name']." File</span></center>"; 
     $fileName = $_FILES['file']['tmp_name']; 
     $file = fopen($fileName,"r") or exit("Unable to open file!"); 
     $string = file_get_contents("$fileName"); // Load text file contents 
     $pattern = '/[a-z0-9_\-\+][email protected][a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i'; 
     preg_match_all($pattern, $string, $matches); 
     $output = var_export($matches[0]); 

     foreach ($output as $item) { 
      echo "$item\n"; 
      $output = $item; 
     } 

     print_r($output); 
    } else { 
     if (isset($_FILES) && $_FILES['file']['type'] == '') { 
      echo "<span>Please Choose a file by click on 'Browse' or 'Choose File' button.</span>"; 
     } 
    } 
} 
?> 
+0

你的問題是什麼? – madforstrength

+0

我有一個充滿電子郵件的文本文件,我必須閱讀電子郵件並將其發送到一個接一個。我從txt文件中讀取的電子郵件是收件人地址。如果您有任何想法,請不要將郵件逐一發送至所有地址,請分享。謝謝 – KaramChand

+0

您可以使用循環或將所有電子郵件地址保存爲逗號分隔的字符串並立即發送。你可以發佈你的郵件程序腳本,所以我可以看看它,也許提供一個解決方案。 –

回答

0

您可以設置多個收件人到一個單一的消息和SingleTo設置爲true。

$recipient_addresses = array(); 
.... 
$message = new \PHPMailer; 
$message->SingleTo = true; 
.... 
foreach($recipient_addresses as $email) $message->addAddress($email);  
.... 

請注意SingleTo僅在「mail」和「sendmail」傳輸中受支持,而不在「SMTP」中支持。

如果您使用SMTP傳輸,只需逐個循環發送每個地址。只需撥打一個時間MESSAGE-$> addAddress傳遞只有一個地址的PHPMailer類的每個實例,而是創建多個實例,一個接一個:

1. $message = new \PHPMailer; 
2. $message->addAddress($email); 
3. fill other data, but don't set the SingleTo property value, keep it default (false). 
4. $message->send(); 
5. unset($message); 

也請注意一個事實,即SingleTo是計劃在PHPMailer 6.0的發行版中棄用,並在7.0中刪除。

相關問題