如何通过CodeIgniter成功发送联系表单中的电子邮件?

5

我已经设置好了我的控制器和联系表单。但是当我在联系页面上点击“提交”时,我没有收到任何错误信息,也没有收到邮件。有人可以帮助我找出为什么我实际上没有收到邮件吗?我非常感激任何帮助。以下是我控制器的代码:

<?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>

1
首先,请确保您已经正确设置了SMTP服务器以正确发送电子邮件。 - Moyed Ansari
调试器输出了什么? - Paul Radich
输出正是这个: 您的信息已成功使用以下协议发送:邮件发件人:"您的姓名" 回复地址: 抄送:another@another-example.com 密送:them@their-example.com 答复地址:"dfgrfgh@gd.com" X-Sender:dfgrfgh@gd.com X-Mailer:CodeIgniter X-Priority:3(普通) 消息ID:51b0de09bea00@gd.com MIME版本:1.0 内容类型:text/plain;charset=utf-8 内容传输编码:8位 =?utf-8?Q?电子邮件测试?= 我的名字是dfdfdf,我正在测试这个电子邮件类。我的电子邮件是dfgrfgh@gd.com。 我的电话号码是This is my message dfdfdfdfdfsdafsafsdfsfdsfdsfsdf 谢谢!! - Ralph David Abernathy
5个回答

1

php.ini文件中启用php_openssl.dll,然后尝试使用gmail。Pascal Spruit或Omade的答案将起作用。在编辑php.ini文件后不要忘记重新启动本地服务器。


1
$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();

0

尝试使用 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();

0
如果有帮助的话,先尝试配置电子邮件类。
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;

$this->email->initialize($config);

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

$this->email->send();更改为

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

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


感谢您的建议,但我没有收到任何输出。当我点击“提交”时,Firefox页面出现并显示: 文档已过期 此文档不再可用。 请求的文档在Firefox缓存中不可用。 - Ralph David Abernathy
1
尝试向 Gmail 发送电子邮件。某些服务器可能会阻止测试邮件。 - Abdul Haseeb
尝试一下这个:mail('abc@example.com', '测试邮件', '这是一个测试');如果不起作用,请检查您的php.ini文件中的SMTP设置。 - Abdul Haseeb

0

我也遇到了同样的问题,但我发现一个问题,所有东西都在 Gmail 上成功进行,但只有邮件没有发送。

如果我发送$email = $this->input->post('useremail');,那么它不会在 Gmail 上发送。它会显示邮件发送成功或者你在“if”条件中传递的任何内容,但邮件不会出现在邮箱中。但是,如果我传递其他任何东西,比如姓名、电话密码或任何东西,那么一切都会成功发送。 CI 或 php 对电子邮件有任何限制吗? 因为我已经成功地在服务器上发送了 HTML 内容,但只有邮件没有发送。 还有一件事,所有东西都通过本地主机发送,但我将我的网站放在 Hostinger 上并配置了一切,一切都很顺利,但只有邮件没有通过表单发送。

如果有人知道如何在联系表单中包含电子邮件,请与我分享您的知识。


我还没有得到完整的解决方案,但如果您想检查,那么请在评论中添加以下代码:$email = $this->input->post('email'); 同时删除表单消息部分,然后检查您的邮箱,也许您会收到邮件。 - heySushil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接