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 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态