duanbanzhi4419 2017-06-07 19:25
浏览 69
已采纳

使用localhost上的codeigniter发送电子邮件

I am trying to create a send email form and my code keeps giving me the error message that the email is not being sent. This is my first time creating a form like this on codeigniter. Please help,

Here's my controller:

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

class Contactus extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('email');
    }
    public function index(){
        $this->load->helper('url');

        $this->load->view('base');
        $this->load->view('contactUs');
    }
    public function send_mail(){
        //read parameters from $_POST using input class
        $name = $this->input->post('name');
        $email = $this->input->post('email');
        $subject = $this->input->post('subject');
        $message = $this->input->post('yourMessage');

        $config = Array(
            'protocol' => 'smtp',
            'smtp_host' => 'your host',
            'smtp_port' => port for host,
            'smtp_user' => 'user@example.com',
            'smtp_pass' => 'yourpassword',
            'mailtype' => 'html',
            'charset' => 'iso-8859-1',
            'wordwrap' => TRUE
        );
        $this->load->library('email', $config);

        $this->email->set_newline("
");

        $this->email->from($email, $name);
        $this->email->to('example@example.com');

        $this->email->subject($subject);
        $this->email->message($message);

        if ($this->email->send()){
            // mail sent
            $this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
            redirect('contactus/send_mail');
        }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('contactus/index');
        }
    }
}

Here's my Html file:

    <html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/contact.css">
        <title>Contact Us</title>
    </head>
    <body>
        <h1><center>Contact Us</center></h1>
        <hr>
        <div class="container">
            <div id="home-search">
                <form action= "<?php echo site_url("contactus/send_mail"); ?>" method='POST'>
                    <div class="row">
                    <div class="col-md-8 col-md-offset-2">
                        <img src="http://oasissanjose.com/uploads/3/6/2/4/3624456/8429182_orig.png" class="pull-right" width="200" height="250">

                        <div class="panel panel-primary">
                            <h2 class="lead section-lead panel-heading">Questions, Concerns? Contact us using the email template below:</h2>
                            <h3 class="text-info panel-body"><center>All Fields are Mandatory</center></h3>
                            <div class="form-all">
                                    <div class="form-group">
                                        <label for="name">Full Name<span class="form-required">*</span></label>
                                        <input type="text" class="form-control" id="name" name="name" placeholder="Jane Doe" required/>
                                    </div>
                                    <div class="form-group">
                                        <label for="email">Email Address<span class="form-required">*</span></label>
                                        <input type="email" class="form-control" id="email" name="email" placeholder="jane.doe@example.com" required/>
                                    </div>
                                    <div class="form-group">
                                        <label for="subject">Subject<span class="form-required">*</span></label>
                                        <input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" required/>
                                    </div>
                                    <div class="form-group ">
                                        <label for="yourMessage">Your Message<span class="form-required">*</span></label>
                                        <textarea  class="form-control" id="yourMessage" name="yourMessage" placeholder="Message" required></textarea> 
                                    </div>
                                    <input type="submit" class="btn btn-primary" name="submit" value="Send Message" >
                                <br>
                            </div>
                        </div>
                        <br><br>
                        <img src="http://content.presentermedia.com/files/animsp/00005000/5562/question_mark_serious_thinker_md_wm.gif" class="pull-right" width="400" height="250">
                    </div>
                </div>
                </form>
                <?php echo $this->session->flashdata('msg'); ?>
            </div>
        </div>
        <!-- End of Container -->
    </body>
</html>
  • 写回答

2条回答 默认 最新

  • dpj997991 2017-06-08 00:23
    关注

    For sending email on localhost using gmail smtp through xampp, edit your sendmail.ini to something like this

        smtp_server=smtp.gmail.com
        smtp_port=587
        smtp_ssl=tls
    
        auth_username=your_email@gmail.com
        auth_password=password_of_email
    
        force_sender=your_email@gmail.com
        hostname=smtp.gmail.com
    

    sendmail location : xampp/sendmail/sendmail.ini
    also, change settings in your email to the Allowing less secure apps to access your account

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?