【问题标题】:Email in loop sends the same file using Email class in Codeigniter循环中的电子邮件使用 Codeigniter 中的电子邮件类发送相同的文件
【发布时间】:2013-05-07 14:16:31
【问题描述】:

嘿,我正在使用 Codeigniter 的电子邮件助手,但遇到了一个奇怪的问题。我有以下代码:

        $path = mpdf_create_billing($html);
        $this->load->library('email');
        $this->email->set_newline("\r\n");
        $this->email->from('no-reply@foo.com');
        $this->email->to("foo@gmail.com");
        $this->email->subject('Invoice for  '.date('d/m/y'));
        $this->email->message($message);
        $this->email->attach($path);
        if($this->email->send())
            echo "Email Sent Successfully to $email with the file $path<br>";
        else
            echo "Should be sending email to $email , but i didn't<br>";

现在这段代码在一个 foreach 循环中,在这种情况下是两次。 mpdf_create_billing 返回 PDF 文件的路径。现在这段代码回显了 2 个不同的文件路径,但电子邮件是相同的,并且在两个循环运行中,两封电子邮件都包含相同的文件,尽管文件路径不同。

有人知道怎么解决吗?这就是我的输出:

Email Sent Successfully to foo@foo.com with the file
   /path/to/pdf/Invoice_1368452801.82065190eec1c85eb.pdf

Email Sent Successfully to foo@foo.com with the file 
  /path/to/pdf/Invoice_1368452804.53475190eec482917.pdf

这可能是我发送电子邮件的 SMTP 服务器的问题吗?我在 2 个邮件帐户上尝试过,结果相同。

【问题讨论】:

  • 为什么不在循环之外创建$path
  • @andrewsi 每次运行的路径不同,因为我想发送不同的文件,但发送的是同一个文件。
  • 我会说在这种情况下,这是 $html 变量的问题 - 你能发布其余的 for 循环代码吗?
  • @andrewsi 其余都是对函数的调用,它与$html var 无关,两个路径都是创建的,实际文件不同并且存在于它们的路径中。而$html 只是一个字符串
  • 因此生成了两个不同的 PDF 文件,但是邮件程序发送了两封电子邮件,都包括第一封?我想我唯一能建议的是您尝试明确取消设置电子邮件,看看是否有帮助。

标签: php codeigniter email smtp


【解决方案1】:

也许你应该清除 $this->email?

来自 CodeIgniter 文档:

$this->email->clear()

将所有电子邮件变量初始化为空状态。这个功能 用于在循环中运行电子邮件发送功能时使用, 允许在周期之间重置数据。

foreach ($list as $name => $address)
{
    $this->email->clear();

    $this->email->to($address);
    $this->email->from('your@example.com');
    $this->email->subject('Here is your info '.$name);
    $this->email->message('Hi '.$name.' Here is the info you requested.');
    $this->email->send();
}

如果您将参数设置为 TRUE,任何附件都将被清除为 好吧:

$this->email->clear(TRUE);

在我看来这是你在做什么?

链接 CI3:https://www.codeigniter.com/user_guide/libraries/email.html

链接 CI2:https://www.codeigniter.com/userguide2/libraries/email.html

【讨论】:

    猜你喜欢
    • 2017-12-07
    • 2012-03-22
    • 2015-09-06
    • 2012-12-29
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 2019-08-07
    相关资源
    最近更新 更多