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

时间:2019-06-25 10:30:43

标签: php codeigniter

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

使用文件路径发送电子邮件没有问题,但是我更喜欢使用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();


    }

1 个答案:

答案 0 :(得分:0)

我还不能发表评论,但您应该尝试设置内容类型和对pdf进行编码。 以下是我目前用来在codeigniter中发送带有附件的电子邮件的内容。 我已经更新了回复。目前,这是我采用pdf格式并将其附加的方式。 所有操作均在$ message字段中完成,并设置了不同的内容传输和类型。

    $sender = "email@gmail.com";
    $emails = "email2@gmail.com";
    $subject = "Store INVOICE [Do not reply]";
    $headers = "From: " . $sender . PHP_EOL;
    $headers .= "MIME-Version: 1.0" . PHP_EOL;
    $message = "";

    $message = "--" . $separator . PHP_EOL;
    $message .= "Content-Type: text/html; charset=\"iso-8859-1\"" . PHP_EOL;
    $message .= "Content-Transfer-Encoding: 8bit" . PHP_EOL . PHP_EOL;
    $message .= ($content = "Dear Valued Customer, <br><br>Thank you for Purchased at Store. <br/><br/>Kindly refer the attachment to view your Invoice.");
    $message .= PHP_EOL . PHP_EOL;

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

    $separator = md5(time());

    $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . PHP_EOL;
    $headers .= "Content-Transfer-Encoding: 7bit";

    $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 . "--";

    $result = mail($emails, $subject, $message, $headers);