如何从while循环创建csv文件并将其附加到php中的电子邮件?

如何从while循环创建csv文件并将其附加到php中的电子邮件?,php,Php,我已经做了一段时间了。我想做的是从while循环中创建一个CSV文件,包含代码和数量,然后将其附加到下面的电子邮件中,然后发送。任何帮助都将不胜感激 $sql = mysql_query("SELECT code, quantity FROM table WHERE active = '1' ORDER BY code Asc"); while($r

我已经做了一段时间了。我想做的是从while循环中创建一个CSV文件,包含代码和数量,然后将其附加到下面的电子邮件中,然后发送。任何帮助都将不胜感激

$sql = mysql_query("SELECT code, quantity 
                    FROM table 
                    WHERE active = '1' 
                    ORDER BY code Asc");

while($row = mysql_fetch_array($sql)){
    $code = $row['code'];
    $quantity = $row['quantity'];   
    //CREATE CSV FILE HERE I THINK?
}


$to = “email@gmail.com” ;
$from = “email@website.com”;
$subject = “Inventory”;
$message = ''<html>Code</html>'';    

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Inventory' . "\r\n";
      
mail($to, $subject, $message, $headers);    
$sql=mysql\u查询(“选择代码,数量
从桌子上
其中活动='1'
按代码(Asc)订购;
while($row=mysql\u fetch\u数组($sql)){
$code=$row['code'];
$quantity=$row['quantity'];
//我想在这里创建CSV文件?
}
$to=”email@gmail.com” ;
$from=”email@website.com”;
$subject=“存货”;
$message='Code';
$headers=“MIME版本:1.0”。“\r\n”;
$headers.=“内容类型:text/html;字符集=UTF-8”。“\r\n”;
$headers.='From:Inventory'。“\r\n”;
邮件($to、$subject、$message、$headers);
我希望它看起来像这样。.csv文件将是一个附件,我仍会在其下方显示常规电子邮件


给你。但是你需要一个PHPMailer。


要将其附加为.csv文件还是纯文本文件?请将其附加为.csv文件以在电子邮件中下载。我仍然希望显示常规html电子邮件,但将该文件作为附件。您不需要使用
phpMailer
发送附件,这可能会使发送附件更容易,但您可以手动完成所有操作,并使用
mail()
如何使用mail附加csv文件。我已经找了两天了,我知道他不需要它。但我写的是PHPMailernecessary@BTR383434初学者使用
PHPMailer
。在开发过程中,如果需要,您可以切换到
mail()
@BTR383434,您不能安装它还是不能使用它?
function create_cvs_from_db($sql)
{
    // create a temp location in memory with 5MB
    $csv = fopen('php://temp/maxmemory:' . (5 * 1024 * 1024), 'r+');

    // add the headers
    fputcsv($csv, array("code", "quantity"));
    // the loop
    while ($row = mysql_fetch_array($sql)) {
        $code = $row['code'];
        $quantity = $row['quantity'];
        // creates a valid csv string
        fputcsv($csv, array($code, $quantity));

    }

    rewind($csv);
    // return the the written data
    return stream_get_contents($csv);

}

// you need to setup PHPMailer
$mail = new PHPMailer();

// configurations

$mail->isSMTP();
$mail->Host = 'smtp.mailtrap.io';
$mail->SMTPAuth = true;
$mail->Username = '1a2b3c4d5e6f7g'; //your mail username
$mail->Password = '1a2b3c4d5e6f7g'; //your mail password
$mail->SMTPSecure = 'tls';
$mail->Port = 2525;


// now the fun part
$mail->setFrom('info@mailtrap.io', 'Mailtrap');
// the subject of the email
$mail->Subject = 'Test Email via Mailtrap SMTP using PHPMailer';
// if your mail body uses html
$mail->isHTML(true);
$mailContent = "";
$mail->Body = $mailContent;

/**
 * now here you attach your file
 */
// the cvs extension is very important
$filename = "summary.cvs";
$mail->addStringAttachment(create_cvs_from_db($sql), $filename);

// now send  the mail

if ($mail->send()):
    echo 'Message has been sent';
else:
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
endif;