【问题标题】:Is possible serialize a PHPmailer object and use it through sessions?是否可以序列化 PHPmailer 对象并通过会话使用它?
【发布时间】:2014-11-05 21:42:18
【问题描述】:

这是我的问题, 我有一个 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

【问题讨论】:

  • PHPMailer 官方文档在这里:phpmailer.github.io/PHPMailer
  • 你应该加载 PHPMailer 的自动加载器而不是直接加载类。无需创建实例,然后将其替换为unserialize 的输出。实例中肯定存在差异,因此我建议您在每个上下文中执行 var_dump 并比较它们,看看第二个中缺少什么。 PHPMailer 中没有__sleep 魔术方法,除了可能的 SMTP 连接句柄之外没有其他资源,它将被重新生成,所以序列化应该没问题。
  • 看起来序列化很好:$mail = unserialize(serialize($mail)); 非常适合我,所以问题一定出在其他地方..
  • 感谢您的建议,但没有任何效果。我解决了在第二个文件中创建新的 PHPmailer 对象并再次配置的问题。

标签: php email session serialization phpmailer


【解决方案1】:

这很容易......

PHPMailer 可以发送预先创建的电子邮件,如果它被分成标题和正文。 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 2011-01-08
    相关资源
    最近更新 更多