2017-06-27 21 views
1

私はcodeigniterのお問い合わせフォームで電子メールを送信しようとしていますが、電子メールは送信しません。 はい、私はこの質問が以前に尋ねられたことを知っていますが、間違ったコードがないかどうかを知りたいだけです。Codeigniter電子メール送信が機能していません

これは私のコントローラファイル(Contactform.php)です:

<?php error_reporting(-1); 
ini_set('display_errors', 'On'); 
set_error_handler("var_dump"); 

error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
?> 


<?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 index() 
    { 
     //set validation rules 
     //$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only'); 
     $this->form_validation->set_rules('email', 'Email 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('contact_form_view'); 
     } 
     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'); 

      //zet to_email naar welk email je het contact form naar wilt laten sturen 
      $to_email = '[email protected]'; 

      //configure email settings 
      $config['protocol'] = 'smtp'; 
      $config['smtp_host'] = 'ssl://smtp.gmail.com'; 
      $config['smtp_port'] = '465'; 
      $config['smtp_user'] = '[email protected]ail.com'; 
      $config['smtp_pass'] = 'tom10'; 
      $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/index'); 
      } 
      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/index'); 
      } 
     } 
    } 

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

そして、これが私の見解フォームファイル(contact_form_view.php)である:これは私の中に私のemail.phpある

<div class="container"> 
    <div class="row"> 
     <div class="col-md-6 col-md-offset-3 well"> 
      <?php $attributes = array("class" => "form-horizontal", "name" => "Contactform"); 
      echo form_open("Contactform/index", $attributes);?> 
      <fieldset> 
      <legend>Contact Form</legend> 
      <div class="form-group"> 
       <div class="col-md-12"> 
        <label for="name" class="control-label">Name</label> 
       </div> 
       <div class="col-md-12"> 
        <input class="form-control" name="name" placeholder="Your Full Name" type="text" value="<?php echo set_value('name'); ?>" /> 
        <span class="text-danger"><?php echo form_error('name'); ?></span> 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-12"> 
        <label for="email" class="control-label">Email ID</label> 
       </div> 
       <div class="col-md-12"> 
        <input class="form-control" name="email" placeholder="Your Email ID" type="text" value="<?php echo set_value('email'); ?>" /> 
        <span class="text-danger"><?php echo form_error('email'); ?></span> 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-12"> 
        <label for="subject" class="control-label">Subject</label> 
       </div> 
       <div class="col-md-12"> 
        <input class="form-control" name="subject" placeholder="Your Subject" type="text" value="<?php echo set_value('subject'); ?>" /> 
        <span class="text-danger"><?php echo form_error('subject'); ?></span> 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-12"> 
        <label for="message" class="control-label">Message</label> 
       </div> 
       <div class="col-md-12"> 
        <textarea class="form-control" name="message" rows="4" placeholder="Your Message"><?php echo set_value('message'); ?></textarea> 
        <span class="text-danger"><?php echo form_error('message'); ?></span> 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-12"> 
        <input name="submit" type="submit" class="btn btn-primary" value="Send" /> 
       </div> 
      </div> 
      </fieldset> 
      <?php echo form_close(); ?> 
      <?php echo $this->session->flashdata('msg'); ?> 
     </div> 
    </div> 
</div> 

configフォルダ:(コントローラに同じコードがあるので、このファイルが必要かどうかわかりません)

<?php 
    $config['protocol'] = 'smtp'; 
    $config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this 
    $config['smtp_port'] = '465'; 
    $config['smtp_user'] = '[email protected]'; 
    $config['smtp_pass'] = 'xxxxxxxx'; 
    $config['mailtype'] = 'html'; 
    $config['charset'] = 'iso-8859-1'; 
    $config['wordwrap'] = TRUE; 
    $config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard 

?> 

答えて

0

あなたが追加するのはです

$autoload['libraries'] = array('email'); 

$autoload['config'] = array('email'); 

autoload.phpで、その後自動ロードの設定とライブラリ

<?php 
    $config['protocol'] = 'smtp'; 
    $config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this 
    $config['smtp_port'] = '465'; 
    $config['smtp_user'] = '[email protected]'; 
    $config['smtp_pass'] = 'xxxxxxxx'; 
    $config['mailtype'] = 'html'; 
    $config['charset'] = 'iso-8859-1'; 
    $config['wordwrap'] = TRUE; 
    $config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard 

?> 

configフォルダ内、あなたがこの

 $to_email = '[email protected]'; 

     $message = "email message"; 

     $this->email->from('[email protected]','Admin'); 
     $this->email->to($to_email); 
     $this->email->subject($subject); 
     $this->email->message($message); 
     if ($this->email->send() == TRUE) 
     { 
      log_message('debug', 'Email Send SuccessFully>>>>>>>>>>>'); 
     } 
     else 
     { 
      log_message('debug', 'Email Not Send>>>>>>>>>>>>>>>'); 
     } 
+0

を使用してメールを送信するので、私は電子メールの設定を構成する必要はありません私のコントローラファイルには? –

+0

はいコントローラでは必要ありません –

+0

Googleアカウントの設定で安全性の低いアプリを有効にする –

関連する問題