簡體   English   中英

使用Codeigniter從表單發送電子郵件

[英]Sending an Email from Form using Codeigniter

項目概況

我正在使用codeigniter php框架來構建包含聯系表單的標准網站,該網頁的用戶應該能夠輸入基本的聯系表單信息。

  1. 全名
  2. 電子郵件地址
  3. 電話
  4. 關於一般情況
  5. 查詢textarea
  6. 你是一個機器人 - 問題答案/驗證碼樣式的問題。

我已經設置並創建了聯系表單的視圖文件,並創建了一個相應的控制器 - 這包括使用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;
                    }



        }






    }

結論

有意思的是,有足夠的信息來看我正在做什么,我相信我一直在做的事情是正確的,我不確定說實在的失敗在哪里。

對於發送郵件,您可以嘗試這樣做

<?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;
    }
?>

固定

可悲的是,我應該暫時不做這篇文章,因為我最終修復並回答了我自己的問題。

問題出在聯系人控制器內部

之前:

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

后:

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

經過一些研究后,如果你將post()函數留空,它會在我剛剛創建自己的問題之前從聯系表單中選擇所有發布的項目。

這里有一些我發送電子郵件的代碼....我只是在控制器中添加它

碼:

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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM