在我的联系我们页面中,我的电子邮件无法正常运行

时间:2017-11-24 16:05:08

标签: php html codeigniter email

我的观点

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

}

发送邮件时收到错误我的服务器配置正常但邮件没有从我的网站发送,如何配置我是否要在配置部分提供我的密码和用户名来发送邮件,否则如何配置请帮助:( 我们与他们混淆地址和电子邮件配置中的地址:(

1 个答案:

答案 0 :(得分:0)

虽然我已多次这样做,但通过其他提供商发送电子邮件总是一项艰巨的任务。

虽然这可能没问题,

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

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

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

你还确定你在gmail中允许不太安全的应用程序吗?