如何在codeigniter中发送电子邮件

时间:2017-12-21 17:50:13

标签: php html codeigniter email

我想在codeigniter中发送电子邮件,但是当我按照教程进行操作时,这似乎不起作用是我的函数send_email()在Contact.php中

public function send_mail() {
    $this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
    $this->form_validation->set_rules('product', 'Message', 'trim|required|xss_clean');

    //run validation on form input
    if ($this->form_validation->run() == FALSE)
    {
        // redirect("/", "refresh");
    }
    else
    {
        // code to send mail
        $email = $this->input->post('email');
        $product = $this->input->post('product');

        $config = Array(
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://smtp.googlemail.com',
            'smtp_port' => 465,
            'smtp_user' => 'myemail@gmail.com',
            'smtp_pass' => 'xxxxx',
            'mailtype'  => 'html', 
            'charset'   => 'iso-8859-1'
        );
        $this->load->library('email', $config);

        $this->email->from($email);
        $this->email->to('tuanta04101996@gmail.com'); 

        $this->email->subject('Email Test');
        $this->email->message('Testing the email class.');

        $result = $this->email->send();          
        if ($result) {
            echo "<script type='text/javascript'>alert(Mail send successfully!!!);</script>";
            redirect("/", "refresh");
        } else {
            echo "<script type='text/javascript'>alert(Soemthing when wrong!!!);</script>";
            redirect("/", "refresh");
        }

    }
}

这是我的html表单

<?php
                $attributes = array('class' => 'w3-container w3-margin-bottom');
                echo form_open('contact/send_mail', $attributes);
            ?>
                <h3>For buyers:</h3>
                <p>
                    <label>Your email address</label>
                    <input class="w3-input" type="text" name="email">
                </p>
                <p>
                    <label>Products you want to source in Vietnam</label>
                    <input class="w3-input" type="text" name="product">
                </p>
                <p><button class="w3-button" style="float: right; border: 1px solid grey" name="submit">Send</button></p>
            <?php
                echo form_close();
            ?>

我已经阅读了教程,但它很通用。请问我,我错了,我该怎么办呢。

1 个答案:

答案 0 :(得分:0)

添加:

$this->email->set_newline("\r\n");

加载库

$this->load->library(...)

我找到的代码对我有用的唯一区别。