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

时间:2017-06-07 19:25:29

标签: php codeigniter email

我正在尝试创建一个发送电子邮件表单,我的代码一直向我提供错误消息,表明该电子邮件未被发送。这是我第一次在codeigniter上创建这样的表单。请帮忙,

这是我的控制器:

  <?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文件:

    <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 个答案:

答案 0 :(得分:0)

$config = Array(
  'protocol' => 'smtp',
  'smtp_host' => 'ssl://smtp.googlemail.com',
  'smtp_port' => 465,
  'smtp_user' => 'xxx',
  'smtp_pass' => 'xxx',
  'mailtype'  => 'html', 
  'charset'   => 'iso-8859-1',
  'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");

// Set to, from, message, etc.

$result = $this->email->send();

了解更多信息:https://www.codeigniter.com/user_guide/libraries/email.html

答案 1 :(得分:0)

要使用gmail smtp通过xampp在localhost上发送电子邮件,请将sendmail.ini编辑为类似的内容

    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位置:xampp / sendmail / sendmail.ini
此外,将电子邮件中的设置更改为允许安全性较低的应用访问您的帐户