-1

I want to send email in codeigniter but when i follow tutorial it seems doesn't work here is my function send_email() in 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");
        }

    }
}

and this is my html form

<?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();
            ?>

I have read tutorial but it is kind general. Please so me where i am wrong and how can i fix it.

1 Answers1

0

Add:

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

After load the library

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

Is the only difference I find with a code that works for me.

Danish Ali
  • 2,354
  • 3
  • 15
  • 26
jeprubio
  • 17,312
  • 5
  • 45
  • 56