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

问题描述 投票:0回答:1
  1. 我正在尝试使用来自mysql的图像类型作为附件发送电子邮件。它一直给我一个错误警告:file_get_contents():/srv/disk18/3303837/www/pommecannelle.lu/encomendas/php/sendmail.php在第41行的文件名不能为空]

    Envio emailto --------------------------------------- 数据:

    </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 <[email protected]>' . "\r\n";   
    
                $headers .= '\r\nContent-Type: multipart/mixed;';
    
                ob_start();
                $attachment = chunk_split(base64_encode(file_get_contents($row['imagem'])));
    
                if(mail("[email protected]","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);
        }
     ?> 
    

php mysql email email-attachments
1个回答
0
投票

警告:文件获取内容():文件名不能为空第41行 /srv/disk18/3303837/www/pommecannelle.lu/encomendas/php/sendmail.php中

$row['imagem']返回的值为空。检查您的数据库该列是否存在,并且对于[[any

行而言,该列不为空。空[为空的第一行导致此错误。您应该检查(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 ... }

我也注意到您从未使用$ attachment做任何事情。

取决于这是否是业余爱好还是大型项目,还应该检查该列是否存在array_key_exists(),并且/或者也要进行错误处理。

P.S。 -如果这很重要,请花一点时间来了解SQL Injection

© www.soinside.com 2019 - 2024. All rights reserved.