【问题标题】:My Email is not working am getting error in my contact us page我的电子邮件无法使用我的联系我们页面中出现错误
【发布时间】:2017-11-24 16:05:08
【问题描述】:

我的看法

<div class="col-sm-6">
  <div class="single_contant_left padding-top-90 padding-bottom-90">
    <?php $attributes = array("class" => "form-horizontal", "name" => "contactform");
            echo form_open("contactform/contactus", $attributes);?>
    <div id="formid">
      <div class="col-lg-8 col-md-8 col-sm-10 col-lg-offset-2 col-md-offset-2 col-sm-offset-1">

        <div class="row">
          <div class="col-sm-12">
            <div class="form-group">
              <input type="text" class="form-control" name="name" value="<?php echo set_value('name'); ?>" placeholder="First Name" required>
              <span class="text-danger"><?php echo form_error('name'); ?></span>
            </div>
          </div>
        </div>


        <div class="row">
          <div class="col-sm-12">
            <div class="form-group">
              <input type="email" class="form-control" name="email" placeholder="Email" value="<?php echo set_value('email'); ?>" required>
              <span class="text-danger"><?php echo form_error('email'); ?></span>
            </div>
          </div>
          <div class="col-sm-12">
            <div class="form-group">
              <input type="text" class="form-control" name="subject" placeholder="Subject" value="<?php echo set_value('subject'); ?>" required>
              <span class="text-danger"><?php echo form_error('subject'); ?></span>
            </div>
          </div>
        </div>


        <div class="form-group">
          <textarea class="form-control" name="message" rows="7" placeholder="Message"><?php echo set_value('message'); ?></textarea>
          <span class="text-danger"><?php echo form_error('message'); ?></span>
        </div>

        <div class="">
          <input type="submit" name="submit" value="SEND MESSAGE" class="btn btn-lg">
        </div>
      </div>
      <?php echo form_close(); ?>
      <?php echo $this->session->flashdata('msg'); ?>
    </div>
  </div>

</div>

我的控制器

<?php




 class Contactform extends CI_Controller
     {
         public function __construct()
         {
           parent::__construct();
         $this->load->helper(array('form','url'));
         $this->load->library(array('session', 'form_validation', 'email'));
}

function contactus()
{
    //set validation rules
    $this->form_validation->set_rules('name', 'Name', 'required');
    $this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
    $this->form_validation->set_rules('subject', 'Subject', 'trim|required');
    $this->form_validation->set_rules('message', 'Message', 'trim|required');

    //run validation on form input
    if ($this->form_validation->run() == FALSE)
    {
        //validation fails
        $this->load->view('header_vw');
        $this->load->view('center_vw');
        $this->load->view('footer_vw');
    }
    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 = 'alshoja@gmail.com';

        //configure email settings
        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'ssl://smtp.gmail.com';
        $config['smtp_port'] = '465';
        $config['smtp_user'] = '....@gmail.com';
        $config['smtp_pass'] = 'als....@......';
        $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->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('contactform/contactus');
        }
        else
        {
            //error
            $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">There is error in sending mail! Please try again later</div>');
            redirect('contactform/contactus');
        }
    }
}

//custom validation function to accept only alphabets and space input
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;
    }
}

}

发送消息时出现错误,我的服务器配置良好,但邮件没有从我的站点发送,我该如何配置我想在配置部分提供用于发送邮件的密码和用户名,否则如何配置请帮忙:( 我对他们的地址和电子邮件配置中的地址感到困惑:(

【问题讨论】:

  • 您在发送消息时遇到了哪些错误?你能与我们分享这个错误吗?

标签: php html codeigniter email


【解决方案1】:

虽然我已经多次这样做了,但通过其他提供商发送电子邮件始终是一项痛苦的任务。

虽然这可能没问题,

$config['newline'] = "\r\n"; //use double quotes

但是,Sending email with gmail smtp with codeigniter email library 建议

$this->email->set_newline("\r\n");

您还确定您在 gmail 中允许安全性较低的应用程序吗?

【讨论】:

  • 我想在 $config['smtp_user'] = '....@gmail.com' 中提供我的用户名和密码吗? $config['smtp_pass'] = 'als....@......';
  • 是的,你必须,只是不要在这里粘贴:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-03
相关资源
最近更新 更多