dongziche8030 2013-10-11 00:42
浏览 54
已采纳

PHP Foreach用于从数据库发送电子邮件?

I am a bit confused about how to use foreach. I read some internet things on it and I kind of understand how it works, but I don't fully understand it. I think I could use foreach to create a PHP mass emailer that sends blank carbon copy to email addresses and adresses the customer by name in the subject (Dear, Michael Here is your email). I've figured out how to retrieve the names and emails from my database into variables and I know how to email, but I don't know how to send multiple emails at once and to associate the name and email address.

<?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);
            }
        }

        ?>

Here is my email code:

        <?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('admins_email@yahoo.com', 'Site Admin');

      $mail->AddReplyTo('admins_email@yahoo.com', '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>
";   

    } 

    catch (phpmailerException $e) 
    {

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

    } 
    catch (Exception $e) 
    {

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

    }

?>

Basically, I need a way to combine these two scripts and link them together and that's what I'm unsure of how to do.

  • 写回答

1条回答 默认 最新

  • dongshan3759 2013-10-11 00:50
    关注

    Put the mailing code in a function, e.g. send_mail(), so that it can be called from different places. Then change your database query loop to:

    while ($row = mysql_fetch_assoc($result)) {
        send_mail($row['name'], $row['email'), "Text of the email");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料