在本地主机上使用codeigniter发送电子邮件 [英] Send email using codeigniter on localhost

查看:70
本文介绍了在本地主机上使用codeigniter发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个发送电子邮件表单,并且我的代码不断给我错误消息,表明未发送电子邮件。这是我第一次在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,

这是我的控制器:

  <?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("\r\n");

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

这是我的HTML文件:

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>


推荐答案

要使用gmail smtp通过xampp在本地主机上发送电子邮件,将您的sendmail.ini修改为类似的内容

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

发送邮件位置:xampp / sendmail / sendmail.ini

,将电子邮件中的设置更改为允许安全性较低的应用访问您的帐户

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

这篇关于在本地主机上使用codeigniter发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆