phpmailer类的base64_encode中的警告

时间:2018-11-30 05:58:55

标签: php phpmailer

我正在使用phpmailer发送带有附件的邮件,该邮件正在发送带有空白附件的邮件,并且显示以下警告

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

我从数据库中检索文件,该文件存储为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); 
?>

1 个答案:

答案 0 :(得分:0)

您收到此错误,因为$result包含MySQLi结果集对象(mysqli_result),而不是字符串。您需要先提取字段值,然后再使用它。另外,PHPMailer将从提供的文件名(在$result1中为您找出编码和内容类型,因此您无需自己设置它们,就像这样:

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

另一个问题是,您使用的是非常老的PHPMailer版本,该版本具有安全漏洞和许多错误,并且您的代码基于一个过时的示例,因此get the latest