使用Codeigniter中的URL发送带附件的电子邮件

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (5)

我想发送附件附件的电子邮件。

使用文件路径发送电子邮件没有问题,但我更喜欢使用URL设置附件。

电子邮件可以成功发送到我的电子邮件,无需附件。

我的PDF URL示例:http://store/index.php/Store/GI/OrderID.pdf

希望有些人可以帮助我。万分感激。谢谢。

控制器::

    public function SendInvoice(){
        $orderid = $this->uri->segment(3);
        $result = $this->order->GetCustOrder($orderid);

        unset($data);
        $data = array(
                        $OrderID,
                        $result[0]->cust_id,
                        $result[0]->cust_name,
                        $result[0]->cust_email
                      );
        $this->store_email->SendInvoiceToCust($data); //library

        $this->session->set_flashdata('sent_email','Email has been sent to customer.');
        redirect('Store/Index');
    }   

图书馆::

    public function SendInvoiceToCust($data){
        $message = "Dear Valued Customer, <br><br>Thank you for Purchased at Store. <br/><br/>Kindly refer the attachment to view your Invoice.";
        //$attached_file = base_url(). 'index.php/Store/GI/' .$data[0]. '.pdf';
        $form = base_url(). 'index.php/Store/GI/' .$data[0]. '.pdf';
        $attachment = chunk_split(base64_encode($form));

        $separator = md5(time());

        $message .= "--" . $separator . PHP_EOL;
        $message .= "Content-Type: application/octet-stream; name=\"\" . $data[0]. '.pdf'\"" . PHP_EOL;
        $message .= "Content-Transfer-Encoding: base64" . PHP_EOL;
        $message .= "Content-Disposition: attachment" . PHP_EOL . PHP_EOL;
        $message .= $attachment . PHP_EOL . PHP_EOL;
        $message .= "--" . $separator . "--";           

        $this->email->from('email@gmail.com', 'Store');
        $this->email->to('email2@gmail.com'); 

        $this->email->subject('Store INVOICE [Do not reply]');
        $this->email->message($message);
        $this->email->attach($attachment);
        $this->email->send();


    }
提问于
用户回答回答于

我还不能评论,但你应该尝试设置内容类型和编码pdf。以下是我目前用于在codeigniter中发送带附件的电子邮件。

    $form = base_url(). 'index.php/Store/GI/' .$data[0]. '.pdf';
    $attachment = chunk_split(base64_encode($form));

    $separator = md5(time());

    $message .= "--" . $separator . PHP_EOL;
    $message .= "Content-Type: application/octet-stream; name=\"" . $data[0]. '.pdf'\"" . PHP_EOL;
    $message .= "Content-Transfer-Encoding: base64" . PHP_EOL;
    $message .= "Content-Disposition: attachment" . PHP_EOL . PHP_EOL;
    $message .= $attachment . PHP_EOL . PHP_EOL;
    $message .= "--" . $separator . "--";

扫码关注云+社区

领取腾讯云代金券