【问题标题】:CodeIgniter mail not working on server but works fine localhostCodeIgniter 邮件无法在服务器上运行,但在 localhost 上运行良好
【发布时间】:2017-08-10 04:25:44
【问题描述】:

我正在使用共享主机计划。我尽力了,但我无法解决这个问题。 这是我的代码。我首先尝试使用 gmail 但没有工作然后我在某处读到可能我的共享托管计划的 IP 被谷歌列入黑名单然后我厌倦了我自己的 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.

帮帮我,我累了

【问题讨论】:

  • 共享系统上的某些邮件服务器不允许您发送电子邮件,除非发件人地址是该域上的地址。因此,如果您的网站是 example.com,则 From 必须是 something@example.com。尝试设置它,并将发件人的电子邮件地址设置为回复。

标签: php codeigniter email


【解决方案1】:

在您发布的代码的第 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

thisCodeigniter 3 API 页面的底部是有关 print_debugger 和电子邮件库的一些信息。它应该可以帮助您找到正确的方向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多