【问题标题】:How to successfully send an email in a contact form through CodeIgniter?如何通过 CodeIgniter 在联系表单中成功发送电子邮件?
【发布时间】:2013-06-02 21:40:25
【问题描述】:

我已经设置了我的控制器和我的联系表格。但是当我在我的联系页面上单击“提交”时,我没有收到任何错误,但我没有收到电子邮件。谁能帮我弄清楚为什么我实际上没有收到电子邮件?我很感激任何帮助。这是我的控制器代码:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
    public function index()
    {
        $this->template->build('home');
    }

    public function email() {
        $name = $this->input->post('name');
        $email = $this->input->post('email');
        $phone_number = $this->input->post('phone_number');
        $message = $this->input->post('message');
        $this->load->library('email');
        $this->email->from($email, 'Your Name');
        $this->email->to('anish@develop.io');
        $this->email->cc('another@another-example.com');
        $this->email->bcc('them@their-example.com');
        $this->email->subject('Email Test');
        $this->email->message(
          'My name is'.$name.', Im testing this email class. My email is '.$email. '. My Phone number is '.$phone_number.
         ' This is my message '.$message. ' Thanks!!'
         );
        $this->email->send();
        echo $this->email->print_debugger();
        $this->template->build('home');
    }
}

这是我的联系人页面视图代码:

<div class="inner-content" id="contact-content">

  <title>CSS3 Contact Form</title>
  <div id="contact">
    <h1>Send an email</h1>
    <form method="post" action="/home/email">
      <fieldset>
        <label for="name">Name:</label>
        <input name="name" id="name" type="text" placeholder="Enter your full name">
        <label for="email">Email:</label>
        <input name="email" id="email" type="email" placeholder="Enter your email address">
        <label for="message">Message:</label>
        <textarea name="message" id="message" placeholder="Type your message here..."></textarea>
        <input type="submit" value="Send message">
      </fieldset>
    </form>
  </div>
</div>

【问题讨论】:

  • 首先请确保您已设置 smtp 服务器以正确发送电子邮件。
  • 调试器回显了什么?
  • 输出是这样的:您的消息已使用以下协议成功发送:mail From: "Your Name" Return-Path: Cc: another@another-example.com Bcc: them@their -example.com 回复:“dfgrfgh@gd.com” X-Sender:dfgrfgh@gd.com X-Mailer:CodeIgniter X-Priority:3(正常) Message-ID: Mime-版本:1.0 内容类型:文本/纯文本; charset=utf-8 Content-Transfer-Encoding: 8bit =?utf-8?Q?Email_Test?= 我的名字是dfdfdf,我正在测试这个电子邮件类。我的电子邮件是 dfgrfgh@gd.com。我的电话号码是这是我的消息 dfdfdfdfdfsdafsafsdfsfdsfdsfsdf 谢谢!!

标签: php codeigniter email contact-form


【解决方案1】:

php.ini 文件中启用php_openssl.dll 并尝试使用gmail。 Pascal Spruit 或 Omade 的答案会奏效。编辑php.ini文件后别忘了重启本地服务器。

【讨论】:

    【解决方案2】:
    $config = Array(
            'protocol'  => 'smtp',
            'smtp_host' => 'ssl://smtp.googlemail.com',
            'smtp_port' => '465',
            'smtp_user' => 'yourname@gmail.com',
            'smtp_pass' => 'yourpassword',
            'mailtype'  => 'html',
            'starttls'  => true,
            'newline'   => "\r\n"
        );
    
    $this->load->library('email', $config);
    $this->email->from('youremail@gmail.com', 'Jodie');
    $this->email->to('toemail@gmail.com');
    $this->email->subject('testing email');
    $this->email->message('This shows the email worked');
    $this->email->send();
    echo $this->email->print_debugger();
    

    【讨论】:

      【解决方案3】:

      如果有帮助,请先尝试配置电子邮件类

      $config['protocol'] = 'sendmail';
      $config['mailpath'] = '/usr/sbin/sendmail';
      $config['charset'] = 'iso-8859-1';
      $config['wordwrap'] = TRUE;
      
      $this->email->initialize($config);
      

      尝试在 email->send() 函数周围添加此代码以查看响应是什么

      $this-&gt;email-&gt;send();更改为

      if ( ! $this->email->send())
      {
          echo 'Some error in sending email';
      }
      

      如果错误字符串没有回显,则问题不在于 CI,可能与邮件服务器有关。

      【讨论】:

      • 感谢您的建议,但我没有收到任何输出。当我单击“提交”时,Firefox 页面出现说明:文档已过期此文档不再可用。请求的文档在 Firefox 的缓存中不可用。
      • 尝试向 gmail 发送电子邮件。某些服务器可能会阻止测试电子邮件。
      • 试试这个邮件('abc@example.com', 'test email', 'this is a test');如果这不起作用,请检查 php.ini 文件中的 SMTP 设置
      【解决方案4】:

      尝试使用 gmail

      $config = Array(
              'protocol'  => 'smtp',
              'smtp_host' => 'ssl://smtp.googlemail.com',
              'smtp_port' => '465',
              'smtp_user' => 'someuser@gmail.com',
              'smtp_pass' => 'password',
              'mailtype'  => 'html',
              'starttls'  => true,
              'newline'   => "\r\n"
          );
      
      $this->load->library('email', config);
      $this->email->from('email@gmail.com', 'George');
      $this->email->to('email@gmail.com');
      $this->email->subject('hey this is an email');
      $this->email->message('this is the content of the email');
      $this->email->send();
      echo $this->email->print_debugger();
      

      【讨论】:

        【解决方案5】:

        我也遇到了同样的问题,但我发现每件事都在 gmail 上成功进行,但只有电子邮件没有成功。

        如果我发送 $email = $this-&gt;input-&gt;post('useremail');那不是 gmail。它显示邮件发送成功或您在“如果”条件下传递了什么,但邮件没有进入邮箱。但是,如果我通过其他任何东西,例如姓名电话密码或任何东西,那一切都会成功。 电子邮件的 CI 或 php 是否有任何限制。 因为我在服务器上成功发送了 html 内容,但只有电子邮件没有发送。 还有一件事,包括电子邮件,但通过本地主机发送,但我在托管服务器上打开了我的网站,并配置了一切,一切都很好,但只是电子邮件没有通过表单,

        如果有人对此有任何想法 - 如何在联系表中包含电子邮件,请与我分享您的知识。

        【讨论】:

        • 我还没有得到完整的解决方案,但如果你想检查然后评论你的 $email = $this->input->post('email');并删除表单消息部分,然后检查,我是你的邮箱收到邮件。
        猜你喜欢
        • 2014-08-21
        • 1970-01-01
        • 2020-03-23
        • 2021-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-21
        相关资源
        最近更新 更多