codeigniter 无法从live服务器中的codeignitor项目发送电子邮件,但在localhost中工作

kdfy810k  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(79)

我得到的错误
严重性:警告消息:fsockopen():无法连接到ssl://smtp.gmail.com:465(连接被拒绝)
文件名:libraries/Email.php
这是我的控制器功能:

public function contactload(){
        // to load the register.php     
        // here the data is stored to database
        $this->load->model('User_model');
        //storing the data into the array
        $formArray =array();
        $formArray['name']= $this->input->post('name');
        $formArray['email']= $this->input->post('email');
        $formArray['country']= $this->input->post('country');
        $formArray['subject']= $this->input->post('subject');
    
        // calling the create function in user_model
        $check=$this->User_model->create($formArray);
        if($check==true){
            if ($this->User_model->sendEmail($this->input->post('email'), $this->input->post('name')))
        {
            // successfully sent mail
            echo '<script>alert("Mail Is sent kindly check the Mail")</script>';
        } else {
            // error
            echo '<script>alert("UNABLE TO SEND THE MAIL")</script>';
        }
    }else{
        echo "error !";
    }

}

模型:

public function sendEmail($to_email,$name) { 
    $from_email = '*********@gmail.com'; //change this to yours 
    $subject = 'Your meaasge Has Sucessfully reached Us'; 
    $message = ' <html> <head> <title>Conformation Mail</title> </head> <body>
    
    $this->load->library('email'); 
    $config = Array(
            'protocol'  => 'smtp',
            'smtp_host' => 'ssl://smtp.gmail.com',
            'smtp_port' => '465',
            'smtp_user' => '***************@gmail.com',
            'smtp_pass' => '****************',
            'mailtype'  => 'html',
            'smtp_timeout' => '60',
            'charset'   => 'iso-8859-1',
            'wordwrap'  => TRUE,
            'newline'   => "\r\n"
    );
    $this->load->library('email',$config);
    $this->email->initialize($config);
   
    
    //send mail
    $this->email->from($from_email);
    $this->email->to($to_email);
    $this->email->subject($subject);
    $this->email->message($message);
    return $this->email->send();
    echo $this->email->print_debugger();
}

邮件发送操作在本地主机上工作,但在活动服务器上不工作
这是一个代码点火器项目

ufj5ltwl

ufj5ltwl1#

您需要使用$_SERVER ['REMOTE_ADDR']变量来获取服务器的地址。
在Controller中,您必须执行以下操作:

public function SendEmail()
{
    $email = $this->input->post('email');

    $message = '
                <html>
                    <head>
                        <title>Conformation Mail</title>
                    </head>
                    <body>
                        <h1>Hello </h1>
                        <h3>New User</h3>
                        <p>tO cHECK THE EMAIL SENT  </p>
                        <p>Thank Your.</p>
                    </body>
                </html>';
    //Sending email from localhost or live server
    $localhosts = array(
        '::1',
        '127.0.0.1',
        'localhost'
    );
        
    $protocol = 'mail';
    if (in_array($_SERVER['REMOTE_ADDR'], $localhosts)) {
        $protocol = 'smtp';
    }

    $config = array(
        'protocol' => $protocol,
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => '**************@gmail.com',
        'smtp_pass' => '***************',
        'mailtype' => 'html',
        'starttls'  => true,
        'newline'   => "\r\n",
    );

    $this->load->library('email');
    $this->email->initialize($config);
    $this->email->from("************@gmail.com");
    $this->email->to($email);
    $this->email->subject("New user contacts");
    $this->email->message($message);
    $flag = $this->email->send();

    if($flag){
        echo "Email sent";
        echo '<script>alert("email sent")</script>';
        $this->load->view('main');
    }else{
        echo "Email sending failed";
        echo '<script>alert("mail not sent")</script>';
        $this->load->view('main');
    }
}

现在使用控制器功能发送电子邮件。

相关问题