dongpiao8821 2017-03-08 10:55
浏览 68
已采纳

使用带有codeigniter的mailgun API

I'm trying to use the mailgun api to send user registration confirmation emails from my website. My site is on a shared host. The mailgun api seems to work perfectly, when I test it out on a localhost (WAMP server). when I try to use the api in my source code, i.e. inside a controller, I get a syntax error. The shared host is a linux based (CentOS) server.

This is the error message.

Parse error: syntax error, unexpected '[' in /var/www/html/log2pdf/mailgun/vendor/guzzlehttp/psr7/src/functions.php on line 78

Since it was a syntax error, I downloaded and installed the mailgun dependancies (i.e. composer, guzzle adapter etc.) separately on the linux server, because I had originally copied the files from my localhost (windows) and was under the impression that these files are different for windows and linux. But that made no difference either.

Running php 5.6.25 on localhost (WAMP), php 5.3.3 on shared host(CentOS). All the mailgun dependencies were installed using the commands on the mailgun webpage.

I have tried all that I could, any help on this will be greatly appreciated.

This is my controller:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require '/var/www/html/mysite/mailgun/vendor/autoload.php';
use Mailgun\Mailgun;

class Register extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->model('User_model');
    $this->load->helper('cookie');
}
public function index($param = '')
{

    if (($this->session->userdata('userLogin') == TRUE))  
    { 
        redirect(site_url('users/dashboard'));  }

    if (isset($_POST['registration'])){

        $data['postdata'] = $_POST;

        if(!empty($_POST['firstname']) && !empty($_POST['lastname']) && 
        !empty($_POST['email']) && !empty($_POST['password']) && 
        !empty($_POST['organization']) && !empty($_POST['country']) && 
        !empty($_POST['address'])){ 

            $checkEmail = $this->User_model->checkEmail();

            if($checkEmail==true){

                $this->session->set_flashdata('error', '<p><font>
        This Email id is already registered!</font></p>');

            }else{

                $un = str_replace(' ', '',$_POST['firstname'].$_POST['lastname']);
                //remove all spaces
                $unwsc  =  preg_replace('/[^A-Za-z0-9\-]/', '', $un); 
                // Removes special chars.

                $checkusername = $this->User_model->checkusername($unwsc);

                if($checkusername==true){

                    $username = $this->getUserName($unwsc);

                }else{
                    $username = $unwsc;
                }

                $data['code'] = rand(0,100000); 
                $data['username'] = $_POST['firstname'];

                $this->User_model->saveUser($username,$data['code']);

                mkdir("/var/www/html/users/".$username);

                /*
                $fromemail="_my_organisation's_email_";
                $subject = "Registration Confirmation";

                //this was the email sender that i used initially
                            //it's codeigniter's built in email library. but after a
                        //while, emails stopped being delivered.

                $this->load->library('email');  
                $config['mailtype'] = 'html'; 
                $this->email->initialize($config);
                $this->email->to($_POST['email']);
                $this->email->from($fromemail, "_my_organisation's_email_");
                $this->email->subject($subject);                    
                $body = $this->load->view('email/register.php',$data,TRUE);
                $this->email->message($body);                   
                $this->email->send();
                */                                      

                $subject = "Registration Confirmation";
                $body = $this->load->view('email/register.php',$data,TRUE);

                //using mailgun api                                 

                # Instantiate the client.
                $mgClient = new Mailgun('_my_mailgun_key_');
                $domain = "_my_maigun_domain_";

                # Make the call to the client.
                $result = $mgClient->sendMessage($domain, array(
                'from'    => 'NO REPLY <_my_organisation's_email_>',
                'to'      => 'new user <_authorized_recipient_>',
                'subject' => 'Mailgun test',
                'text'    => 'testing email sender'
                ));


                $this->session->set_flashdata('success','<p><font>Thanksfor registration!                   </font></p>');
                redirect(site_url('register/complete'));    
            }

        }else{
            $this->session->set_flashdata('error', '<p><font>
            Missing Some Fields!</font></p>');
        }
    }   
    $data['title']='';
    $data['param']=$param;
    $this->load->view('registration/index',$data);
}
function getUserName($string) {
    $result = $string;
    for ($i = 1; $i < 100; $i++) {
         $userChecking = $this->User_model->checkusername($string.$i);
         if(empty($userChecking)){
            $result = $string.$i;
            break;
         }
    }
    return $result;
}
}
?>
  • 写回答

1条回答 默认 最新

  • douchao9899 2017-08-18 09:52
    关注

    The problem was solved after updating PHP to version 5.6.30. After a long duration of talk with Godaddy's customer support, They suggested that I update my PHP. It worked perfectly after.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题