0

I made an contact form in codeigniter so users can contact me but when I try to send the email nothing is send. I don't know why it happens. Also I know This has been asked before but I only want to know if there's something wrong with my code.

This is my controller:

    <?php
class Contactform extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form','url'));
        $this->load->library(array('session', 'form_validation', 'email'));
    }

    function index()
    {
      //set validation rules
      //$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only');
        //$this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email');
       //$this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
       //$this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');

        //run validation on form input
        if ($this->form_validation->run() == FALSE)
        {
            //validation fails
            $this->load->view('contact_form_view');
        }
        else
        {
            //get the form data
            $name = $this->input->post('name');
            $from_email = $this->input->post('email');
            $subject = $this->input->post('subject');
            $message = $this->input->post('message');

            //naar welk email je het wilt sturen
            $to_email = 'ferran1004@gmail.com';



            //send mail
            $this->load->library('email', $config);
            $this->email->from($from_email, $name);
            $this->email->to($to_email);
            $this->email->subject($subject);
            $this->email->message($message);
             if ($this->email->send() == TRUE)
            {
                // email sent
                $this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
                redirect('contactform/index');
            }
            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('contactform/index');
            }
        }
    }

    //Alleen alfabet letters en spaties code
    function alpha_space_only($str)
   {
        if (!preg_match("/^[a-zA-Z ]+$/",$str))
        {
            $this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
            return FALSE;
        }
        else
        {
           return TRUE;
        }
    } 
}
?>

This is my view:

<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3 well">
            <?php $attributes = array("class" => "form-horizontal", "name" => "Contactform");
            echo form_open("Contactform/index", $attributes);?>
            <fieldset>
            <legend>Contact Form</legend>
            <div class="form-group">
                <div class="col-md-12">
                    <label for="name" class="control-label">Name</label>
                </div>
                <div class="col-md-12">
                    <input class="form-control" name="name" placeholder="Your Full Name" type="text" value="<?php echo set_value('name'); ?>" />
                    <span class="text-danger"><?php echo form_error('name'); ?></span>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-12">
                    <label for="email" class="control-label">Email ID</label>
                </div>
                <div class="col-md-12">
                    <input class="form-control" name="email" placeholder="Your Email ID" type="text" value="<?php echo set_value('email'); ?>" />
                    <span class="text-danger"><?php echo form_error('email'); ?></span>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-12">
                    <label for="subject" class="control-label">Subject</label>
                </div>
                <div class="col-md-12">
                    <input class="form-control" name="subject" placeholder="Your Subject" type="text" value="<?php echo set_value('subject'); ?>" />
                    <span class="text-danger"><?php echo form_error('subject'); ?></span>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-12">
                    <label for="message" class="control-label">Message</label>
                </div>
                <div class="col-md-12">
                    <textarea class="form-control" name="message" rows="4" placeholder="Your Message"><?php echo set_value('message'); ?></textarea>
                    <span class="text-danger"><?php echo form_error('message'); ?></span>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-12">
                    <input name="submit" type="submit" class="btn btn-primary" value="Send" />
                </div>
            </div>
            </fieldset>
            <?php echo form_close(); ?>
            <?php echo $this->session->flashdata('msg'); ?>
        </div>
    </div>
</div>

This is my email.php file in my config folder:

<?php
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this
    $config['smtp_port'] = '465';
    $config['smtp_user'] = 'xxxxx@gmail.com'; //change this
    $config['smtp_pass'] = 'xxxxxxx'; //change this
    $config['mailtype'] = 'html';
    $config['charset'] = 'iso-8859-1';
    $config['wordwrap'] = TRUE;
    $config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard

?>

When I click on send, no email is send.

lablanco
  • 103
  • 1
  • 13

3 Answers3

1

you can configure Google SMTP server as follows

$config['protocol'] = 'smtp';

$config['smtp_host'] = 'ssl://smtp.gmail.com';

$config['smtp_port'] = '465';

$config['smtp_timeout'] = '7';

$config['smtp_user'] = 'youremail@gmail.com';

$config['smtp_pass'] = 'youremailpassword';

$config['charset'] = 'utf-8';

$config['newline'] = "\r\n";

$config['mailtype'] = 'html'; // html or text

$config['validation'] = TRUE; // bool whether to validate email or not

Google's SMTP server requires authentication, so here's how to set it up:

  1. SMTP server (i.e., outgoing mail): smtp.gmail.com
  2. SMTP username: Your full Gmail or Google Apps email address (e.g example@gmail.com or example@yourdomain.com)
  3. SMTP password: Your Gmail or Google Apps email password
  4. SMTP port: 465
  5. SMTP TLS/SSL required: yes In order to store a copy of outgoing emails in your Gmail or Google Apps Sent folder, log into your Gmail or Google Apps email Settings and:
  6. Click on the Forwarding/IMAP tab and scroll down to the IMAP Access section: IMAP must be enabled in order for emails to be properly copied to your sent folder.

for more https://www.digitalocean.com/community/tutorials/how-to-use-google-s-smtp-server OR Codeigniter email sending is not working

SANDEEP S S
  • 68
  • 1
  • 9
0

since this seems to be a matter of urgency because there are a ton of questions lately regarding codeigniter & sending mails, i try to explain what you could do to find out your problem

1. installing PHP Mailer

CI doesn't provide that much information, in order to find out what's wrong with your attempt to send emails - i suggest to use PHP Mailer for that purpose.

Download the latest PHPMailer Build from Github. You can find the project here

Click now on "clone or download" and download it as zip - as in the image below is shown.

PHPMailer Zip Download

Create a Folder phpmailer where Codeigniters index.php lies.

The root folder in this zip File is called - PHPMailer-master. Unzip it to your phpmailer directory and rename the folder to lib.

You should see something like

Folder Overview Codeigniter - with phpmailer

2. Using PHP Mailer

first of all i'm not writing down here what stays in the Documentation or can found elsewhere

The following links are useful for you to study PHPMailer

Troubleshooting

API

Examples

Create a php file called mail.php within your phpmailer directory. An example could be:

require_once("./lib/PHPMailerAutoload.php");
echo "<pre>";

$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = '0.0.0.0';
$mail->SMTPAuth = true;
$mail->Username = "yyy";
$mail->Password = "xxx";
$mail->Port = 25;
$mail->SMTPDebug = 2;
$mail->From = 'office@example.com';
$mail->FromName = 'John Doe';
$mail->addAddress('john.doe@example.com', 'John Doe');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';

if(!$mail->send()) {
    echo '<br />Message could not be sent.';
    echo '<br />Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

3. Finishing investigating your problem

the most important thing here is, you've to separate your mail problem from Codeigniter. If you manage to send emails via PHP Mailer, it should be easy to integrate it with CI.

As long as you are not able to send these mails, you should debug it with PHPMailer.

if you call now your mail.php file via browser you should be able to see enough information in order to investigate your problem.

One last word: look @your spam folder too ;)

Atural
  • 5,389
  • 5
  • 18
  • 35
0

change this code in your config email.php:

$config['protocol'] = 'smtp';

to this:

$config['protocol'] = 'mail';

source here

winnie damayo
  • 426
  • 9
  • 17