使用codeigniter在php中发送电子邮件

时间:2017-07-26 11:18:25

标签: codeigniter email

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

    <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是发送邮件的电子邮件

2 个答案:

答案 0 :(得分:0)

要从(例如)youremailid@gmail.com向电子邮件地址*****@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 'youremailid@gmail.com' is the sender 
  'smtp_pass' => 'passwordofyouremailid',   // password of sender gmail id ie. password of 'youremailid@gmail.com'
  'newline' => "\r\n",
);

步骤2)加载电子邮件库并调用initialize

$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  `youremailid@gmail.com` 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
}
  

记得youremailid@gmail.com是发件人,***** @ gmail.com是收件人。

答案 1 :(得分: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'
);