[英]Sending an Email from Form using Codeigniter
I'm using the codeigniter php framework to build a standard website including a contact form , the user of the webpage should be able to enter basic contact form information. 我正在使用codeigniter php框架来构建包含联系表单的标准网站,该网页的用户应该能够输入基本的联系表单信息。
I've set about and created the view file for the contact form and created a corresponding controller - this includes validation using the codeigniter validation library and then a private function to send the email to the webmaster regarding the enquiry etc. 我已经设置并创建了联系表单的视图文件,并创建了一个相应的控制器 - 这包括使用codeigniter验证库进行验证 ,然后使用私有函数将关于查询等的电子邮件发送给网站管理员。
I'm having issues getting past the form validation scenario, yet when submitting the form, I know for a fact that the data input should pass the validation tests and then pass the data to a private function to send away the email. 我在通过表单验证方案时遇到了问题,但在提交表单时,我知道数据输入应通过验证测试,然后将数据传递给私有函数以发送电子邮件。
Below are my files laid bare, if anyone could spot a problem, and promtly put me in the correct direction I'd be most greatful! 下面是我裸露的文件,如果有人能发现问题,并且正确地把我放在正确的方向上,我会非常感激!
<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;
}
}
}
Hopfully that's enough information to see what it is I'm doing, I believe what I've been doing to be correct, I'm not exactly sure where the fall out is to be honest. 有意思的是,有足够的信息来看我正在做什么,我相信我一直在做的事情是正确的,我不确定说实在的失败在哪里。
For sending mail you can try like this 对于发送邮件,您可以尝试这样做
<?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;
}
?>
It appears sadly, I should of held off making this post, as I eventually fixed and answered my own problem. 可悲的是,我应该暂时不做这篇文章,因为我最终修复并回答了我自己的问题。
The issue was inside the contact controller 问题出在联系人控制器内部
if ($this->input->post('form') == 'contactform')
if ($this->input->post()){}
It turns out after some more research if you leave the post() function empty, it selects all posted items from the contact form, before I had just simply created my own problem. 经过一些研究后,如果你将post()函数留空,它会在我刚刚创建自己的问题之前从联系表单中选择所有发布的项目。
Here some of my codes of sending email....i just add this in controller 这里有一些我发送电子邮件的代码....我只是在控制器中添加它
Code: 码:
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.