2013-10-11 55 views
1

我对如何使用foreach有点困惑。我阅读了一些网络上的东西,并且我理解它是如何工作的,但我并不完全理解它。我想我可以使用foreach来创建一个PHP大量电子邮件发送器,它将空白抄送发送到电子邮件地址,并通过主题中的姓名给客户提供地址(亲爱的,迈克尔,这里是您的电子邮件)。我已经想出了如何从我的数据库中检索名称和电子邮件到变量中,并知道如何发送电子邮件,但我不知道如何一次发送多封电子邮件并将名称和电子邮件地址关联起来。PHP Foreach用于从数据库发送电子邮件?

<?php 
     //Variables for connecting to your database. 
     //These variable values come from your hosting account. 
     $hostname = "MichaelBerna.db.10339998.hostedresource.com"; 
     $username = "MichaelBerna"; 
     $dbname = "MichaelBerna"; 

    //These variable values need to be changed by you before deploying 
     $password = "********"; 
     $usertable = "subscribers"; 
     $yourfield = "name"; 
     $yourfield1 = "email"; 

     //Connecting to your database 
     $link = mysql_connect($hostname, $username, $password) OR DIE ("Unable to connect to database! Please try again later."); 
     mysql_select_db($dbname); 

     //Fetching from your database table. 
     $query = "SELECT * FROM $usertable"; 
     $result = mysql_query($query); 

     if ($result) 
     { 
      while($row = mysql_fetch_array($result)) 
      {      
       $name = $row["$yourfield"]; 
       $email = $row["$yourfield1"]; 
       echo "Name: $name<br>"; 
       echo "Email: $email<br>"; 
       //mysqli_free_result($result); 
       //mysqli_close($link); 
      } 
     } 

     ?> 

这里是我的电子邮件代码:

 <?php 

    require_once '../PHPMailer_5.2.2/class.phpmailer.php'; 

    $name = $_POST['name'] ; 

    $email = $_POST['email'] ; 

    //$file = $_POST['file'] ; // I'm going to later add a file later to be attached in email from database 


    $body = "Hey $name thank you for continuing to be a valued customer! This month's story is included in this email asa an attachment."; 

    $mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch 


    try 

    { 

     $mail->AddAddress($email, $name); 

     $mail->SetFrom('[email protected]', 'Site Admin'); 

     $mail->AddReplyTo('[email protected]', 'Site Admin'); 

     $mail->Subject = "Dear $name Your monthly subscription has arrived!"; 

     $mail->Body = $body; 

     if ($_FILES['file']['size']) 

     { 

     $mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);// attachment 

     } 

     $mail->Send(); 

     echo "Email Sent Successfully</p>\n"; 

    } 

    catch (phpmailerException $e) 
    { 

     echo $e->errorMessage(); //Pretty error messages from PHPMailer 

    } 
    catch (Exception $e) 
    { 

     echo $e->getMessage(); //Boring error messages from anything else! 

    } 

?> 

基本上,我需要一种方法来这两个脚本结合起来,将它们链接在一起,这就是我不确定该怎么办。

+0

您不明白'foreach'吗?这只是循环,一个基本的计算机编程概念。 – Barmar

+0

只需在您已有的'while'循环中使用您的电子邮件代码 – 2013-10-11 00:49:10

+0

如果您要发送个性化电子邮件,则一次不会发送多封电子邮件:您一次只发送一封电子邮件。大多数主机会限制您每小时或每天发送的号码,或者两者都有。我建议你看看持续连接以提高吞吐量,但如果你正在为'foreach'挣扎,我怀疑你已经做好了准备。 – 2013-10-11 00:49:54

回答

4

将邮寄代码放入一个函数中,例如send_mail(),以便它可以从不同的地方调用。然后将您的数据库查询循环更改为:

while ($row = mysql_fetch_assoc($result)) { 
    send_mail($row['name'], $row['email'), "Text of the email"); 
} 
+0

非常感谢!这是有道理的,并简化它。 – mberna