是否可以序列化PHPmailer对象并通过会话使用它?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了是否可以序列化PHPmailer对象并通过会话使用它?相关的知识,希望对你有一定的参考价值。

这是我的问题,我有一个phpmailer对象在文件“enviar1.php”中正常工作,但是当我序列化这个对象时,将它存储在会话变量中并尝试反序列化并在另一个文件中使用它,它不起作用。问题是电子邮件已发送,但没有主题,没有正文和附件。

文件:“send1.php”

[...]
$email = new PHPMailer();
$email->CharSet = 'UTF-8';
$emaildes = "";
$campusEsc = addslashes($campus);
$query = "SELECT email FROM bibliotecarios WHERE campus = '$campusEsc'";
$emailBibliotecario = mysqli_query($conn,$query);
while ($row = mysqli_fetch_assoc($emailBibliotecario)) 
    $emaildes = $row['email']; 
$email->isSMTP();
$email->Host ="ssl://smtp.googlemail.com";
$email->SMTPAuth = true;
$email->Port     = 465;
$email->Username = "someemail";
$email->Password = "somepass";
$email->From     = "someemail";
$email->FromName = "Sistema de Encaminhamento de Ficha";
$email->AddAddress($emaildes, "bibliotecario do campus " . $campus);
$email->Ishtml(true);
$email->Subject  = "ficha catalografica para revisao";
$mensagem = "<p>ficha do(a) aluno(a):" . $nome . " " . $sobrenome . "(" .$emailAluno    .   ") do campus de " . $campus . "</p>" ;
$mensagem .= "</br>";
$mensagem .= "</br>";
$email->Body = $mensagem;
$email->AddStringAttachment($string, 'ficha Catalografica - ' . $nome . " " .   $sobrenome . '.odt');
$email->AddStringAttachment($string2, 'ficha Catalografica - ' . $nome . " " . $sobrenome . '.docx');
$email->AddAttachment($_FILES["arquivo"]["tmp_name"],$_FILES["arquivo"]["name"] );

session_start();
$_SESSION['nome'] = $nome; //I need these variables to create TBS a object in "preview.php"
$_SESSION['sobrenome'] = $sobrenome; 
$_SESSION['email'] = $emailAluno;
$_SESSION['titulo'] = $titulo;
$_SESSION['subtitulo'] = $subtitulo; 
$_SESSION['ano'] = $ano;
$_SESSION['folhas'] = $folhas;
$_SESSION['ilus'] = $ilus;
$_SESSION['tipo'] = $tipo; 
$_SESSION['curso'] = $curso; 
$_SESSION['campus'] = $campus; 
$_SESSION['orientacao'] = $orientacao;
$_SESSION['coorientacao'] = $coorientacao;
$_SESSION['assunto1'] = $assunto1;
$_SESSION['assunto2'] = $assunto2;
$_SESSION['assunto3'] = $assunto3;
$_SESSION['assunto4'] = $assunto4;
$_SESSION['assunto5'] = $assunto5;

if($autorizacao != "true") //this is a checkbox from the previus form
{
    if ($email->Send())    //here the email works like a charm
         header("Location: preview.php");
    else 
        echo "Erro ao enviar o email";
}
else
{
    $_SESSION['emailParcial'] = serialize($email); //I stored in a session variable to edit the object later
    header("Location: termo.php");//just another page with a form
}
[...]

问题是在这个文件“enviar2.php”中,电子邮件被发送到正确的命运但是空,没有主题,没有正文和没有附件。

文件:“Send2.php”

<?php

ini_set("display_errors", true);
error_reporting(-1);

require 'configuracoes.php';
include_once('tbs_class.php');
include_once('tbs_plugin_opentbs.php');

function __autoload( $className ) {
require_once('./phpmailer/class.phpmailer.php');
}
[...]
$emailcompleto = new PHPMailer();
session_start();
$emailcompleto = unserialize($_SESSION['emailParcial']);
echo $emailcompleto->Body; //I put this to test the value and its ok
echo $emailcompleto->Subject; //the value is ok,the same as the $email object from "enviar1.php" file
if ($emailcompleto->Send()) //the email is sent but i get a empty email.
     header("Location: preview.php");
else 
    echo "Erro ao enviar o email" . $email->ErrorInfo;
[...]

我不知道它为什么不起作用,我使用echo并打印$ emailcompleto对象的属性,它们具有正确的值,它具有与“enviar1.php”文件中的$ email对象相同的主题和主体,但最后我收到一封空邮件。有人知道发生了什么?

PHPmailer类的文档:http://www.tig12.net/downloads/apidocs/wp/wp-includes/PHPMailer.class.html#det_methods_AddAddress

答案

这很简单....

如果PHPMailer分为Header和Body,它可以发送预先创建的电子邮件。 PHPMailer还可以创建彼此分开的Header和Body:

$header = $mailer->createHeader();
$body = $mailer->createBody();

拥有该数据后 - 您可以以您选择的任何方式存储它。稍后调用它以使用3个PHPMailer Send()方法中的任何一个发送:

// SMTP
$mailer->smtpSend($header,$body);

// Sendmail
$mailer->sendmailSend($header,$body);

// PHP Mail
$mailer->mailSend($header,$body);

以上是关于是否可以序列化PHPmailer对象并通过会话使用它?的主要内容,如果未能解决你的问题,请参考以下文章

PHPMailer:是不是可以为密件抄送设置不同的消息 ID?

PHP 通过PHPMailer发送邮件

PHP 通过PHPMailer发送邮件

跨浏览器会话持久化javascript对象可能吗? [复制]

Flask - 将对象直接存储在会话中[重复]

ThinkPHP 通过 PHPMailer插件来发送邮件