Re: How to create a form for users to submit info + a file?
Posted: Fri Oct 05, 2012 10:08 am
I used the tutorial from the first page to generate a simple contact form using controller, template and language files. My form has a file upload option; however, I am stumped on how to attach the file to the email and change the content type.
I've listed my controller file. It sends a basic text email, it just doesn't attach the file not does it honor html tags within the body of the email.
Hopefully someone can push me in the right direction.
I've listed my controller file. It sends a basic text email, it just doesn't attach the file not does it honor html tags within the body of the email.
Hopefully someone can push me in the right direction.
Code: Select all
<?php
class ControllerInformationSellform extends Controller {
private $error = array(); // <-- add this line right here!
public function index() {
$this->language->load('information/sellform'); //Optional. This calls for your language file
$this->data['text_name'] = $this->language->get('text_name');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$mail = new Mail();
$mail->protocol = $this->config->get('config_mail_protocol');
$mail->parameter = $this->config->get('config_mail_parameter');
$mail->hostname = $this->config->get('config_smtp_host');
$mail->username = $this->config->get('config_smtp_username');
$mail->password = $this->config->get('config_smtp_password');
$mail->port = $this->config->get('config_smtp_port');
$mail->timeout = $this->config->get('config_smtp_timeout');
$mail->setTo($this->config->get('config_email'));
$mail->setFrom($this->request->post['email']);
$mail->setSender($this->request->post['name']);
$email_body = '<h1>Name: </h1>' . $this->request->post['name'] . "\n\r";
$email_body .= 'Phone: ' . $this->request->post['phone'] . "\n\r";
$email_body .= 'Email: ' . $this->request->post['email'] . "\n\r";
$email_body .= 'Comments: ' . $this->request->post['enquiry'] . "\n\r";
$mail->setSubject(sprintf("Sell my Watch or Jewelry Request from Website", $this->request->post['name']));
$mail->setText(strip_tags(html_entity_decode($email_body, ENT_QUOTES, 'UTF-8')));
$mail->send();
$this->redirect($this->url->link('information/sellform/success'));
}
if (isset($this->error['name'])) {
$this->data['error_name'] = $this->error['name'];
} else {
$this->data['error_name'] = '';
}
if (isset($this->error['email'])) {
$this->data['error_email'] = $this->error['email'];
} else {
$this->data['error_email'] = '';
}
if (isset($this->error['phone'])) {
$this->data['error_phone'] = $this->error['phone'];
} else {
$this->data['error_phone'] = '';
}
if (isset($this->error['enquiry'])) {
$this->data['error_enquiry'] = $this->error['enquiry'];
} else {
$this->data['error_enquiry'] = '';
}
if (isset($this->error['file'])) {
$this->data['error_file'] = $this->error['file'];
} else {
$this->data['error_file'] = '';
}
if (isset($this->error['captcha'])) {
$this->data['error_captcha'] = $this->error['captcha'];
} else {
$this->data['error_captcha'] = '';
}
if (isset($this->request->post['name'])) {
$this->data['name'] = $this->request->post['name'];
} else {
$this->data['name'] = '';
}
if (isset($this->request->post['email'])) {
$this->data['email'] = $this->request->post['email'];
} else {
$this->data['email'] = '';
}
if (isset($this->request->post['phone'])) {
$this->data['phone'] = $this->request->post['phone'];
} else {
$this->data['phone'] = '';
}
if (isset($this->request->post['enquiry'])) {
$this->data['enquiry'] = $this->request->post['enquiry'];
} else {
$this->data['enquiry'] = '';
}
if (isset($this->request->post['file'])) {
$this->data['file'] = $this->request->post['file'];
}
if (isset($this->request->post['captcha'])) {
$this->data['captcha'] = $this->request->post['captcha'];
} else {
$this->data['captcha'] = '';
}
$this->data['action'] = $this->url->link('information/sellform');
$this->data['store'] = $this->config->get('config_name');
$this->data['address'] = nl2br($this->config->get('config_address'));
$this->data['telephone'] = $this->config->get('config_telephone');
$this->data['fax'] = $this->config->get('config_fax');
//*$this->document->setTitle($this->language->get('heading_title')); //Optional. Set the title of your web page.
$this->data['breadcrumbs'] = array();
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home'),
'separator' => false
);
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('information/sellform'),
'separator' => $this->language->get('text_separator')
);
// Text from language file
$this->data['heading_title'] = $this->language->get('heading_title');
$this->data['text_content'] = $this->language->get('text_content');
$this->data['button_continue'] = $this->language->get('button_continue');
$this->data['heading_title'] = $this->language->get('heading_title');
$this->data['text_location'] = $this->language->get('text_location');
$this->data['text_contact'] = $this->language->get('text_contact');
$this->data['text_address'] = $this->language->get('text_address');
$this->data['text_telephone'] = $this->language->get('text_telephone');
$this->data['text_fax'] = $this->language->get('text_fax');
$this->data['entry_name'] = $this->language->get('entry_name');
$this->data['entry_email'] = $this->language->get('entry_email');
$this->data['entry_phone'] = $this->language->get('entry_phone');
$this->data['entry_enquiry'] = $this->language->get('entry_enquiry');
$this->data['entry_file'] = $this->language->get('entry_file');
$this->data['entry_captcha'] = $this->language->get('entry_captcha');
// We call this Fallback system
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/information/sellform.tpl')) { //if file exists in your current template folder
$this->template = $this->config->get('config_template') . '/template/information/sellform.tpl'; //get it
} else {
$this->template = 'default/template/information/sellform.tpl'; //or get the file from the default folder
}
//Required. The children files for the page.
$this->children = array(
'common/column_left',
'common/column_right',
'common/content_top',
'common/content_bottom',
'common/footer',
'common/header'
);
$this->response->setOutput($this->render());
}
public function success() {
$this->language->load('information/sellform');
$this->document->setTitle($this->language->get('heading_title'));
$this->data['breadcrumbs'] = array();
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home'),
'separator' => false
);
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('information/sellform'),
'separator' => $this->language->get('text_separator')
);
$this->data['heading_title'] = $this->language->get('heading_title');
$this->data['text_message'] = $this->language->get('text_message');
$this->data['button_continue'] = $this->language->get('button_continue');
$this->data['continue'] = $this->url->link('common/home');
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/success.tpl')) {
$this->template = $this->config->get('config_template') . '/template/common/success.tpl';
} else {
$this->template = 'default/template/common/success.tpl';
}
$this->children = array(
'common/column_left',
'common/column_right',
'common/content_top',
'common/content_bottom',
'common/footer',
'common/header'
);
$this->response->setOutput($this->render());
}
public function captcha() {
$this->load->library('captcha');
$captcha = new Captcha();
$this->session->data['captcha'] = $captcha->getCode();
$captcha->showImage();
}
private function validate() {
if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 32)) {
$this->error['name'] = $this->language->get('error_name');
}
if (!preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
$this->error['email'] = $this->language->get('error_email');
}
if ((utf8_strlen($this->request->post['phone']) != null)) {
if ((utf8_strlen($this->request->post['phone']) < 7) || (utf8_strlen($this->request->post['phone']) > 3000) && (utf8_strlen($this->request->post['phone']) != null)) {
$this->error['phone'] = $this->language->get('error_phone');
}
}
if ((utf8_strlen($this->request->post['enquiry']) != null)) {
if ((utf8_strlen($this->request->post['enquiry']) < 10) || (utf8_strlen($this->request->post['enquiry']) > 3000) && $this->request->get['information_id'] == 10) {
$this->error['enquiry'] = $this->language->get('error_enquiry');
}
}
if (!isset($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
$this->error['captcha'] = $this->language->get('error_captcha');
}
if (!$this->error) {
return true;
} else {
return false;
}
}
}
?>