codeigniter发送pdf文件作为电子邮件附件 [英] codeigniter send pdf file as email attachment

查看:226
本文介绍了codeigniter发送pdf文件作为电子邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 TCPDF 生成PDF文件。通过使用TCPDF我得到base64编码的原始文件现在我想使用 codeigniter 电子邮件帮助函数作为电子邮件附件发送这个原始数据。

I am generating pdf files on fly using TCPDF. By using TCPDF i am getting raw file with base64 encoded now i want to send this raw data as email attachment using codeigniter email helper function.

如何做到这一点?

推荐答案

花了一会儿找到答案,希望它能帮助未来的任何人:

Took my a while to find the answer, hope it helps anyone in the future:

    // Get email address and base64 via ajax
    $email = $this->input->post('email');
    $base64 = $this->input->post('base64');

    $base64 = str_replace('data:application/pdf;base64,', '', $base64);
    $base64 = str_replace(' ', '+', $base64);

    $data = base64_decode($base64);

    // Locally emails do now work, so I use this to connect through my gmail account to send emails
    $config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => 'MyEmailAddress@gmail.com',
        'smtp_pass' => 'MyEmailPassword',
        'mailtype'  => 'html', 
        'charset'   => 'iso-8859-1'
    );

    $this->load->library('email', $config);

    $this->email->set_newline("\r\n");
    $this->email->to($email);
    $this->email->from('noreply@whatever.ca', 'base64');
    $this->email->subject('base64');
    // Using the string_attach($str_file, $filename, $mime, $disposition = 'attachment')
    // function located in CodeIgniter\application\libraries\Email.php
    $this->email->string_attach($data, 'base64.pdf', 'application/pdf');

    $this->email->send();

这篇关于codeigniter发送pdf文件作为电子邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆