Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php codeigniter将pdf文件作为电子邮件附件发送_Php_Codeigniter_Email_Tcpdf - Fatal编程技术网

Php codeigniter将pdf文件作为电子邮件附件发送

Php codeigniter将pdf文件作为电子邮件附件发送,php,codeigniter,email,tcpdf,Php,Codeigniter,Email,Tcpdf,我正在使用TCPDF动态生成pdf文件。通过使用TCPDF,我获得了base64编码的原始文件,现在我想使用codeigniteremail helper功能将这些原始数据作为电子邮件附件发送 你怎么能做到这一点 在将文件附加到电子邮件之前,必须先保存该文件 $obj = new $this->pdf; $obj->SetSubject('BLAH BLAH'); // set document information $obj->SetKe

我正在使用
TCPDF
动态生成pdf文件。通过使用TCPDF,我获得了base64编码的原始文件,现在我想使用
codeigniter
email helper功能将这些原始数据作为电子邮件附件发送


你怎么能做到这一点

在将文件附加到电子邮件之前,必须先保存该文件

      $obj = new $this->pdf;
      $obj->SetSubject('BLAH BLAH'); // set document information
      $obj->SetKeywords('Blah, Blah, Blah'); 
      $obj->AddPage(); // add a page
      $obj->SetFont('helvetica', '', 6);
      $obj->writeHTML("Your text goes here", true, false, false, false, '');
      $obj->Output('Path/to/save/filename.pdf', 'F'); //Close and output PDF document
然后像这样发送电子邮件

     $this->load->library('email');
     $this->email->from('noreply@example.com', 'Example');
     $this->email->to('to@example.com');
     $this->email->subject('Subject Goes Here');
     $this->email->message('Message goes here');
     $this->email->attach('Path/to/saved/filename.pdf');
     $this->email->send();

您必须先保存文件,然后再将其与电子邮件一起附加

      $obj = new $this->pdf;
      $obj->SetSubject('BLAH BLAH'); // set document information
      $obj->SetKeywords('Blah, Blah, Blah'); 
      $obj->AddPage(); // add a page
      $obj->SetFont('helvetica', '', 6);
      $obj->writeHTML("Your text goes here", true, false, false, false, '');
      $obj->Output('Path/to/save/filename.pdf', 'F'); //Close and output PDF document
然后像这样发送电子邮件

     $this->load->library('email');
     $this->email->from('noreply@example.com', 'Example');
     $this->email->to('to@example.com');
     $this->email->subject('Subject Goes Here');
     $this->email->message('Message goes here');
     $this->email->attach('Path/to/saved/filename.pdf');
     $this->email->send();

我花了一段时间才找到答案,希望它对未来的任何人都有帮助:

    // 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();

我花了一段时间才找到答案,希望它对未来的任何人都有帮助:

    // 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();

您需要将其保存为服务器上的文件,然后将其附加。有没有办法不将文件保存到服务器上?是的,您必须扩展电子邮件库并更改一些方法(尤其是
\u build\u message()
)以接受原始文件内容而不是文件路径。浏览库,您将看到它如何处理附件。扩展和更改以满足您的需要。您需要将其保存为服务器上的文件,然后附加。有没有办法不将文件保存到服务器上?是的,您必须扩展电子邮件库并更改一些方法(尤其是
\u build\u message()
)以接受原始文件内容而不是文件路径。浏览库,您将看到它如何处理附件。扩展和更改以满足您的需要。我知道这一点,但我希望在服务器上发送电子邮件时不保存pdf。我知道这一点,但我希望在服务器上发送电子邮件时不保存pdf。