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

时间:2014-09-11 19:59:40

标签: php email session serialization phpmailer

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

文件: “enviar1.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”,电子邮件被发送到正确的命运,但是空,没有主题,没有正文和没有附件。

的文件: “Enviar2.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

1 个答案:

答案 0 :(得分:0)

这很容易......

如果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);