简体   繁体   中英

how to send filled form via php email

我在我的网站上有一个表单供用户填写。表单验证后,我想按原样发送带有填入邮件的值的表单(可能是HTML表单或pdf),我应该怎么做呢?使用Php或Codeigniter发送表单,但我不知道如何将表单发送为HTML表单或Pdf。

Try this:

#post your HTML form in a view say form.php
public function sendForm(){                 #the controller function
    if($this->input->post(null)){
        $postValues = $this->input->post(null); #retrieve all the post variables and send to form.php
        $form   = $this->load->view('form.php', $postValues, true); #retrieve the form as HTML and send via email
        $this->load->library('email');
        $this->email->from('your@example.com', 'Your Name');
        $this->email->to('someone@example.com'); 
        $this->email->subject('Email Test');
        $this->email->message($form);   
        $this->email->send();
    }
}

You mean send the form as is via email?

In laravel framework, you can send a view(with your form) entirely by passing in a $view. I don't know about codeigniter, maybe they have this kind of function too?

I would use PHPMailer if I were you, since it has a very convenient interface and supports different secure protocols through socket. If you follow the mentioned link, you'll find a very descriptive example of usage. Now, you want to focus on these lines:

<?php

$mail = new PHPMailer;
// [...]
$mail->IsHTML(true);
// [...]
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';

?>

I bet you can pass any string to the Body property. So just render your HTML form filled with values into a variable, then pass it to Body .

Well this is pretty easy with CI's Email Class

all you have to do is handle the post and then you can create a new view and pass the forms data through to it

$this->email->initialize($config);
$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->cc('another@another-example.com');
$this->email->bcc('them@their-example.com');

$this->email->subject('Email Test');
$data['form_post'] = $this->input->post();
$msg = $this->load->view('email/template',$data,true);
$this->email->message($msg); 
$this->email->alt_message('Something Should go here Else CI just takes the original and  strips the tags');
$this->email->send();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM