簡體   English   中英

CodeIgniter郵件無法在服務器上運行,但可以在本地主機上正常工作

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

我正在使用共享主機計划。 我盡了最大努力,但無法解決此問題。 這是我的代碼。 我先嘗試使用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;
        }
    }
}

我正在得到這個輸出

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

幫我,我累了

在您發布的代碼的第73行,它看起來像是if語句

$this->email->send()

失敗,然后運行此命令:

  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');
  }

我會檢查Email.php中的配置-在我的Codeigniter 3項目中,它位於此處:

html/system/libraries/Email.php

Codeigniter 3 API頁面的底部,有一些有關print_debugger和電子郵件庫的信息。 它應該可以幫助您朝正確的方向前進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM