这是我的codiegniter联系我们表单代码,这是正确的,但我希望在这个联系我们表格中包含邮件功能,

时间:2016-04-07 11:24:59

标签: codeigniter phpmailer sendmail contact-form

此代码非常适合将数据保存到数据库中,但我只想在此代码中包含邮件功能。 请看一下。 我已经尝试了很多,但是徒劳无功。任何帮助都会很好。

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

    function index()
    {
        //set validation rules
        $this->form_validation->set_rules('name', 'Name', 'trim|required|callback_alpha_space_only');
        $this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
        $this->form_validation->set_rules('subject', 'Subject', 'trim|required');
        $this->form_validation->set_rules('companyname', 'Companyname', 'trim|required');
         //$this->form_validation->set_rules('industryname', 'Industryname', 'trim|required');
          $this->form_validation->set_rules('country', 'Country', 'trim|required');
        $this->form_validation->set_rules('message', 'Message', 'trim|required');


        //run validation on post data
        if ($this->form_validation->run() == FALSE)
        {   //validation fails
            $this->load->view('contact_form_view');
        }
        else
        {
            //insert the contact form data into database
            $data = array(
                'name' => $this->input->post('name'),
                'email' => $this->input->post('email'),
                'subject' => $this->input->post('subject'),
                'companyname' => $this->input->post('companyname'),
                //'industryname' => $this->input->post('industryname'),
                'country' => $this->input->post('country'),
                'message' => $this->input->post('message')
            );

            if ($this->db->insert('contacts', $data))
            {
                // i just want a mailer 
                $this->session->set_flashdata('msg','<div class="alert alert-success text-center">We received your message! Will get back to you shortly!!!</div>');
                redirect('contactform/index');

            }
            else
            {
                // error
                $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Some Error.  Please try again later!!!</div>');
                redirect('contactform/index');
            }
        }
    }

    //custom callback to accept only alphabets and space input
    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;
        }
    }
}
?>

此代码工作正常,将数据保存到数据库中。提前致谢。

我的邮件尝试就是这个

<?php
    //configure email settings
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'ssl://smtp.gmail.com';
    $config['smtp_port'] = '465';
    $config['smtp_user'] = 'user@gmail.com'; // email id
    $config['smtp_pass'] = 'password'; // email password
    $config['mailtype'] = 'html';
    $config['wordwrap'] = TRUE;
    $config['charset'] = 'iso-8859-1';
    $config['newline'] = "\r\n"; //use double quotes here
    $this->email->initialize($config);
?>


<?php
    //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');

    //set to_email id to which you want to receive mails
    $to_email = 'user@gmail.com';

    //send mail
    $this->email->from($from_email, $name);
    $this->email->to($to_email);
    $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">successfully!</div>');
        redirect('contactform/index');
    }
    else
    {
        //error
        $this->session->set_flashdata('msg','<div class="alert alert-danger text-center"> error </div>');
        redirect('contactform/index');
    }
?>

2 个答案:

答案 0 :(得分:1)

我假设您要从localhost发送邮件。要执行此操作,您可以使用PHPMailer

$this->load->library('PHPMailer/PHPMailer');
//require("class.phpmailer.php");
$mail=new PHPMailer(); //creat PHPMailer object
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; //needs login information
$mail->SMTPSecure = "tls"; //specifies tls security
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; //gmail smtp port

/******************* Set Your Credentials********************/
$mail->Username ="user@gmail.com" ; //SMTP gmail address
$mail->Password = "password"; // SMTP account password
$mail->From = "fromxyz"; //sender's gmail address
$mail->isHTML(true);

$mail->FromName = "From name";
$mail->AddAddress($email);//receiver's e-mail address
$mail->Subject = "Email Subject";//e-mail subject
$mail->Body ="Email Message";//e-mail message
$mail->WordWrap = 50;
if(!$mail->send()) 
{
     echo "error";
}
else
{
    echo "successs";
}

如果这也行不通,请尝试在gmail帐户中进行一些更改:

登录gmail。

转到我的帐户部分。

然后转到登录&amp;安全&gt;连接的应用程序&amp;网站

在此部分中,选择允许安全性较低的应用:开启

答案 1 :(得分:0)

控制器中的

尝试此类型

//run validation on post data
if ($this->form_validation->run() == FALSE)
{   //validation fails
    $this->load->view('contact_form_view');
}
else
{
    //insert the contact form data into database
    $data = array(
        'name' => $this->input->post('name'),
        'email' => $this->input->post('email'),
        'subject' => $this->input->post('subject'),
        'companyname' => $this->input->post('companyname'),
        //'industryname' => $this->input->post('industryname'),
        'country' => $this->input->post('country'),
        'message' => $this->input->post('message')
        );



 //load email libary
     $this->load->library('email');  

 //configure email settings
    $config['protocol'] = 'smtp`';
    $config['smtp_host'] = 'ssl://smtp.gmail.com';
    $config['smtp_port'] = '465';
    $config['smtp_user'] = 'user@gmail.com'; // email id
    $config['smtp_pass'] = 'password'; // email password
    $config['mailtype'] = 'html';
    $config['wordwrap'] = TRUE;
    $config['charset'] = 'iso-8859-1';
    $config['newline'] = "\r\n"; //use double quotes here
    $this->email->initialize($config);

     //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');

        //set to_email id to which you want to receive mails
        $to_email = 'user@gmail.com';

        //send mail
        $this->email->from($from_email, $name);
        $this->email->to($to_email);
        $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">successfully!</div>');

        }
        else
        {
            //error
            $this->session->set_flashdata('msg','<div class="alert alert-danger text-center"> error </div>');

        }

    }