联系我们电子邮件发送在codeigniter php不工作 [英] Contact us email sending is not working in codeigniter php

查看:166
本文介绍了联系我们电子邮件发送在codeigniter php不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个请求表单在我的网站一旦我提交的请求表格的细节不能发送电子邮件给管理员关于请求。它是重定向欢迎/请求功能没有得到任何错误

Hi i am having a request form in my website once i submit the details in request form not able to send an email to admin regarding the request.It is redirecting welcome/request function not getting any errors as well

控制器:

class Welcome extends CI_Controller {
function __construct() 
        { 
            parent::__construct();

    $this->load->helper(array('form','url'));
    $this->load->library(array('session', 'form_validation', 'email'));
        }
public function index()
{
    $this->load->model('index_model');
    $data['records2'] = $this->index_model->get_all_banners();
    $data['mainpage'] = "index";
    $this->load->view('templates/template',$data);
}

function request()
{
    $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
        $this->form_validation->set_rules('name','First Name' , 'required');
        $this->form_validation->set_rules('email','Email');
        $this->form_validation->set_rules('phone','Mobile Number');
        $this->form_validation->set_rules('description','Description');
        if($this->form_validation->run()== FALSE)   
        {   
        $data['mainpage']='index';
        $this->load->view('templates/template',$data);
        }
    else
    {
        //get the form data
        $name = $this->input->post('name');
        $from_email = $this->input->post('email');
        $subject = $this->input->post('phone');
        $message = $this->input->post('description');       

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

        $config=Array(
    'protocol'=> 'smtp',
    'smtp_host' => 'ssl://smtp.gmail.com', //smtp host name
    'smtp_port' => '465', //smtp port number
    'smtp_user' => 'xxx@gmail.com',
    'smtp_pass' => '*******', //$from_email password
    'mailtype' =>'html',
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE
    );

    //send mail
    $this->load->library('email',$config);
    $this->email->from($name);
    $this->email->to('$to_email');
    $this->email->subject($subj);
    $this->email->message($messagess1);
    $this->email->set_newline("\r\n");

        if ($this->email->send())
        {
            // mail sent
            $this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
            redirect('welcome');
        }
        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('welcome');
        }
    }
}


}


推荐答案

下载: codeigniter-phpmailer

public function send_mail_ctrl()
    { 

        $config['useragent']        = 'PHPMailer';              // Mail engine switcher: 'CodeIgniter' or 'PHPMailer'  
        $config['protocol']         = 'smtp';                   // 'mail', 'sendmail', or 'smtp'
        $config['mailpath']         = '/usr/sbin/sendmail';
        $config['smtp_host']        = 'smtp.gmail.com';
        $config['smtp_user']        = 'youremail';
        $config['smtp_pass']        = 'password';
        $config['smtp_port']        = 465;
        $config['smtp_timeout']     = 30;                       // (in seconds)
        $config['smtp_crypto']      = 'ssl';                    // '' or 'tls' or 'ssl'
        $config['smtp_debug']       = 0;                        // PHPMailer's SMTP debug info level: 0 = off, 1 = commands, 2 = commands and data, 3 = as 2 plus connection status, 4 = low level data output.
        $config['smtp_auto_tls']    = false;                    // Whether to enable TLS encryption automatically if a server supports it, even if `smtp_crypto` is not set to 'tls'.
        $config['smtp_conn_options'] = array();                 // SMTP connection options, an array passed to the function stream_context_create() when connecting via SMTP.
        $config['wordwrap']         = true;
        $config['wrapchars']        = 76;
        $config['mailtype']         = 'html';                   // 'text' or 'html'
        $config['charset']          = null;                     // 'UTF-8', 'ISO-8859-15', ...; NULL (preferable) means config_item('charset'), i.e. the character set of the site.
        $config['validate']         = true;
        $config['priority']         = 3;                        // 1, 2, 3, 4, 5; on PHPMailer useragent NULL is a possible option, it means that X-priority header is not set at all, see https://github.com/PHPMailer/PHPMailer/issues/449
        $config['crlf']             = "\n";                     // "\r\n" or "\n" or "\r"
        $config['newline']          = "\n";                     // "\r\n" or "\n" or "\r"
        $config['bcc_batch_mode']   = false;
        $config['bcc_batch_size']   = 200;
        $config['encoding']         = '8bit';

        $this->load->library('email');
         $this->email->initialize($config);
        /*$subject = 'This is a test';
        $message = '<p>This message has been sent for testing purposes.</p>';*/        

            $message = $this->input->post('message');
            $email = $this->input->post('email');
            $subject = $this->input->post('subject');


        // Get full html:
        $body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=' . strtolower(config_item('charset')) . '" />
            <title>' . html_escape($subject) . '</title>
            <style type="text/css">
                body {
                    font-family: Arial, Verdana, Helvetica, sans-serif;
                    font-size: 16px;
                }
            </style>
        </head>
        <body>

        ' . $message . '
        </body>
        </html>';
        // Also, for getting full html you may use the following internal method:
        //$body = $this->email->full_html($subject, $message);

        $result = $this->email
                ->from('your_email@gmail.com')
                ->reply_to('any_email@gmail.com')    // Optional, an account where a human being reads.
                ->to($email)
                ->subject($subject)
                ->message($body)
                ->send();

        var_dump($result);
        echo '<br />';
        //echo $this->email->print_debugger();

        if ($result == TRUE)
        {
            // mail sent
            echo "<div class='alert alert-success text-center'>Your mail has been sent successfully!</div>";
        }
        else
        {
           echo'<div class="alert alert-danger text-center">Error in sending your message! Please try again later.</div>';
        }

        exit;
}

这篇关于联系我们电子邮件发送在codeigniter php不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆