使用Codeigniter从表单发送电子邮件

时间:2013-11-20 11:17:33

标签: php forms codeigniter email

项目概述

我正在使用 codeigniter php框架来构建包含联系表单的标准网站,该网页的用户应该能够输入基本的联系表单信息。

  1. 姓名
  2. 电子邮件地址
  3. 电话
  4. 关于一般
  5. 查询textarea
  6. 你是机器人 - 问题答案/验证码样式问题。
  7. 我已经设置并创建了联系表单的视图文件并创建了相应的控制器 - 这包括使用 codeigniter验证库进行验证,然后使用私有函数将电子邮件发送给网站管理员关于询问等。

    问题

    我遇到了通过表单验证方案的问题,但在提交表单时,我知道数据输入应通过验证测试,然后将数据传递给私有函数以发送电子邮件。

    我的代码

    以下是我裸露的文件,如果有人能发现问题,并且正确地指示我正确的方向,我会非常感激!

    联络视图

                <form method="POST" action="/contact/contactvalidate" name="contactform" id="contactform">
    
                <?php echo validation_errors(); ?>
                <label>Full Name:</label><br /> 
                <?php echo form_error('fullname'); ?>               
                <input type="text" name="fullname" id="fullname" maxlength="100" size="50" />
                <br />
                <label>Email:</label><br />
                <?php echo form_error('email'); ?>
                <input type="text" name="email" id="email" maxlength="100" size="50" />
                <br />
                <label>Telephone:</label>   <br />
                <?php echo form_error('telephone'); ?>
                <input type="text" name="telephone" id="telephone" maxlength="100" size="50" />
                <br />  
                <label>What is your enquiry regarding</label>   <br />
                <?php echo form_error('regarding'); ?>
                <select name="regarding" id="regarding">
                    <option value="General Enquiry">General Enquiry</option>
                    <option value="HR Consultancy Service">HR Consultancy Service</option>
                    <option value="Business Startup Service">Business Startup Service</option>
                    <option value="Solutions for Individuals">Solutions for Individuals</option>
                    <option value="Other">Other</option>
                </select>
                <br />
                <label>Your Enquiry</label> <br />
                <?php echo form_error('enquiry'); ?>
                <textarea name="enquiry" id="enquiry"></textarea>       
                <br />
                <label>What is 4 + 1?</label>   <br />
                <?php echo form_error('robot'); ?>
                <input type="text" name="robot" id="robot" />               
                <br />
                <input type="submit" value="Submit Post" class="button" />
    
    
    
                </form>
    

    联系控制器

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Contact extends CI_Controller {
    
            public function index()
            {
    
    
    
                        $this->load->view('general/view_header');
                        $this->load->view('page/view_contact');
                        $this->load->view('general/view_footer');
    
    
    
            }
    
    
    
            public function contactvalidate()
                {
                $this->load->library('form_validation');
    
                if ($this->input->post('form') == 'contactform'){
                        //Set the validation rules based on the page
                        $this->form_validation->set_rules('fullname', 'Name', 'required|max_length[50]|xss_clean|prep_for_form');
                        $this->form_validation->set_rules('email', 'Email Address', 'trim|required|max_length[255]|xss_clean|valid_email|prep_for_form');
                        $this->form_validation->set_rules('telephone', 'Telephone', 'required|max_length[20]|xss_clean|prep_for_form');
                        $this->form_validation->set_rules('regarding', 'Regarding');
                        $this->form_validation->set_rules('enquiry', 'Enquiry', 'required|max_length[800]|xss_clean|prep_for_form');
                        $this->form_validation->set_rules('robot', 'Sum', 'required');
                    }
    
    
                                if ($this->form_validation->run() === true)
                                            {
                                                //Send the email
                                                if($this->sendemail($_POST))
                                                {
                                                    //If successful load the appropriate view
                                                    redirect('/thank-you');
                                                }
    
                                            }
                        else{
                            //If page exists load all necessary views
                            $this->load->view('general/view_header');
                            $this->load->view('page/view_contact');
                            $this->load->view('general/view_footer');
                        }
    
    
                    }
    
    
    
            private function sendemail($content)
    
            {
    
    
    
    
    
    
    
    
                        //Load the email library
    
                        $this->load->library('email');
    
                        //Initialise the email helper and set the "from"
                        $this->email->initialize(array("mailtype" => "html"));
                        $this->email->from("no-reply@lesleynowell.com", "Lesley Nowell HR Consultancy");
    
                        //Set the recipient, subject and message based on the page
    
    
    
    
                                //$this->email->to('enquiries@lesleynowell.com');
                                $this->email->to('adam@urbanfeather.com');
                                $this->email->subject('Website Enquirie');
                                $this->email->message("My name is: {$content["fullname"]}<br /><br />My email address is: {$content["email"]}<br /><br />My telephone number is: {$content["telephone"]}<br /><br />The enquiry is regarding: {$content["regarding"]}<br /><br />Enquiry: {$content["enquiry"]}");
    
    
    
                        //If the email is sent
                        if($this->email->send())
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
    
    
    
            }
    
    
    
    
    
    
        }
    

    结论

    有意思的是,有足够的信息来看我正在做什么,我相信我一直在做的事情是正确的,我不确定失败的地方是诚实的。

3 个答案:

答案 0 :(得分:1)

对于发送邮件,您可以尝试这样

<?Php

$this->load->library('email', $config);
$this->email->initialize($config);
$this->email->from('info@domain.com', 'Name');

$message_body='Message Content';
$email = 'abc@domain.com';
$this->email->to($email);       
$this->email->subject('Your Subjec');       
$this->email->message($message_body);
$suc=$this->email->send();
if($suc)
   {
     return true;
    }
    else
    {
      return false;
    }
?>

答案 1 :(得分:1)

固定

令人遗憾的是,我应该暂时不做这篇文章,因为我最终修复并回答了我自己的问题。

问题出在联系人控制器

之前:

if ($this->input->post('form') == 'contactform')

后:

 if ($this->input->post()){}

经过一些研究后发现,如果你将post()函数留空,它会在我刚刚创建自己的问题之前从联系表单中选择所有发布的项目。

答案 2 :(得分:0)

这里有一些发送电子邮件的代码....我只是在控制器中添加它

代码:

function send_email($id){
        $this->emailformat($id,"10"); //i make another function that this will be call in view 
    }

    function emailformat($c_id,$days){
            $config['protocol'] = 'smtp'; // mail, sendmail, or smtp    The mail sending protocol.
            $config['smtp_host'] = '192.168.0.1'; // SMTP Server Address.
            $config['smtp_user'] = 'email@yahoo.com'; // SMTP Username.
            $config['smtp_pass'] = '12345'; // SMTP Password.
            $config['smtp_port'] = '25'; // SMTP Port. 25 is for local host
            $config['smtp_timeout'] = '5'; // SMTP Timeout (in seconds).
            $config['wordwrap'] = TRUE; // TRUE or FALSE (boolean)    Enable word-wrap.
            $config['wrapchars'] = 76; // Character count to wrap at.
            $config['mailtype'] = 'html'; // text or html Type of mail. If you send HTML email you must send it as a complete web page. Make sure you don't have any relative links or relative image paths otherwise they will not work.
            $config['charset'] = 'utf-8'; // Character set (utf-8, iso-8859-1, etc.).
            $config['validate'] = FALSE; // TRUE or FALSE (boolean)    Whether to validate the email address.
            $config['priority'] = 3; // 1, 2, 3, 4, 5    Email Priority. 1 = highest. 5 = lowest. 3 = normal.
            $config['crlf'] = "\r\n"; // "\r\n" or "\n" or "\r" Newline character. (Use "\r\n" to comply with RFC 822).
            $config['newline'] = "\r\n"; // "\r\n" or "\n" or "\r"    Newline character. (Use "\r\n" to comply with RFC 822).
            $config['bcc_batch_mode'] = FALSE; // TRUE or FALSE (boolean)    Enable BCC Batch Mode.
            $config['bcc_batch_size'] = 200; // Number of emails in each BCC batch.

                $this->load->library('email');
                $this->email->initialize($config);
                $this->email->from('email@yahoo.com', 'Robot');
                $this->email->to('email@yahoo.com');
                $this->email->subject('Expiration Notification of '.$c_id);
                $this->email->message('<html><body>This Message is to notify you that '.$c_id.' contract will expire in '.$days.' !</body></html>');
                $this->email->send();
    }