我在codeigniter中发送带有发送网格的附件邮件

时间:2016-05-12 07:06:32

标签: php codeigniter email

我在codeigniter中发送带有发送网格的附件邮件。邮件发送成功但下载后附件文件无法显示。我的代码: -

public function test() {
            $attach = base_url()."/uploads/a.png";
            $this->SendMail('asheesh9308@gmail.com', 'asheesh9308@gmail.com', 'sss sub', 'ss msg',$attach, 'dss.png');

        }

发送网格API

function SendMail($from, $email, $subject, $message, $attach = '', $filename = '') 
 {
       $url = 'https://api.sendgrid.com/';
       $user = 'XXXXXX';
       $pass = 'XXXXXX';

       if ($attach <> '' && $filename <> '') {

        $params = array(
                        'api_user' => $user,
                        'api_key' => $pass,
                        'to' => $email,
                        'fromname' => $from,
                        'from' => $from,
                        'subject' => $subject,
                        'html' => $message,
                        'files[' . $filename . ']' => new \CurlFile($attach),
                           'files[' . $filename . ']' => '@' . $attach,
                    );
        } else {

                $params = array(
                        'api_user' => $user,
                        'api_key' => $pass,
                        'to' => $email,
                        'fromname' => $from,
                        'from' => $from,
                        'subject' => $subject,
                        'html' => $message);
        }
        $request = $url . 'api/mail.send.json';
        $session = curl_init($request);
        curl_setopt($session, CURLOPT_POST, true);
        curl_setopt($session, CURLOPT_POSTFIELDS, $params);
        curl_setopt($session, CURLOPT_HEADER, false);
        // Tell PHP not to use SSLv3 (instead opting for TLS)
       //curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
       curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
       // obtain response
       $response = curl_exec($session);
        curl_close($session);
                return true; 
}

1 个答案:

答案 0 :(得分:0)

您需要以不同方式附加文件,将更多参数传递给Curlfile构造函数,如

new CurlFile('filename.png', 'image/png', 'filename.png')

检查这些链接ab