【问题标题】:Send email in php using codeigniter使用 codeigniter 在 php 中发送电子邮件
【发布时间】:2017-07-26 11:18:24
【问题描述】:

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

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

【问题讨论】:

  • 错误是什么?你想达到什么目标?
  • @ArunpandianM 没有错误
  • 是的,我发现您希望收到每个填写的联系表格的详细信息。检查my answer它可以简单地用html表单完成
  • if ($this-&gt;email-&gt;send()) 子句之前使用 echo $this-&gt;email-&gt;print_debugger();die(); 并查看它的输出...
  • 使用表单辅助函数&lt;?php echo form_open('controllername/sendmail');?&gt;

标签: codeigniter email


【解决方案1】:

你需要使用:

 $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'
);

【讨论】:

  • 您可以使用任何 gmail 帐户,但请确保您使用的是正确的帐户详细信息。如果有效并被接受,请不要忘记添加投票
【解决方案2】:

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

第二步)加载邮件库并调用初始化

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

记住 yourremailid@gmail.com 是发件人,而 *****@gmail.com 是收件人。

【讨论】:

  • 所以我不必将“youremail@domain_registred.com”替换为复制过去的任何内容?
  • 我应该在'smtp_pass'中写什么??
  • 我已经编辑了我的答案。请检查,如果它有助于解决问题,请将其标记为答案。
  • 查看编辑,smtp_pass 是发件人邮箱的密码
  • 在实现您的代码后,它给了我这个错误 An Error Was Encountered 220 smtp.gmail.com ESMTP 124sm34854421pgj.57 - gsmtp 无法验证密码。错误:534-5.7.14 请通过您的网络浏览器和 534-5.7.14 登录,然后重试。 534-5.7.14 了解更多信息,请致电 534 5.7.14 support.google.com/mail/answer/78754 124sm34854421pgj.57 - gsmtp 无法使用 PHP SMTP 发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。
猜你喜欢
  • 2017-12-07
  • 1970-01-01
  • 2013-05-06
  • 2011-04-21
  • 2014-07-13
  • 1970-01-01
相关资源
最近更新 更多