简体   繁体   中英

My Email is not working am getting error in my contact us page

My view

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

My controller

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

}

Am getting error while sending message my server is configured good but mail is not sending from my site, how can I configure shall I want to give my password and username for sending mail in configuring section or else how can configure that please help :( Am confused with them to address and from address in email configuration :(

Though I have done this several times, sending email through another provider is always a painful task.

Though this maybe ok,

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

but, Sending email with gmail smtp with codeigniter email library suggests

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

Also are you sure you allowed less secure apps in gmail?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM