Примерный костяк:
В шаблон пилите такое:
<div id="form">
<form id="z">
<input type="text" placeholder="Ваше имя" name="n">
<input type="text" placeholder="+38(__)___-__-__" name="t">
<textarea placeholder="Комментарий" name="c"></textarea>
<div class="send_form">Отправить</div>
</form>
</div>
<script type="text/javascript"><!--
$('.send_form').click(function() {
$.ajax({
url: 'index.php?route=common/header/send_form', //сюда пишите путь к контроллеру куда вставим код ниже
type: 'post',
data: $('#z input, #z textarea'),
dataType: 'json',
success: function(json) {
if(json['error']){
alert(json['error']);
}
if(json['success']){
$('.send_form').hide();
$('.send_form').before('<span class="succ" style="color:green;font-weight:bold;">Вы успешно отправили заявку!</span>');
setTimeout (function(){
$('.succ').remove();
$('.send_form').show('600');
}, 3000);
}
}
});
});
//--></script>
И в контроллер такое:
public function send_form() {
$json = array();
if ($this->request->server['REQUEST_METHOD'] == 'POST') {
if ((utf8_strlen($this->request->post['t']) < 6) || (utf8_strlen($this->request->post['t']) > 2500)) {
$json['error'] = 'Заполните пожалуйста номер телефона';
}
if ((utf8_strlen($this->request->post['n']) < 3) || (utf8_strlen($this->request->post['n']) > 2500)) {
$json['error'] = 'Пожалуйста, укажите Ваше имя';
}
$text = '';
if (!isset($json['error'])) {
//send form
if(!$this->request->post['c']) $this->request->post['n'] = 'Аноним';
$html = '<h3>Письмо от '.$this->request->post['n'].'</h3>';
if($this->request->post['n'])$html .= '<p><b>Имя:</b> '.$this->request->post['n'].'</p>';
$html .= '<p><b>Телефон:</b> '.$this->request->post['t'].'</p>';
if($this->request->post['c']) $html .= '<p><b>Комментарий:</b> '.$this->request->post['c'].'</p>';
$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->config->get('config_email'));
$mail->setSender($this->request->post['n']);
$mail->setSubject(html_entity_decode('Тема письма от '.$this->request->post['n'], ENT_QUOTES, 'UTF-8'));
$mail->setHtml($html);
$mail->setText(html_entity_decode($text, ENT_QUOTES, 'UTF-8'));
$mail->send();
$json['success'] = true;
}
}
$this->response->setOutput(json_encode($json));
}
Схема упрощена до безобразия - но главное что все отлично работает.