How to work with Mailgun API in CodeIgniter; Forbidden error in curl_exe()

匿名 (未验证) 提交于 2019-12-03 01:36:02

问题:

I am working on CodeIgniter, I am using Mailgun API to send the emails.But I am getting FORBIDDEN while echoing curl_exe() as shown in code:

 <?php Class Email3 extends CI_Controller     {         function __construct()         {             parent::__construct();         }         function index() {         $ua = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13';                 $ch = curl_init();               curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);                curl_setopt($ch, CURLOPT_USERPWD, 'my-api-key');               curl_setopt($ch, CURLOPT_HEADER, true);                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);               curl_setopt($ch, CURLOPT_USERAGENT, $ua);               curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');               curl_setopt($ch, CURLOPT_URL,                            'https://api.mailgun.net/v2/samples.mailgun.org/messages');               curl_setopt($ch, CURLOPT_POSTFIELDS,                              array('from' => 'mymail',                                   'to' => 'othermail',                                   'subject' => 'The Printer Caught Fire',                                   'text' => 'We have a problem.'));               $result = curl_exec($ch);               curl_close($ch);               echo $result;             }      }   ?> 

I googled it as well searched over SO also but Unable to get solution.Please help me for this. Also email is not been send.

回答1:

Here's a simple CI library you can use:

application/libraries/Mailgun.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');  class Mailgun {    protected $CI;   private static $api_key;   private static $api_base_url;    public function __construct() {     // assign CI super object     $this->CI =& get_instance();      // config     self::$api_key = "key-XXXXX";     self::$api_base_url = "https://api.mailgun.net/v3/mg.example.com";   }    /**    * Send mail    * $mail = array(from, to, subject, text)    */   public static function send($mail) {     $ch = curl_init();     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);     curl_setopt($ch, CURLOPT_USERPWD, 'api:' . self::$api_key);     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');     curl_setopt($ch, CURLOPT_URL, self::$api_base_url . '/messages');     curl_setopt($ch, CURLOPT_POSTFIELDS, $mail);     $result = curl_exec($ch);     curl_close($ch);     return $result;   }  } 

Then in your controller:

application/controllers/Email.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');  class Email extends CI_Controller {    public function index() {     $this->load->library('mailgun');     $this->mailgun::send([       'from' => "Example.com team <no-reply@mg.example.com>",       'to' => "somerandomuser@gmail.com",       'subject' => "Welcome to Example.com",       'text' => "We just want to say hi. Have fun at Example.com"     ]);   }  } 

You must use a valid API key and base url for it to work.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!