2

我正在使用TCPDF. 通过使用 TCPDF,我得到了 base64 编码的原始文件,现在我想使用codeigniter电子邮件助手功能将此原始数据作为电子邮件附件发送。

怎么能做到这一点?

4

4 回答 4

4

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

    // 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();
于 2015-01-15T14:33:27.653 回答
2
 /*its batter to make function*/ 

 /*-------COPY THIS CODE---- */

  $newFile  = 'Path/to/save/filename.pdf';
  $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($newFile, 'F'); //Close and output PDF document

 /*-------SENDING EMAIL---- */

 $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($newFile);
 $this->email->send();
于 2013-05-20T08:02:16.967 回答
0

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

      $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();
于 2013-03-29T08:14:02.867 回答
-2
  $newFile  = 'Path/to/save/filename.pdf';
  $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($newFile, 'F'); //Close and output PDF document
于 2014-05-17T04:21:45.093 回答