向具有从数组中提取的id的用户发送电子邮件

时间:2015-10-29 11:23:27

标签: php mysql arrays email csv

我有使用php在mysql中导入CSV数据的代码,导入工作正常但我需要额外的功能,它将通过从csv搜索user_id向用户发送电子邮件。我已将csv转换为数组$ csv_array,

user_id     order    part

1           test     phone
1           another  pad
2           all      tab

现在,代码将搜索用户并从用户数据库获取user_email,然后向用户发送电子邮件。

foreach ($csv_array as $row) {

                $this->load->library('email');
                $this->load->library('parser');

                $email = $this->admin_model->getEmailNotification();

                $user = $this->model->getUserShipment($row['user_id']);

                if(count($user) == 1)
                {
                    $data = array(  
                        'user_firstname' => $user->user_firstname,
                        'user_lastname' => $user->user_lastname,
                        'user_email' => $user->user_email,
                        );

                    $subject = 'Your Order has been shipped';
                    $message = $this->parser->parse('templates/update', $data, TRUE);

                    $config['protocol'] = $email->email_protocol;
                    $config['smtp_host'] = $email->email_host;
                    $config['smtp_user'] = $email->email_address;
                    $config['smtp_pass'] = $email->email_password;
                    $config['smtp_port'] = $email->email_port;
                    $config['newline']    = "\r\n";

                    $this->email->initialize($config);

                    $this->email->from($email->email_address);
                    $this->email->to($user->user_email);  
                    $this->email->subject($subject);
                    $this->email->message($message);        

                    $this->email->send();
                }


            }

现在我有两个问题,

  1. 这将为每一行发送电子邮件,但我只想发送一封包含两个订单的电子邮件,例如,在上表中,user_id 1将只收到一封包含订单和部件信息的电子邮件,

    < / LI>
  2. 此外,我将如何创建数组以传递user_id-1的模板 - &gt;订单和部件放在模板内的表格中?

  3. 谢谢,

1 个答案:

答案 0 :(得分:0)

我修改了你的代码。它只会在检测到新的user_id时发送电子邮件。

我将您的电子邮件发送过程解压缩到一个函数中,以便在完成读取csv后调用它(否则最后一个user_id将不会收到电子邮件)。

我创建的数组$ orderandparts的格式是你想要的,你只需要在函数内部使用它。

代码:

function sendmail($user_id,$orderandparts)
{
$this->load->library('email');
$this->load->library('parser');

$email = $this->admin_model->getEmailNotification();

$user = $this->model->getUserShipment($user_id);

if(count($user) == 1)
{
    $data = array(  
        'user_firstname' => $user->user_firstname,
        'user_lastname' => $user->user_lastname,
        'user_email' => $user->user_email,
        );

    $subject = 'Your Order has been shipped';
    $message = $this->parser->parse('templates/update', $data, TRUE);

    $config['protocol'] = $email->email_protocol;
    $config['smtp_host'] = $email->email_host;
    $config['smtp_user'] = $email->email_address;
    $config['smtp_pass'] = $email->email_password;
    $config['smtp_port'] = $email->email_port;
    $config['newline']    = "\r\n";

    $this->email->initialize($config);

    $this->email->from($email->email_address);
    $this->email->to($user->user_email);  
    $this->email->subject($subject);
    $this->email->message($message);        

    $this->email->send();
}
}
$user_id ="";
$orders = array();
$parts = array();
$orderandparts = array();
foreach ($csv_array as $row) {
            if($user_id == "")
                $user_id = $row["user_id"];

            if($user_id == $row["user_id"])
            {
                array_push($orders,$row["orders"]);
                array_push($parts,$row["parts"]);
            }
            else//Sends mail if all user data has been finished (new user_id detected)
            {
            for($i;$i<count($orders);$i++)
            {
                $orderandparts[$i]["order"] = $orders[$i];
                $orderandparts[$i]["part"] = $orders[$i];
            }
            sendmail($user_id,$orderandparts);
            $user_id = $row["user_id"];
            unset($orders);
            unset($parts);
            unset($orderandparts);
            $orders = array();
            $parts = array();
            $orderandparts = array();
            array_push($orders,$row["orders"]);
            array_push($parts,$row["parts"]);
            for($i;$i<count($orders);$i++)
            {
                $orderandparts[$i]["order"] = $orders[$i];
                $orderandparts[$i]["part"] = $orders[$i];
            }
            }
}
sendmail($user_id,$orderandparts);