Cledson Oliveira
Cledson Oliveira

Reputation: 45

Is possible serialize a PHPmailer object and use it through sessions?

Here is my problem, I have a PHPmailer object that works fine in the file "enviar1.php",but when I serialize this object,store it in a session variable and try to unserialize and use it in another file,it doesnt work.the issue is that the email is sent,but with no subject,no body and no attachments.

File:"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
}
[...]

the problem is in this file "enviar2.php",the email is sent to the correct destiny but empty,no subject,no body and none attachments.

file:"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;
[...]

I dont know why it is not working,I used a echo and printed the Properties of the $emailcompleto object,they have correct values,it has the same subject and body of the $email object from the "enviar1.php" file has,but in the end i get a empty email. someone have a clue of what is happening?

doc of class PHPmailer: http://www.tig12.net/downloads/apidocs/wp/wp-includes/PHPMailer.class.html#det_methods_AddAddress

Upvotes: 1

Views: 908

Answers (1)

Michael
Michael

Reputation: 9293

It's easy....

PHPMailer can send a pre-created email if it's separated into Header and Body. PHPMailer can also create a Header and Body separate of each other:

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

Once you have that data - you can store it in any manner you choose. Calling it up at a later time to send using any of the 3 PHPMailer Send() methods:

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

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

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

Upvotes: 0

Related Questions