简体   繁体   English

CodeIgniter邮件无法在服务器上运行,但可以在本地主机上正常工作

[英]CodeIgniter mail not working on server but works fine localhost

i am using Shared hosting plan. 我正在使用共享主机计划。 I tried my level best but i am not able to resolve this issue. 我尽了最大努力,但无法解决此问题。 here is my code. 这是我的代码。 i tried first with gmail but was not working then i read somewhere that may the IP of my shared hosting plan is blacklisted by google then i tired my own smtp then imap server, same results it works fine on localhost but again i am getting the same error. 我先尝试使用gmail,但无法正常工作,然后我读到某个地方,可能我的共享主机计划的IP被Google列入了黑名单,然后我累了我自己的smtp然后是imap服务器,同样的结果在localhost上也能正常工作,但是我又得到了相同的结果错误。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contact extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -  
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function __construct()
    {
        parent::__construct();   
        $this->load->helper(array('form','url'));
        $this->load->library(array('session', 'form_validation', 'email'));
    }   
    public function index()
    {
        $this->load->helper('security');
        //set validation rules
        $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only');
        $this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
        $this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
        $this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');

        //run validation on form input
        if ($this->form_validation->run() == FALSE)
        {
            //validation fails
        $this->load->view('head');
        $this->load->view('map');
        $this->load->view('footer_map');
        }

        else
        {
            //get the form data
            $name = $this->input->post('name');
            $from_email = $this->input->post('email');
            $subject = $this->input->post('subject');
            $message = $this->input->post('message');

            //set to_email id to which you want to receive mails
            $to_email = 'example@gmail.com';

            //configure email settings
            $config['protocol'] = 'imap';
            $config['smtp_host'] = 'imap.example.com';
            $config['smtp_port'] = '587';
            $config['smtp_user'] = 'info@exampl.com';
            $config['smtp_pass'] = 'example';
            $config['mailtype'] = 'html';
            $config['charset'] = 'iso-8859-1';
            $config['wordwrap'] = TRUE;
            $config['newline'] = "\r\n"; //use double quotes
           // $this->load->library('email', $config);


            $this->email->initialize($config); 
            //send mail
            $this->email->from($from_email, $name);
            $this->email->to($to_email);
            $this->email->reply_to($from_email, $name);
            $this->email->subject($subject);
            $this->email->message($message);


            if ($this->email->send())
            {
                // mail sent
                $this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
                redirect('contact/index');
            }
            else
            {
                //error
                echo $this->email->print_debugger();
                $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">There is error in sending mail! Please try again later</div>');
                redirect('contact/index');
            }
        }

    }
    public function alpha_space_only($str)
    {
        if (!preg_match("/^[a-zA-Z ]+$/",$str))
        {
            $this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}

i am getting this output 我正在得到这个输出

There is error in sending mail! Please try again later.

Help me guys, im tired 帮我,我累了

On line 73 of the code you posted it looks like the if statement for 在您发布的代码的第73行,它看起来像是if语句

$this->email->send()

is failing, which then runs this: 失败,然后运行此命令:

  else
  {
        //error
        echo $this->email->print_debugger();
        $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">There is error in sending mail! Please try again later</div>');
        redirect('contact/index');
  }

I would check the configuration in Email.php - in my Codeigniter 3 project it is located here: 我会检查Email.php中的配置-在我的Codeigniter 3项目中,它位于此处:

html/system/libraries/Email.php

At the bottom of this Codeigniter 3 API page is some info about print_debugger and about the Email library. Codeigniter 3 API页面的底部,有一些有关print_debugger和电子邮件库的信息。 It should help you get in the right direction. 它应该可以帮助您朝正确的方向前进。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SMTP邮件无法在服务器上正常工作在本地主机上 - SMTP Mail Not Working ON Server Working Fine On Localhost 在服务器中通过gmail smtp发送邮件时出现问题,但在localhost中工作正常 - problem in sending mail by gmail smtp in server but works fine in localhost 为什么 CodeIgniter 中的重定向功能在实时服务器上不起作用,但在 localhost 上运行良好 - Why in CodeIgniter redirect function is not working on live server, but working fine on localhost CodeIgniter 显示在服务器上找不到 404,但在 localhost (xampp) 中工作正常 - CodeIgniter shows 404 not found on the server but working fine in localhost (xampp) CakeSession :: write()在远程服务器上不起作用,但在localhost上工作正常 - CakeSession::write() Not working on remote server, but works fine on localhost 宁静的PHP客户端无法在服务器上运行,但在localhost上可以正常工作 - Restful php client not working on server but works fine in localhost php 代码在 xampp 本地主机上运行良好,但在实时服务器上运行不正常 - php code works fine on xampp localhost but not working on live server CodeIgniter在本地主机上工作正常,但是当我传递到服务器时,只有索引页有效 - CodeIgniter works fine in localhost but when I passed to server only the index page works Codeigniter 3.0可在localhost上运行,但不能在服务器上运行 - Codeigniter 3.0 works on localhost but not server Phpmailer 在 localhost 中工作正常但不在服务器中 - Phpmailer working fine in localhost But not in server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM