如何在codeigniter中使用sendgrid api库作为第三方库

如何在 codeigniter 中使用 sendgrid 库作为第三方。在他们的网站上,我可以找到扩展普通 codeigniter 电子邮件类的 smtp 代码。有什么帮助吗?


<?php

$this->load->library('email');


$this->email->initialize(array(

  'protocol' => 'smtp',

  'smtp_host' => 'smtp.sendgrid.net',

  'smtp_user' => 'sendgridusername',

  'smtp_pass' => 'sendgridpassword',

  'smtp_port' => 587,

  'crlf' => "\r\n",

  'newline' => "\r\n"

));


$this->email->from('your@example.com', 'Your Name');

$this->email->to('someoneexampexample@example.com');

$this->email->cc('another@another-example.com');

$this->email->bcc('them@their-example.com');

$this->email->subject('Email Test');

$this->email->message('Testing the email class.');

$this->email->send();


echo $this->email->print_debugger();

?>


浮云间
浏览 76回答 1
1回答

梦里花落0921

Step1:将您的 sendgrid 库复制到第三方文件夹 step2:将以下代码添加到您的控制器方法include APPPATH . 'third_party/sendgrid-php/sendgrid-php.php';&nbsp; &nbsp; &nbsp; &nbsp; //$this->load->third_party('sendgrid-php');&nbsp; &nbsp; &nbsp; &nbsp; $email = new \SendGrid\Mail\Mail();&nbsp; &nbsp; &nbsp; &nbsp; $email->setFrom("setfrom@test.com", "testname");&nbsp; &nbsp; &nbsp; &nbsp; $email->setSubject("MailTest");&nbsp; &nbsp; &nbsp; &nbsp; $email->addTo('tomail@gmail.com', "User");&nbsp; &nbsp; &nbsp; &nbsp; $email->addContent("text/plain", "subject");&nbsp; &nbsp; &nbsp; &nbsp; $email->addContent(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "text/html",'<html><body>Message</body><html>');&nbsp; &nbsp; &nbsp; &nbsp; $sendgrid = new \SendGrid(('sendgrid-API-KEY_HERE'));&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $response = $sendgrid->send($email);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print $response->statusCode() . "\n";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print_r($response->headers());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print $response->body() . "\n";&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception $e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo 'Caught exception: '. $e->getMessage() ."\n";&nbsp; &nbsp; &nbsp; &nbsp; }希望它会工作
打开App,查看更多内容
随时随地看视频慕课网APP