简体   繁体   中英

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. 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

    Envio emailto

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

    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

The value returned by $row['imagem'] is empty. 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.

You should check (a) if that row has a filename and (b) that the file was read successfully:

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.

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.

PS - If this is anything important, please, please take a minute to learn about SQL Injection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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