【问题标题】:Contact us email function in codeigniter is not workingCodeigniter 中的联系我们电子邮件功能不起作用
【发布时间】:2017-12-03 03:14:12
【问题描述】:

我在 codeigniter 中制作了一个联系表格,以便用户可以联系我,但是当我尝试发送电子邮件时,没有发送任何内容。我不知道为什么会这样。我也知道以前有人问过这个问题,但我只想知道我的代码是否有问题。

这是我的控制器:

    <?php
class Contactform extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form','url'));
        $this->load->library(array('session', 'form_validation', 'email'));
    }

    function index()
    {
      //set validation rules
      //$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only');
        //$this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email');
       //$this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
       //$this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');

        //run validation on form input
        if ($this->form_validation->run() == FALSE)
        {
            //validation fails
            $this->load->view('contact_form_view');
        }
        else
        {
            //get the form data
            $name = $this->input->post('name');
            $from_email = $this->input->post('email');
            $subject = $this->input->post('subject');
            $message = $this->input->post('message');

            //naar welk email je het wilt sturen
            $to_email = 'ferran1004@gmail.com';



            //send mail
            $this->load->library('email', $config);
            $this->email->from($from_email, $name);
            $this->email->to($to_email);
            $this->email->subject($subject);
            $this->email->message($message);
             if ($this->email->send() == TRUE)
            {
                // email sent
                $this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
                redirect('contactform/index');
            }
            else
            {
                //error
                $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">There is error in sending mail! Please try again later</div>');
                redirect('contactform/index');
            }
        }
    }

    //Alleen alfabet letters en spaties code
    function alpha_space_only($str)
   {
        if (!preg_match("/^[a-zA-Z ]+$/",$str))
        {
            $this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
            return FALSE;
        }
        else
        {
           return TRUE;
        }
    } 
}
?>

这是我的看法:

<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3 well">
            <?php $attributes = array("class" => "form-horizontal", "name" => "Contactform");
            echo form_open("Contactform/index", $attributes);?>
            <fieldset>
            <legend>Contact Form</legend>
            <div class="form-group">
                <div class="col-md-12">
                    <label for="name" class="control-label">Name</label>
                </div>
                <div class="col-md-12">
                    <input class="form-control" name="name" placeholder="Your Full Name" type="text" value="<?php echo set_value('name'); ?>" />
                    <span class="text-danger"><?php echo form_error('name'); ?></span>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-12">
                    <label for="email" class="control-label">Email ID</label>
                </div>
                <div class="col-md-12">
                    <input class="form-control" name="email" placeholder="Your Email ID" type="text" value="<?php echo set_value('email'); ?>" />
                    <span class="text-danger"><?php echo form_error('email'); ?></span>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-12">
                    <label for="subject" class="control-label">Subject</label>
                </div>
                <div class="col-md-12">
                    <input class="form-control" name="subject" placeholder="Your Subject" type="text" value="<?php echo set_value('subject'); ?>" />
                    <span class="text-danger"><?php echo form_error('subject'); ?></span>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-12">
                    <label for="message" class="control-label">Message</label>
                </div>
                <div class="col-md-12">
                    <textarea class="form-control" name="message" rows="4" placeholder="Your Message"><?php echo set_value('message'); ?></textarea>
                    <span class="text-danger"><?php echo form_error('message'); ?></span>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-12">
                    <input name="submit" type="submit" class="btn btn-primary" value="Send" />
                </div>
            </div>
            </fieldset>
            <?php echo form_close(); ?>
            <?php echo $this->session->flashdata('msg'); ?>
        </div>
    </div>
</div>

这是我的配置文件夹中的 email.php 文件:

<?php
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this
    $config['smtp_port'] = '465';
    $config['smtp_user'] = 'xxxxx@gmail.com'; //change this
    $config['smtp_pass'] = 'xxxxxxx'; //change this
    $config['mailtype'] = 'html';
    $config['charset'] = 'iso-8859-1';
    $config['wordwrap'] = TRUE;
    $config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard

?>

当我点击发送时,没有发送电子邮件。

【问题讨论】:

标签: php codeigniter codeigniter-3


【解决方案1】:

您可以按如下方式配置 Google SMTP 服务器

$config['protocol'] = 'smtp';

$config['smtp_host'] = 'ssl://smtp.gmail.com';

$config['smtp_port'] = '465';

$config['smtp_timeout'] = '7';

$config['smtp_user'] = 'youremail@gmail.com';

$config['smtp_pass'] = '你的邮箱密码';

$config['charset'] = 'utf-8';

$config['newline'] = "\r\n";

$config['mailtype'] = 'html'; // html 或文本

$config['验证'] = TRUE; // bool 是否验证电子邮件

Google 的 SMTP 服务器需要身份验证,因此设置方法如下:

  1. SMTP 服务器(即外发邮件):smtp.gmail.com
  2. SMTP 用户名:您的完整 Gmail 或 Google Apps 电子邮件地址(例如 example@gmail.com 或 example@yourdomain.com)
  3. SMTP 密码:您的 Gmail 或 Google Apps 电子邮件密码
  4. SMTP 端口:465
  5. 需要 SMTP TLS/SSL:是 要将外发电子邮件的副本存储在您的 Gmail 或 Google Apps 已发送文件夹中,请登录您的 Gmail 或 Google Apps 电子邮件设置并:
  6. 单击“转发/IMAP”选项卡并向下滚动到“IMAP 访问”部分:必须启用 IMAP 才能将电子邮件正确复制到您发送的文件夹中。

更多https://www.digitalocean.com/community/tutorials/how-to-use-google-s-smtp-serverCodeigniter email sending is not working

【讨论】:

    【解决方案2】:

    由于最近有很多关于 codeigniter 和发送邮件的问题,这似乎是一个紧迫的问题,我试着解释一下你可以做些什么来找出你的问题

    1。安装 PHP Mailer

    CI 没有提供那么多信息,以便找出您尝试发送电子邮件的问题 - 我建议为此目的使用 PHP Mailer。

    从 Github 下载最新的 PHPMailer Build。 可以找到项目here

    现在点击“克隆或下载”并将其下载为 zip - 如下图所示。

    在 Codeigniters index.php 所在的位置创建一个文件夹 phpmailer。

    这个 zip 文件中的根文件夹名为 - PHPMailer-master。 解压到你的phpmailer目录,把文件夹重命名为lib。

    你应该看到类似的东西

    2。使用 PHP 邮件程序

    首先我不会在这里写下文档中的内容或可以在其他地方找到的内容

    以下链接对你学习PHPMailer很有用

    Troubleshooting

    API

    Examples

    在您的 phpmailer 目录中创建一个名为 mail.php 的 php 文件。 一个例子可能是:

    require_once("./lib/PHPMailerAutoload.php");
    echo "<pre>";
    
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->Host = '0.0.0.0';
    $mail->SMTPAuth = true;
    $mail->Username = "yyy";
    $mail->Password = "xxx";
    $mail->Port = 25;
    $mail->SMTPDebug = 2;
    $mail->From = 'office@example.com';
    $mail->FromName = 'John Doe';
    $mail->addAddress('john.doe@example.com', 'John Doe');
    $mail->isHTML(true);
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    
    if(!$mail->send()) {
        echo '<br />Message could not be sent.';
        echo '<br />Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }
    

    3。完成对您的问题的调查

    这里最重要的是,您必须将邮件问题与 Codeigniter 分开。如果您设法通过 PHP Mailer 发送电子邮件,那么将它与 CI 集成应该很容易。

    只要你不能发送这些邮件,你应该调试它 使用 PHPMailer。

    如果您现在通过浏览器调用 mail.php 文件,您应该能够看到足够的信息来调查您的问题。

    最后一句话: 看看@你的垃圾邮件文件夹;)

    【讨论】:

      【解决方案3】:

      在您的配置 email.php 中更改此代码:

      $config['protocol'] = 'smtp';
      

      到这里:

      $config['protocol'] = 'mail';
      

      来源here

      【讨论】:

        猜你喜欢
        • 2017-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-26
        • 2012-09-06
        • 2015-11-03
        • 2016-12-07
        相关资源
        最近更新 更多