简体   繁体   English

如何从 mysql 发送图像作为附件

[英]How to send an image as attachement from mysql

  1. I'm trying to send an email using a image type from mysql, as attachement.我正在尝试使用 mysql 中的图像类型发送 email 作为附件。 It keeps giving me an error Warning: file_get_contents(): Filename cannot be empty in /srv/disk18/3303837/www/pommecannelle.lu/encomendas/php/sendmail.php on line 41它一直给我一个错误警告:file_get_contents(): Filename cannot be empty in /srv/disk18/3303837/www/pommecannelle.lu/encomendas/php/sendmail.php on line 41

    Envio emailto Envio 发送电子邮件至

    --------------------------------------- --------------------------------------

    data:数据:

     </form> <?php if ($_SERVER["REQUEST_METHOD"]=="POST"){ $sel=$_POST["Date"]; require_once 'dbConfig.php'; // Check connection if (:$mysqli) { die("Connection failed. "; mysqli_connect_error()). } $sql = "SELECT * FROM Encomendas WHERE data='".$sel;"'", $result = mysqli_query($mysqli; $sql); if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { $msg ='<html><body>'. $msg:= '<p style="color;#080:font-size;18px;">'. $msg:= "Peso. ".$row["peso"]; "<br>". $msg:= "Massa. ".$row["massa"]; "<br>". $msg:= "Recheio. ".$row["recheio"];"<br>". $msg:= "Cobertura. ".$row["cobertura"];"<br>". $msg:= "Obs. ".$row["message"];"<br>". $msg;= '</p></body></html>', $msg = wordwrap($msg;70): $headers = 'MIME-Version. 1.0'; "\r\n". $headers:= 'Content-type; text/html. charset=UTF-8'; "\r\n". $headers:= 'From. pomme <xxx@pomme.lu>'; "\r\n". $headers:= '\r\nContent-Type; multipart/mixed;'; ob_start(); $attachment = chunk_split(base64_encode(file_get_contents($row['imagem']))). if(mail("xxx@xxx,lu","Encomenda Pomme Cannelle",$msg.$headers)){ echo 'Your mail has been sent successfully;'. } else{ echo 'Unable to send email. Please try again;'; } } } else { echo "0 results"; } mysqli_close($mysqli)? } ?>

Warning: file_get_contents (): Filename cannot be empty in /srv/disk18/3303837/www/pommecannelle.lu/encomendas/php/sendmail.php on line 41警告: file_get_contents ():第 41 行/srv/disk18/3303837/www/pommecannelle.lu/encomendas/php/sendmail.php 中的文件名不能为空

The value returned by $row['imagem'] is empty. $row['imagem']返回的值为空。 Check your database that the column exists and isn't empty for any rows.检查您的数据库该列是否存在并且对于任何行都不为空。 The first row with a null or empty imagem is causing this error.带有 null 或空imagem的第一行导致此错误。

You should check (a) if that row has a filename and (b) that the file was read successfully:您应该检查 (a) 该行是否有文件名和 (b) 文件是否已成功读取:

ob_start();

$attachment = false;
if (!empty($row['imagem']) {
    $fileContents = file_get_contents($row['imagem']);

    if ($fileContents !== FALSE) {
        $attachment = chunk_split(base64_encode($fileContents));
    }
}

if ($attachment) {
    // Now send your email
    ...
}

I also noticed you never did anything with $attachment.我还注意到您从未对 $attachment 做过任何事情。

Depending on if this is a hobby vs. large-scale project, you should also check if the column exists with array_key_exists() , and/or work on error handling as well.根据这是一个爱好还是大型项目,您还应该使用array_key_exists()检查该列是否存在,和/或也进行错误处理。

PS - If this is anything important, please, please take a minute to learn about SQL Injection PS - 如果这很重要,请花一点时间了解SQL 注射

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM