Does anyone with knowledge of OpenCart 2.0.1.1 know how I could implement the following addAttachment function found in system/libary/mail.php:
public function addAttachment($filename) {
$this->attachments[] = $filename;
}
into catalog/controller/information/contact.php - so that the default contact form can also include an attachment upload feature? I tried this but no dice.
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
unset($this->session->data['captcha']);
$mail = new Mail($this->config->get('config_mail'));
$mail->setTo($this->config->get('config_email'));
$mail->setFrom($this->request->post['email']);
$mail->setSender($this->request->post['name']);
$mail->setSubject(sprintf($this->language->get('email_subject'), $this->request->post['name']));
$mail->setText(strip_tags($this->request->post['enquiry']));
$mail->addAttachment($this->request->post['file']);
$mail->send();
$this->response->redirect($this->url->link('information/contact/success'));
}
You can not directly pass file to $mail->addAttachment($this->request->post['file']);
First you need to upload file
//catalog/view/theme/default/template/information/contact.tpl
<div class="form-group">
<label class="col-sm-2 control-label" for="input-file">File</label>
<div class="col-sm-10">
<button type="button" id="button-upload" data-loading-text="Uploading.." class="btn btn-default btn-block"><i class="fa fa-upload"></i> <?php echo 'Upload'; ?></button>
<input type="hidden" name="file" value="" id="file"/>
</div>
</div>
Now we need upload script to upload file
//before footer in catalog/view/theme/default/template/information/contact.tpl
<script>
$('button[id^=\'button-upload\']').on('click', function() {
var node = this;
$('#form-upload').remove();
$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');
$('#form-upload input[name=\'file\']').trigger('click');
timer = setInterval(function() {
if ($('#form-upload input[name=\'file\']').val() != '') {
clearInterval(timer);
$.ajax({
url: 'index.php?route=tool/upload',
type: 'post',
dataType: 'json',
data: new FormData($('#form-upload')[0]),
cache: false,
contentType: false,
processData: false,
beforeSend: function() {
$(node).button('loading');
},
complete: function() {
$(node).button('reset');
},
success: function(json) {
$('.text-danger').remove();
if (json['error']) {
$(node).parent().find('input').after('<div class="text-danger">' + json['error'] + '</div>');
}
if (json['success']) {
alert(json['success']);
$(node).parent().find('input').attr('value', json['code']);
}
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
}
}, 500);
});
</script>
Finally now you can pass attachment file to mail function
//catalog/controller/information/contact.php
if($this->request->post['file']){
$this->load->model('tool/upload');
$upload_info = $this->model_tool_upload->getUploadByCode($this->request->post['file']);
$phyname = DIR_UPLOAD.$upload_info['filename'];
$temp_name = DIR_UPLOAD.$upload_info['name'];
copy($phyname,$temp_name);
$mail->AddAttachment($temp_name);
}
$mail->send();
if(isset($temp_name)){
unlink( $temp_name );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With