Yii2 - 批量发送电子邮件

时间:2018-05-09 15:04:08

标签: email yii2 yii2-advanced-app yii2-active-records

我想使用yii2 mailer发送电子邮件。我已经配置了邮件程序,并且已经尝试在一封电子邮件中发送一条记录。现在我想在一封电子邮件中发送多条记录。

以下是我的动作控制器

 $sql = "SELECT COUNT(DISTINCT od.`meter_serial`) AS 'OGP Created', 
         COUNT(DISTINCT mp.`meter_id`) AS 'Installed & Un-Verified Meters', 
         COUNT(DISTINCT ins.`meter_msn`) AS 'Installed & Verified', 
         sd.`sub_div_code` AS 'SD Code',sd.`name` AS 'SD-Name'
         FROM `ogp_detail` od
         INNER JOIN `survey_hesco_subdivision` sd ON od.`sub_div` = 
         sd.`sub_div_code`
         LEFT JOIN `meter_ping` mp ON od.`meter_id` = mp.`meter_id`
         LEFT JOIN `installations` ins ON od.`meter_serial` = ins.`meter_msn`
         WHERE od.`meter_type` = '3-Phase'
         GROUP BY sd.`name`";
    $result = Yii::$app->db->createCommand($sql)->queryAll();
    print_r($result);
    exit();

输出

Array ( [0] => Array ( [OGP Created] => 7 [Installed & Un-Verified Meters] => 2 [Installed & Verified] => 4 [SD Code] => 37153 [SD-Name] => ALLAMA IQBAL ) [1] => Array ( [OGP Created] => 68 [Installed & Un-Verified Meters] => 3 [Installed & Verified] => 0 [SD Code] => 37281 [SD-Name] => BADIN ) [2] => Array ( [OGP Created] => 13 [Installed & Un-Verified Meters] => 6 [Installed & Verified] => 0 [SD Code] => 37336 [SD-Name] => BHIT SHAH ) [3] => Array ( [OGP Created] => 6 [Installed & Un-Verified Meters] => 2 [Installed & Verified] => 0 [SD Code] => 37254 [SD-Name] => BULRISHAH KARIM ) [4] => Array ( [OGP Created] => 26 [Installed & Un-Verified Meters] => 18 [Installed & Verified] => 0 [SD Code] => 37144 [SD-Name] => CHAMBER ) [5] => Array ( [OGP Created] => 13 [Installed & Un-Verified Meters] => 3 [Installed & Verified] => 6 [SD Code] => 37182 [SD-Name] => CITIZEN COLONY ) [6] => Array ( [OGP Created] => 117 [Installed & Un-Verified Meters] => 0 [Installed & Verified] => 0 [SD Code] => 37314 [SD-Name] => DAUR ) [7] => Array ( [OGP Created] => 78 [Installed & Un-Verified Meters] => 26 [Installed & Verified] => 2 [SD Code] => 37421 [SD-Name] => DIGRI ) [8] => Array ( [OGP Created] => 15 [Installed & Un-Verified Meters] => 1 [Installed & Verified] => 7 [SD Code] => 37112 [SD-Name] => GARI KHATA ) [9] => Array ( [OGP Created] => 24 [Installed & Un-Verified Meters] => 10 [Installed & Verified] => 0 [SD Code] => 37283 [SD-Name] => GOLARCHI ) [10] => Array ( [OGP Created] => 7 [Installed & Un-Verified Meters] => 4 [Installed & Verified] => 0 [SD Code] => 37335 [SD-Name] => HALA ) [11] => Array ( [OGP Created] => 4 [Installed & Un-Verified Meters] => 0 [Installed & Verified] => 3 [SD Code] => 37151 [SD-Name] => HALI ROAD ) [12] => Array ( [OGP Created] => 21 [Installed & Un-Verified Meters] => 8 [Installed & Verified] => 1 [SD Code] => 37183 [SD-Name] => HIRABAD ) [13] => Array ( [OGP Created] => 85 [Installed & Un-Verified Meters] => 9 [Installed & Verified] => 0 [SD Code] => 37413 [SD-Name] => HIRABAD MPK ) [14] => Array ( [OGP Created] => 112 [Installed & Un-Verified Meters] => 28 [Installed & Verified] => 49 [SD Code] => 37111 [SD-Name] => HYD SADDAR ) [15] => Array ( [OGP Created] => 10 [Installed & Un-Verified Meters] => 10 [Installed & Verified] => 0 [SD Code] => 37243 [SD-Name] => ILYASABAD ) [16] => Array ( [OGP Created] => 70 [Installed & Un-Verified Meters] => 37 [Installed & Verified] => 6 [SD Code] => 37222 [SD-Name] => JAMSHORO ) [17] => Array ( [OGP Created] => 123 [Installed & Un-Verified Meters] => 28 [Installed & Verified] => 0 [SD Code] => 37342 [SD-Name] => JHOLE ) [18] => Array ( [OGP Created] => 16 [Installed & Un-Verified Meters] => 9 [Installed & Verified] => 0 [SD Code] => 37422 [SD-Name] => JHUDO ) [19] => Array ( [OGP Created] => 20 [Installed & Un-Verified Meters] => 20 [Installed & Verified] => 0 [SD Code] => 37345 [SD-Name] => KHIPRO ) [20] => Array ( [OGP Created] => 38 [Installed & Un-Verified Meters] => 6 [Installed & Verified] => 10 [SD Code] => 37221 [SD-Name] => KOTRI ) [21] => Array ( [OGP Created] => 18 [Installed & Un-Verified Meters] => 13 [Installed & Verified] => 0 [SD Code] => 37434 [SD-Name] => KUNRI ) [22] => Array ( [OGP Created] => 9 [Installed & Un-Verified Meters] => 1 [Installed & Verified] => 6 [SD Code] => 37115 [SD-Name] => LIAQAT COLONY ) [23] => Array ( [OGP Created] => 45 [Installed & Un-Verified Meters] => 19 [Installed & Verified] => 0 [SD Code] => 37273 [SD-Name] => MAKLI ) [24] => Array ( [OGP Created] => 49 [Installed & Un-Verified Meters] => 4 [Installed & Verified] => 0 [SD Code] => 37334 [SD-Name] => MATIARI ) [25] => Array ( [OGP Created] => 27 [Installed & Un-Verified Meters] => 7 [Installed & Verified] => 0 [SD Code] => 37253 [SD-Name] => MATLI ) [26] => Array ( [OGP Created] => 5 [Installed & Un-Verified Meters] => 1 [Installed & Verified] => 0 [SD Code] => 37244 [SD-Name] => MEMON HOSPITAL ) [27] => Array ( [OGP Created] => 20 [Installed & Un-Verified Meters] => 11 [Installed & Verified] => 0 [SD Code] => 37411 [SD-Name] => MIPURKHAS CITY ) [28] => Array ( [OGP Created] => 26 [Installed & Un-Verified Meters] => 12 [Installed & Verified] => 6 [SD Code] => 37155 [SD-Name] => MIRAN M.SHAH ) [29] => Array ( [OGP Created] => 64 [Installed & Un-Verified Meters] => 3 [Installed & Verified] => 0 [SD Code] => 37414 [SD-Name] => MIRWAH ) [30] => Array ( [OGP Created] => 84 [Installed & Un-Verified Meters] => 3 [Installed & Verified] => 0 [SD Code] => 37424 [SD-Name] => MITHI ) [31] => Array ( [OGP Created] => 11 [Installed & Un-Verified Meters] => 6 [Installed & Verified] => 0 [SD Code] => 37423 [SD-Name] => NAUKOT ) [32] => Array ( [OGP Created] => 59 [Installed & Un-Verified Meters] => 3 [Installed & Verified] => 6 [SD Code] => 37311 [SD-Name] => NAWAB SHAH-I ) [33] => Array ( [OGP Created] => 26 [Installed & Un-Verified Meters] => 0 [Installed & Verified] => 0 [SD Code] => 37312 [SD-Name] => NAWAB SHAH-II ) [34] => Array ( [OGP Created] => 43 [Installed & Un-Verified Meters] => 13 [Installed & Verified] => 0 [SD Code] => 37225 [SD-Name] => NOORIABAD ) [35] => Array ( [OGP Created] => 14 [Installed & Un-Verified Meters] => 9 [Installed & Verified] => 0 [SD Code] => 37333 [SD-Name] => ODERO LAL ) [36] => Array ( [OGP Created] => 3 [Installed & Un-Verified Meters] => 0 [Installed & Verified] => 0 [SD Code] => 37241 [SD-Name] => PARETABAD ) [37] => Array ( [OGP Created] => 9 [Installed & Un-Verified Meters] => 5 [Installed & Verified] => 0 [SD Code] => 37432 [SD-Name] => PITHORO ) [38] => Array ( [OGP Created] => 27 [Installed & Un-Verified Meters] => 3 [Installed & Verified] => 15 [SD Code] => 37181 [SD-Name] => QASIMABAD ) [39] => Array ( [OGP Created] => 66 [Installed & Un-Verified Meters] => 0 [Installed & Verified] => 0 [SD Code] => 37318 [SD-Name] => QAZI AHMED ) [40] => Array ( [OGP Created] => 5 [Installed & Un-Verified Meters] => 0 [Installed & Verified] => 0 [SD Code] => 37152 [SD-Name] => RIZVI HOSPITAL ) [41] => Array ( [OGP Created] => 189 [Installed & Un-Verified Meters] => 22 [Installed & Verified] => 0 [SD Code] => 37316 [SD-Name] => SAEEDABAD ) [42] => Array ( [OGP Created] => 130 [Installed & Un-Verified Meters] => 0 [Installed & Verified] => 0 [SD Code] => 37315 [SD-Name] => SAKRAND ) [43] => Array ( [OGP Created] => 21 [Installed & Un-Verified Meters] => 7 [Installed & Verified] => 0 [SD Code] => 37433 [SD-Name] => SAMARO ) [44] => Array ( [OGP Created] => 172 [Installed & Un-Verified Meters] => 117 [Installed & Verified] => 0 [SD Code] => 37341 [SD-Name] => SANGHAR ) [45] => Array ( [OGP Created] => 21 [Installed & Un-Verified Meters] => 2 [Installed & Verified] => 13 [SD Code] => 37113 [SD-Name] => SARFARAZ COLONY ) [46] => Array ( [OGP Created] => 55 [Installed & Un-Verified Meters] => 13 [Installed & Verified] => 0 [SD Code] => 37412 [SD-Name] => SATELLITE TOWN ) [47] => Array ( [OGP Created] => 94 [Installed & Un-Verified Meters] => 8 [Installed & Verified] => 2 [SD Code] => 37226 [SD-Name] => SEHWAN SHARIF ) [48] => Array ( [OGP Created] => 18 [Installed & Un-Verified Meters] => 2 [Installed & Verified] => 13 [SD Code] => 37154 [SD-Name] => SH:UMAID ALI KHAN ) [49] => Array ( [OGP Created] => 16 [Installed & Un-Verified Meters] => 0 [Installed & Verified] => 1 [SD Code] => 37223 [SD-Name] => SHAHBAZ ) [50] => Array ( [OGP Created] => 20 [Installed & Un-Verified Meters] => 20 [Installed & Verified] => 0 [SD Code] => 37343 [SD-Name] => SHAHDAD PUR-I ) [51] => Array ( [OGP Created] => 20 [Installed & Un-Verified Meters] => 20 [Installed & Verified] => 0 [SD Code] => 37344 [SD-Name] => SHAHDAD PUR-II ) [52] => Array ( [OGP Created] => 20 [Installed & Un-Verified Meters] => 20 [Installed & Verified] => 0 [SD Code] => 37346 [SD-Name] => SHAHPUR CHAKAR ) [53] => Array ( [OGP Created] => 20 [Installed & Un-Verified Meters] => 20 [Installed & Verified] => 0 [SD Code] => 37347 [SD-Name] => SINDHRI ) [54] => Array ( [OGP Created] => 111 [Installed & Un-Verified Meters] => 0 [Installed & Verified] => 0 [SD Code] => 37313 [SD-Name] => SOCIETY ) [55] => Array ( [OGP Created] => 37 [Installed & Un-Verified Meters] => 5 [Installed & Verified] => 0 [SD Code] => 37272 [SD-Name] => SUJAWAL ) [56] => Array ( [OGP Created] => 24 [Installed & Un-Verified Meters] => 4 [Installed & Verified] => 0 [SD Code] => 37251 [SD-Name] => T.MUHAMMAD KHAN-I ) [57] => Array ( [OGP Created] => 8 [Installed & Un-Verified Meters] => 5 [Installed & Verified] => 0 [SD Code] => 37252 [SD-Name] => T.MUHAMMAD KHAN-II ) [58] => Array ( [OGP Created] => 36 [Installed & Un-Verified Meters] => 11 [Installed & Verified] => 0 [SD Code] => 37282 [SD-Name] => TALHAR ) [59] => Array ( [OGP Created] => 4 [Installed & Un-Verified Meters] => 1 [Installed & Verified] => 0 [SD Code] => 37331 [SD-Name] => TANDO ADAM-I ) [60] => Array ( [OGP Created] => 18 [Installed & Un-Verified Meters] => 12 [Installed & Verified] => 0 [SD Code] => 37332 [SD-Name] => TANDO ADAM-II ) [61] => Array ( [OGP Created] => 97 [Installed & Un-Verified Meters] => 31 [Installed & Verified] => 20 [SD Code] => 37141 [SD-Name] => TANDO ALLAH YAR-I ) [62] => Array ( [OGP Created] => 82 [Installed & Un-Verified Meters] => 1 [Installed & Verified] => 11 [SD Code] => 37142 [SD-Name] => TANDO ALLAH YAR-II ) [63] => Array ( [OGP Created] => 22 [Installed & Un-Verified Meters] => 2 [Installed & Verified] => 0 [SD Code] => 37143 [SD-Name] => TANDO JAM ) [64] => Array ( [OGP Created] => 47 [Installed & Un-Verified Meters] => 27 [Installed & Verified] => 0 [SD Code] => 37271 [SD-Name] => THATTA ) [65] => Array ( [OGP Created] => 48 [Installed & Un-Verified Meters] => 19 [Installed & Verified] => 0 [SD Code] => 37431 [SD-Name] => UMERKOT ) )

如上所述,我想在一封电子邮件中发送多条记录。

我还要加OGP Created,'已安装&未经验证的仪表'和'已安装&已验证的'计数值,即将它们与单独的变量相加

更新1

根据建议,csv附件可以使用。但是我想补充一些东西。正如我上面所说,我希望sum up所有前三列的计数值。所以我试图像这样添加它们

 foreach ($result as $set)
    {
        $sum_OGP +=$set['OGP_Created'];
        $sum_UnVerified +=$set['Installed _& _Un_Verified_Meters'];
        $sum_Verified +=$set['Installed_& _Verified_Meters'];
    }

    echo "Total OGP ";
    print_r($sum_OGP);
    echo "<br>";
    echo "<br>";
    echo "Total Un-Verified Meters ";
    print_r($sum_UnVerified);
    echo "<br>";
    echo "<br>";
    echo "Total Verified Meters ";
    print_r($sum_Verified);
    exit();

output

Total OGP 2813

Total Un-Verified Meters 712

Total Verified Meters 181

总和是正确的,但如何将其发送到附件?

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

如果您想将查询的输出发送到电子邮件中,那么您应该创建一个csv文件并通过电子邮件作为附件发送。

public function test() {
    $sql = "SELECT COUNT(DISTINCT od.`meter_serial`) AS 'OGP Created', 
     COUNT(DISTINCT mp.`meter_id`) AS 'Installed & Un-Verified Meters', 
     COUNT(DISTINCT ins.`meter_msn`) AS 'Installed & Verified', 
     sd.`sub_div_code` AS 'SD Code',sd.`name` AS 'SD-Name'
     FROM `ogp_detail` od
     INNER JOIN `survey_hesco_subdivision` sd ON od.`sub_div` = 
     sd.`sub_div_code`
     LEFT JOIN `meter_ping` mp ON od.`meter_id` = mp.`meter_id`
     LEFT JOIN `installations` ins ON od.`meter_serial` = ins.`meter_msn`
     WHERE od.`meter_type` = '3-Phase'
     GROUP BY sd.`name`";
    $results = Yii::$app->db->createCommand($sql)->queryAll();

    //create a csv file
    $filename   =   $this->getAttachment($results);

    //send email 
    $this->sendEmail('omer@omer.com',$filename);
}

/**
 * 
 * @param type $email
 * @param type $filename
 * @return type
 */
public function sendEmail($email,$filename)
{
    return Yii::$app->mailer->compose()
        ->setTo($email)
        ->setFrom(['admin@domain.com' => 'Admin'])
        ->setSubject('Some Subject for the email')
        ->setTextBody('Text body of the email ')
        ->attach($filename,['filename'=>'information','contentType'=>'text/csv'])
        ->send();
}

/**
 * 
 * @param type $results
 * @return string $filename
 */
public function getAttachment($results) {
    $filename = Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . 'my-attachment-' . time() . '.csv';

    //open a csv file
    $file = fopen($filename, "w");

    $headerInjected = false;
    $header = ['OGP Created', 'Installed & Un-Verified Meters', 'Installed & Verified', 'SD Code', 'SD-Name'];

    //write lines to the csv file 
    foreach ($results as $result) {
        if (!$headerInjected) {
            $headerInjected = true;
            fputcsv($file, $header);
        }
        fputcsv($file, $result);
        $sum_OGP +=$result['OGP_Created'];
        $sum_UnVerified +=$result['Installed_&_Un_Verified_Meters'];
        $sum_Verified +=$result['Installed_&_Verified_Meters'];
    }

    //add the sum in the last row
    fputcsv($file,[$sum_OGP,$sum_UnVerified,$sum_Verified]);

    //close the file handle
    fclose($file);
    return $filename;
}