2015-07-20 82 views
-1

我有下面的代碼來選擇事件,用戶和子用戶,併發送電子郵件給用戶,如果有新的事件,子用戶創建。合併內容,如果相同的變量(電子郵件)

$result_m = mysqli_query($db, $query_mail) or die(mysqli_error()); 

while ($re4 = mysqli_fetch_array($result_m, MYSQLI_ASSOC)) 
    { 
    $ajdi = $re4["pid"]; 
    $name1 = $re4["name"]; 
    $surname1 = $re4["surname"]; 
    $email1 = $re4["email"]; 
    $mob = $re4["mob"]; 
    $titl1 = $re4["title"]; 
    $zip1 = $re4["zip"]; 
    $city1 = $re4["city"]; 
    $address1 = $re4["address"]; 
    $insurance1 = $re4["insurance"]; 
    $born1 = $re4["born"]; 
    $receiver = $re4["demail"]; 
    $content= "<p><img src='$logo'></p>"; 
    $content.= "<p><br/>Name: $name1 $surname1, Born: $born1, Insurance: $insurance1<br/>$address1, $city1 $zip1, Tel: $mob, Email: $email1</p>"; 
    $title= "Mail title"; 
    $content.= "<p>Footer message</p>"; 
    $headers = array(); 
    $headers[] = "MIME-Version: 1.0"; 
    $headers[] = "Content-type: text/html; charset=utf-8"; 
    $headers[] = "From: [email protected]"; 
    if ($receiver!= "") 
     { 
     mail($receiver, $title, $content, implode("\r\n", $headers)); 
     } 
    } 

這也包裹在另一個while循環得到的所有事件。 當用戶創建事件時,該用戶會收到電子郵件,但如果有更多事件,則會爲每個事件獲取電子郵件。 如何只發送一封電子郵件給所有創建的活動的用戶?

例如,當我做echo "$receiver<br>$content<br>";我得到:

[email protected] 
Some content 
[email protected] 
Some content2 
[email protected] 
Some content 
[email protected] 
Some content2 

但我需要這樣的:

[email protected] 
Some content 
Some content2 
[email protected] 
Some content 
Some content2 

如何實現這一目標?謝謝

UPDATE(根據歐文穆勒回答陣列)

Array 
(
    [[email protected]] => 
22.07.2015., 08:30 Uhr 
) 
Array 
(
    [[email protected]] => 
23.07.2015., 08:15 Uhr 
) 
Array 
(
    [[email protected]] => 
09.09.2015., 14:45 Uhr 
) 
Array 
(
    [[email protected]] => 
03.08.2015., 14:40 Uhr 
) 
Array 
(
    [[email protected]] => 
03.08.2015., 11:40 Uhr 
) 
Array 
(
    [[email protected]] => 
03.08.2015., 14:20 Uhr 
) 
Array 
(
    [[email protected]] => 
30.07.2015., 15:40 Uhr 
) 
Array 
(
    [[email protected]] => 
24.07.2015., 14:00 Uhr 
) 
Array 
(
    [[email protected]] => 
24.07.2015., 14:00 Uhr 
) 
Array 
(
    [[email protected]] => 
30.07.2015., 15:40 Uhr 
) 

回答

2

您可以在幾個方面接近這一點。

一個簡單的方法是: 不要發送電子郵件,但首先將它們全部存儲在數組中,並使用emailaddress作爲密鑰。

事情是這樣的:

... db stuff 
$allRecipients = array(); 
while ($re4 = mysqli_fetch_array($result_m, MYSQLI_ASSOC)) { 
    $ajdi = $re4["pid"]; 
    $name1 = $re4["name"]; 
    $surname1 = $re4["surname"]; 
    $email1 = $re4["email"]; 
    $mob = $re4["mob"]; 
    $titl1 = $re4["title"]; 
    $zip1 = $re4["zip"]; 
    $city1 = $re4["city"]; 
    $address1 = $re4["address"]; 
    $insurance1 = $re4["insurance"]; 
    $born1 = $re4["born"]; 
    $receiver = $re4["demail"]; 
    $content= "<p><img src='$logo'></p>"; 
    $content.= "<p><br/>Name: $name1 $surname1, Born: $born1, Insurance: $insurance1<br/>$address1, $city1 $zip1, Tel: $mob, Email: $email1</p>"; 
    if ($receiver!= ""){ 
      if (isset($allRecipients[$receiver]){ 
       // add the content. 
       $allRecipients[$receiver] .= "<hr>".$content; 
      } else { 
       $allRecipients[$receiver] = $content; 
      }   
    } 
} 

// now mail 
$title= "Mail title"; 
$headers = array(); 
$headers[] = "MIME-Version: 1.0"; 
$headers[] = "Content-type: text/html; charset=utf-8"; 
$headers[] = "From: [email protected]"; 
// If you are curious how that array looks, add this: 
// echo "<pre>"; 
// print_r($allRecipients); 
// echo "</pre>"; 

foreach ($allRecipients as $receiver => $content){ 
    $content.= "<p>Footer message</p>"; 
    mail($receiver, $title, $content, implode("\r\n", $headers));  
} 
+0

旁註:OP使用'MYSQL_ASSOC'應該是'MYSQLI_ASSOC'並添加「I」。你複製了相同的代碼。 –

+0

@ Fred-ii-謝謝,我確實是盲目複製它的,因爲我專注於array-email-as-key技巧。 ;-)現在修復。 –

+0

不客氣Erwin。 –

0

我覺得你的問題可以通過
加入GROUP BY在您選擇查詢來解決和合並的所有內容
SELECT email,"Merged Content" FROM your_table_name GROUP BY email
注: - 此合併的內容你有按照您的要求格式化

然後只是遍歷查詢結果併發送郵件:)