通过电子邮件发送mysql查询结果

时间:2019-01-30 17:54:24

标签: php mysqli

我不确定发生了什么,但是下面的功能不起作用。我没有收到任何错误,但也没有发送任何电子邮件。如果有人可以帮助我了解我在做什么错,那将是很棒的。

更新-添加了CreateEmailString函数以供进一步参考

function CreateEmailString($data) {
    if (!$fp = fopen('php://temp', 'w+')) return FALSE;

    while ($row = $data->fetch_array(MYSQLI_NUM)) {
        fputcsv($fp, array_values($row));
    }

    rewind($fp);

    return stream_get_contents($fp);

}
function myEmail($_jn, $_cn, $_pt, $_sn, $_tn, $_rg, $_of, $_dv, $_mgr) {
    $body = "blah blah blah";
    $to = "you@email.com";
    $subject = "testing";
    $from = "me@email.com";
    $servername = "localhost"; 
    $username = "root"; 
    $password = "";
    $database = "Company_Intranet";
    $sql = "select projects.job_number, projects.client_name, projects.project_title, projects.section, projects.town, projects.range, offices.name as office, divisions.name as department, CONCAT(employees.first_name, ' ', employees.last_name) as manager
            from projects
            join offices on projects.managing_office = offices.id
            join divisions on projects.discipline = divisions.id
            join employees on projects.project_manager = employees.id
            where (projects.job_number like '$_jn' or projects.job_number is Null)
            and (projects.client_name like '$_cn' or projects.client_name is Null)
            and (projects.project_title like '$_pt' or projects.project_title is Null)
            and (projects.section like '$_sn' or projects.section is Null)
            and (projects.town like '$_tn' or projects.town is Null)
            and (projects.range like '$_rg' or projects.range is Null)
            and (offices.name like '$_of' or offices.name is Null)
            and (divisions.name like '$_dv' or divisions.name is Null)
            and (concat(employees.first_name, ' ', employees.last_name) like '$_mgr' or concat(employees.first_name, ' ', employees.last_name) is Null)
            ";   

    $db_con = mysqli_connect($servername, $username, $password, $database);

    $data = $db_con->query($sql);

    $multipartSep = '-----'.md5(time()).'-----';

    $headers = array(
        "From:".$from,
        "Reply-To:".$from,
        "Content-Type: multipart/mixed; boundary=".$multipartSep
    );

    $attachment = chunk_split(base64_encode(CreateEmailString($data))); 

    $body = "--$multipartSep\r\n"
        . "Content-Type: text/plain; charset=ISO-8859-1; format=flowed\r\n"
        . "Content-Transfer-Encoding: 7bit\r\n"
        . "\r\n"
        . "$body\r\n"
        . "--$multipartSep\r\n"
        . "Content-Type: text/csv\r\n"
        . "Content-Transfer-Encoding: base64\r\n"
        . "Content-Disposition: attachment; filename=TestEmail- . date('F-j-Y') . '.csv'\r\n"
        . "\r\n"
        . "$attachment\r\n"
        . "--$multipartSep--";

    return @mail($to, $subject, $body, implode("\r\n", $headers)); 

}

0 个答案:

没有答案