2017-07-26 25 views
0

好吧,所以我在这里寻找一些帮助。我有一个联系我们的网站形式。它有三个字段名称,电子邮件和消息。我需要做的是当用户填写表格时,电子邮件应该发送到管理员电子邮件,其中显示用户及其电子邮件和名称的消息。我有一个工作正常的代码,它给了我发送电子邮件的消息,但是当我打开邮件时没有电子邮件。好的,我需要你的帮助。下面是代码使用codeigniter在php中发送电子邮件

<h3 class="title-big wow fadeIn">Contact Us</h3> 
    <br> 
</div> 
<div class="col-md-12 text-center"> 
<?php if($success != ""): ?> 
<span class="col-md-4 col-md-offset-4" style="color:#D91E18"> <?php echo $success;?></span> 
<?php endif; ?> 
<div class="form-group col-md-offset-4 col-md-4 col-md-offset-4 wow fadeInDown"> 
<form name="myform" role="form" action="sendmail" method="post"> 
<div class="form-group"> 
<?php echo form_input($first_name);?> 
</div> 


<div class="form-group"> 
<?php echo form_input($email);?> 
</div> 


<div class="form-group"> 
<?php echo form_textarea($message1);?> 
<br> 
<a class="sign-up-button btn btn-border btn-lg col-md-offset-3 col-md-6 col-md-offset-3 wow fadeInUp" data-wow-delay="1s" href="javascript: submitform()">Submit Query</a> 
</div> 
</form> 

Controller.php这样

public function contact() 
{ 
    $data['success'] = (validation_errors() ? validation_errors() : ($this->session->flashdata('message'))); 
    //$data['message'] = (validation_errors() ? validation_errors() : ($this->session->flashdata('message'))); 
    $data['message1'] = array(
      'name' => 'message1', 
      'id' => 'message1', 
      'class' => 'form-control', 
      'placeholder' => "Enter message here...", 
      'size'  => 32, 
      'maxlength' => 500, 
      'rows' => 5, 
      'cols' => 41, 

     ); 
     $data['first_name'] = array(
      'name' => 'first_name', 
      'id' => 'first_name', 
      'type' => 'text', 
      'class' => 'form-control', 
      'placeholder' => "First Name...", 
      'size'  => 32, 
      'maxlength' => 50, 

     ); 
     $data['email'] = array(
      'name' => 'email', 
      'id' => 'email', 
      'type' => 'text', 
      'class' => 'form-control', 
      'placeholder' => "Email Address (Mandatory)", 
      'size'  => 32, 
      'maxlength' => 128,     

     ); 

    $this->load->view('contact',$data); 
} 

public function sendmail() 
{ 
    $this->form_validation->set_rules('first_name', 'Name', 'required'); 
    //$this->form_validation->set_rules('last_name', 'Name', 'required'); 
    $this->form_validation->set_rules('message1', 'Message', 'required'); 
    $this->form_validation->set_rules('email', 'Email Address', 'required'); 
    //$this->form_validation->set_rules('email_confirm', 'Reenter Email Address', 'required'); 
    if ($this->form_validation->run() == true) 
    { 
     $data = array(
     'first_name' => $this->input->post('first_name'), 
     //'last_name' => $this->input->post('last_name'), 
     'message1' => $this->input->post('message1'), 
     'email' => $this->input->post('email'), 
     ); 

    //if ($this->form_validation->run() == true) 
    //{ 


       $config = array(
      'protocol' => 'smtp', 
      'smtp_host' => 'ssl://smtp.gmail.com', 
      'smtp_port' => 465, 
      'auth' => true, 
      'smtp_user' => '********@gmail.com', 
      'smtp_pass' => '********' 
     ); 
     $emailsubject = $data['first_name']." ".$data['last_name']." has sent a Query message."; 

     $this->load->library('email',$config); 
     $this->email->set_newline("\r\n"); 
     // $this->email->initialize($config); 

     $this->email->from('********@gmail.com', 'AOTS Lahore Regional Center'); 
     //$this->email->to('********@gmail.com'); 
     $this->email->to('********@gmail.com'); 
     $this->email->cc('********@gmail.com'); 
     $this->email->subject($emailsubject); 
     $this->email->message($data['message1']."\nEmail ID: ".$data['email']); 

     if ($this->email->send()) 
     { 
      $data['success'] = "Your Query has been sent successfully... !!"; 
      //$data['message'] = (validation_errors() ? validation_errors() : ($this->session->flashdata('message'))); 
    $data['message1'] = array(
      'name' => 'message1', 
      'id' => 'message1', 
      'class' => 'form-control', 
      'placeholder' => "Enter message here...", 
      'size'  => 32, 
      'maxlength' => 500, 
      'rows' => 5, 
      'cols' => 41, 

     ); 
     $data['first_name'] = array(
      'name' => 'first_name', 
      'id' => 'first_name', 
      'type' => 'text', 
      'class' => 'form-control', 
      'placeholder' => "First Name...", 
      'size'  => 32, 
      'maxlength' => 50, 

     ); 

     $data['email'] = array(
      'name' => 'email', 
      'id' => 'email', 
      'type' => 'text', 
      'class' => 'form-control', 
      'placeholder' => "Email Address (Mandatory)", 
      'size'  => 32, 
      'maxlength' => 128,     

     ); 

     $this->load->view('contact',$data); 
     } 
     else 
     { 
      $data['success'] = show_error($this->email->print_debugger()); 
     } 

    } 
    //} 
    else 
    { 

     $data['success'] = (validation_errors() ? validation_errors() : ($this->session->flashdata('message'))); 



    $data['message1'] = array(
      'name' => 'message1', 
      'id' => 'message1', 
      'class' => 'form-control', 
      'placeholder' => "Enter message here...", 
      'size'  => 32, 
      'maxlength' => 500, 
      'rows' => 5, 
      'cols' => 41, 

     ); 
     $data['first_name'] = array(
      'name' => 'first_name', 
      'id' => 'first_name', 
      'type' => 'text', 
      'class' => 'form-control', 
      'placeholder' => "First Name...", 
      'size'  => 32, 
      'maxlength' => 50, 

     ); 
     $data['email'] = array(
      'name' => 'email', 
      'id' => 'email', 
      'type' => 'text', 
      'class' => 'form-control', 
      'placeholder' => "Email Address (Mandatory)", 
      'size'  => 32, 
      'maxlength' => 128,     

     ); 
    $this->load->view('contact',$data); 
    } 
} 

********@gmail.com是电子邮件,其中邮件应发送

+0

是什么错误?你想达到什么目的? –

+0

@ArunpandianM没有错误 –

+0

是的,我发现你想收到每个填充的联系表单的详细信息。在if($ this-> email-> send())'子句之前使用'echo $ this-> email-> print_debugger(); die();'检查'我的回答'它可以简单地使用html表单来完成 –

回答

0

为了发从(比方说)[email protected]到电子邮件ID *****@gmail.com电子邮件按照下列步骤进行:

步骤1)的配置阵列如下面

$config = array(
    'protocol' => 'smtp', 
    'smtp_host' => 'ssl://smtp.gmail.com', 
    'smtp_port' => 465, 
    'auth' => true, 
    'smtp_user' => 'youremailid*****@gmail.com', // email id of sender in this case '[email protected]' is the sender 
    'smtp_pass' => 'passwordofyouremailid', // password of sender gmail id ie. password of '[email protected]' 
    'newline' => "\r\n", 
); 

步骤2)加载邮件库,并调用初始化

$this->load->library('email',$config); 
$this->email->initialize($config); 

步骤3)设置科目等信息到变量后执行以下操作

$this->email->from('youremailid****@gmail.com', 'Sender Name'); // sender emailid and name in this case `[email protected]` is sending email 

$this->email->to('*****@gmail.com'); // receiver email id in this case '*****@gmail.com' is to whome you want to send . so *****@gmail.com is the email id 

$this->email->cc('*****@gmail.com'); // same as above if you want cc to include 

$this->email->subject($emailsubject); // subject of email 


if ($this->email->send()) 
{ 

    // rest of your code 
} 

remember there [email protected] is the sender and *****@gmail.com is the receiver.

+0

所以我不需要用任何东西替换'[email protected]_registred.com'只是复制过去它? –

+0

我应该在'smtp_pass'中写什么? –

+0

我编辑了我的答案。请检查,如果它有助于解决问题,请将其标记为答案。 – 2017-07-26 20:11:55

0

您需要用途:

$config = Array(
    'protocol' => 'smtp', 
    'smtp_host' => 'ssl://smtp.googlemail.com', 
    'smtp_port' => 465, 
    'smtp_user' => '********@gmail.com', 
    'smtp_pass' => '********', 
    'mailtype' => 'html', 
    'charset' => 'iso-8859-1' 
); 
+0

您可以使用任何Gmail帐户,但确保您使用的是您的帐户的正确详细信息。不要忘记添加投票,如果作品,它接受 – Madhur

+0

让我试试我会投它,如果它的工作 –