首页 > 解决方案 > phpmailer 类中 base64_encode 中的警告

问题描述

我正在使用 phpmailer 发送带有附件的邮件,这是发送带有空白附件的邮件,它显示以下警告

警告:base64_encode() 期望参数 1 是字符串,对象在 D:\xampp\htdocs\contactform\class\class.phpmailer.php 1958 行

我从存储为 BLOB 文件的数据库中检索文件

<?php
require 'config.php';        
require 'class/class.phpmailer.php';
$message = '';
$errors ='';
$firstName = $lastName = $emailId = $mobileNumber = $add = '';  

function clean_text($string)
{
    $string = trim($string);
    $string = stripslashes($string);
    $string = htmlspecialchars($string);
    return $string;
}


$firstName = $conn->real_escape_string($_POST['fname']);
$lastName = $conn->real_escape_string($_POST['lname']);
$emailId = $conn->real_escape_string($_POST['email']);
$mobileNumber = $conn->real_escape_string($_POST['mobile']);
$add = $conn->real_escape_string($_POST['address']);
$fileName = $conn->real_escape_string($_FILES['myfile']['name']);
$tmp_name = $_FILES['myfile']['tmp_name']; 
$name = $_FILES['myfile']['name'];   
$size = $_FILES['myfile']['size']; 


if(isset($_POST["submit"]))
{
    if((isset($_POST['fname'])&& $_POST['fname'] !='' ))
    {   
        $sql = "INSERT INTO form (fname, lname, email,mobile,address,file,filename,created) VALUES('".$firstName."','".$lastName."','".$emailId."', '".$mobileNumber."','".$add."','".$fileName."','".$name."',now())";
        if(!$result = $conn->query($sql)){
        die('There was an error running the query [' . $conn->error . ']');
    }
    else
    {
        echo "Registered successfully\n ";
    }
}

else
{
    echo "Please fill Name and Email";
}
    $query="select file from form where email='$emailId'"; 
    $result=$conn->query($query) or die('There was an error1 running the query [' . $conn->error . ']'); 
    $result1="resume.pdf";
    $encoding='base64'; 
    $type=" application/pdf";
    $message ="hi";
    $mail = new PHPMailer;
    $mail->IsSMTP();                                
    $mail->Host = 'smtp.gmail.com';     
    $mail->Port = '587';                            
    $mail->SMTPAuth = true;                         
    $mail->Username = '****';                   
    $mail->Password = '****';                       
    $mail->SMTPSecure = 'tls';                          
    $mail->From = $_POST["email"];                  
    $mail->FromName = $_POST["fname"];              
    $mail->AddAddress('***');       
    $mail->WordWrap = 50;                           
    $mail->IsHTML(true);                            
    $mail->AddStringAttachment($result,$result1,$encoding,$type).
    $mail->Subject = 'Applicant Profile';               
    $mail->Body = $message;                         
    if($mail->Send())                               
    {
        $message = '<div class="alert alert-success">Application Successfully Submitted</div>';
    }
    else
    {
        $message = '<div class="alert alert-danger">There is an Error</div>';
        echo $mail->ErrorInfo;
    }
}
print_r($message); 
?>

标签: phpphpmailer

解决方案


您收到此错误是因为$result包含 MySQLi 结果集对象 ( mysqli_result),而不是字符串。您需要在使用之前提取字段值。PHPMailer 还会根据您提供的文件名 (in ) 为您找出编码和内容类型$result1,因此您无需自己设置它们,如下所示:

$row = $result->fetch_row();
$mail->addStringAttachment($row[0], $result1);

另一个问题是您使用的是非常旧的 PHPMailer 版本,它存在安全漏洞和许多错误,并且您的代码基于过时的示例,因此请获取最新的.


推荐阅读