How to successfully send an email in a contact form through CodeIgniter?

瘦欲@ 提交于 2019-12-07 06:55:12

问题


I have set up my controller and my contact form all set up. But when I click "Submit" on my contact page, I receive no errors but I receive no email. Can anyone help me figure out why I am not actually receiving the email? I appreciate any help. Here is my code for the controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
    public function index()
    {
        $this->template->build('home');
    }

    public function email() {
        $name = $this->input->post('name');
        $email = $this->input->post('email');
        $phone_number = $this->input->post('phone_number');
        $message = $this->input->post('message');
        $this->load->library('email');
        $this->email->from($email, 'Your Name');
        $this->email->to('anish@develop.io');
        $this->email->cc('another@another-example.com');
        $this->email->bcc('them@their-example.com');
        $this->email->subject('Email Test');
        $this->email->message(
          'My name is'.$name.', Im testing this email class. My email is '.$email. '. My Phone number is '.$phone_number.
         ' This is my message '.$message. ' Thanks!!'
         );
        $this->email->send();
        echo $this->email->print_debugger();
        $this->template->build('home');
    }
}

And here is my code for my contact page view:

<div class="inner-content" id="contact-content">

  <title>CSS3 Contact Form</title>
  <div id="contact">
    <h1>Send an email</h1>
    <form method="post" action="/home/email">
      <fieldset>
        <label for="name">Name:</label>
        <input name="name" id="name" type="text" placeholder="Enter your full name">
        <label for="email">Email:</label>
        <input name="email" id="email" type="email" placeholder="Enter your email address">
        <label for="message">Message:</label>
        <textarea name="message" id="message" placeholder="Type your message here..."></textarea>
        <input type="submit" value="Send message">
      </fieldset>
    </form>
  </div>
</div>

回答1:


enable php_openssl.dll in php.ini file and try using gmail. The answer of Pascal Spruit or Omade will work. Don't forget to restart you local server after editing in php.ini file.




回答2:


$config = Array(
        'protocol'  => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => '465',
        'smtp_user' => 'yourname@gmail.com',
        'smtp_pass' => 'yourpassword',
        'mailtype'  => 'html',
        'starttls'  => true,
        'newline'   => "\r\n"
    );

$this->load->library('email', $config);
$this->email->from('youremail@gmail.com', 'Jodie');
$this->email->to('toemail@gmail.com');
$this->email->subject('testing email');
$this->email->message('This shows the email worked');
$this->email->send();
echo $this->email->print_debugger();



回答3:


If it helps try to configure email class first

$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;

$this->email->initialize($config);

Try to add this code around email->send() function to see what is the response

Change $this->email->send(); To

if ( ! $this->email->send())
{
    echo 'Some error in sending email';
}

If the error string doesn't echo then problem is not with CI, its probably with mail server.




回答4:


Try using gmail

$config = Array(
        'protocol'  => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => '465',
        'smtp_user' => 'someuser@gmail.com',
        'smtp_pass' => 'password',
        'mailtype'  => 'html',
        'starttls'  => true,
        'newline'   => "\r\n"
    );

$this->load->library('email', config);
$this->email->from('email@gmail.com', 'George');
$this->email->to('email@gmail.com');
$this->email->subject('hey this is an email');
$this->email->message('this is the content of the email');
$this->email->send();
echo $this->email->print_debugger();



回答5:


I also stuck with this same problem but I am figure out one thing every thing going on successfully on gmail but only email not goes.

if I am send $email = $this->input->post('useremail');that is not goes on gmail. It's show mail send successfully or what ever you pass on "if" condition but mail not come in mail box. But if I am pass any thing else like name phone password or any thing, that's all goes successfully. Is there any restriction in CI or php for email. Because I am send html content successfully on server but only email not goes. One more thing every thing send including email but through local-host but I am up my website on hostinger and config everything and everything going great but just email not pass on form,

If any one have any idea about this- how to include email on contact form, then pleas shear your knowledge with me.



来源:https://stackoverflow.com/questions/16969572/how-to-successfully-send-an-email-in-a-contact-form-through-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!