Page 1 of 1
Adding fields to contact_us.php
Posted: Wed Nov 17, 2021 5:13 pm
by LeeFoster
Hi all,
So I know I can add fields to the contact us for and have used the addMaths hook file as a basis. What I don't know is how to then include these in the email that is sent.
Can anyone point me in the right direction?
Re: Adding fields to contact_us.php
Posted: Thu Nov 18, 2021 3:25 am
by ecartz
If you only want to edit one of enquiry, name, or email_address, you could do so with something like
Code: Select all
class hook_shop_contact_us_add_to_email {
public function listen_injectFormVerify() {
$GLOBALS['enquiry'] .= 'add to enquiry';
$GLOBALS['name'] .= 'add to name'
$GLOBALS['email_address'] .= 'add to email_address';
}
}
Note that some things that you might try to add to the email address might break it. That's also possible with name, although less likely. Adding to the enquiry is much safer.
Re: Adding fields to contact_us.php
Posted: Thu Nov 18, 2021 7:50 am
by LeeFoster
ecartz wrote: ↑Thu Nov 18, 2021 3:25 am
If you only want to edit one of enquiry, name, or email_address, you could do so with something like
Code: Select all
class hook_shop_contact_us_add_to_email {
public function listen_injectFormVerify() {
$GLOBALS['enquiry'] .= 'add to enquiry';
$GLOBALS['name'] .= 'add to name'
$GLOBALS['email_address'] .= 'add to email_address';
}
}
Note that some things that you might try to add to the email address might break it. That's also possible with name, although less likely. Adding to the enquiry is much safer.
Would I be right in assuming that I replace
'add to enquiry'
with my $_POST data?
Re: Adding fields to contact_us.php
Posted: Thu Nov 18, 2021 8:04 am
by ecartz
LeeFoster wrote: ↑Thu Nov 18, 2021 7:50 am
Would I be right in assuming that I replace
'add to enquiry'
with my $_POST data?
If that's what you want to add there. You could add any string that way. If it happens to come from post input, that's fine. Note that you probably want to do some kind of sanitization, e.g. Text::input.
Code: Select all
$GLOBALS['enquiry'] .= Text::input($_POST['new_field']);
And you may want to use sprintf or something to give it more context. But if you just want to append, this should do it.
Re: Adding fields to contact_us.php
Posted: Thu Nov 18, 2021 8:08 am
by LeeFoster
ecartz wrote: ↑Thu Nov 18, 2021 8:04 am
If that's what you want to add there. You could add any string that way. If it happens to come from post input, that's fine. Note that you probably want to do some kind of sanitization, e.g. Text::input.
Code: Select all
$GLOBALS['enquiry'] .= Text::input($_POST['new_field']);
And you may want to use sprintf or something to give it more context. But if you just want to append, this should do it.
Thanks, I managed to get it to just add a text string to the email so just need to work out adding my form data now instead.
Re: Adding fields to contact_us.php
Posted: Thu Nov 18, 2021 5:43 pm
by LeeFoster
Right I managed to get this mostly working for me, just need to work out how to attach images to the email now as well.
@ecartz any miracle suggestion?
Re: Adding fields to contact_us.php
Posted: Thu Nov 18, 2021 11:28 pm
by ecartz
The easy way is to turn on HTML emails and just add an
Code: Select all
<img src="https://example.com/path_to_image.png" />
If you need to embed the image, you have to override the existing mail call. When you call mail, this happens:
Code: Select all
public static function mail($to_name, $to_email_address, $email_subject, $email_text, $from_email_name, $from_email_address) {
if (SEND_EMAILS !== 'true') {
return false;
}
// Instantiate a new mail object
$message = new email();
$message->add_message($email_text);
$message->build_message();
return $message->send($to_name, $to_email_address, $from_email_name, $from_email_address, $email_subject);
}
So you need to do something like
Code: Select all
class hook_shop_contact_us_zz_add_image {
public function listen_injectFormVerify() {
if (Form::is_valid()) {
Form::block_processing();
$message = new email();
$message->add_message($email_text);
$message->find_html_images(DIR_FS_CATALOG . 'images/');
$message->build_message();
return $message->send($to_name, $to_email_address, $from_email_name, $from_email_address, $email_subject);
}
}
}
And then hope it all works, as core doesn't add images, so this isn't tested.
Re: Adding fields to contact_us.php
Posted: Sat Nov 20, 2021 12:40 pm
by LeeFoster
OK so here's where I am with this.
I have temporarily modified the tep_mail function in includes\functions\general.php and added in
$message->find_html_images('uploads/scribe.jpg')
Uploads being a temp folder for testing and scribe.jpg being an image file contained there.
However this just sends a .dat file with the content being uploads/scribe.jpg
add_html_image & add_attachment do the same.
What am I missing?
* Yes I know this shouldn't be done in core and once it's working I'll move it out.
Re: Adding fields to contact_us.php
Posted: Sun Nov 21, 2021 9:21 pm
by LeeFoster
ecartz wrote: ↑Thu Nov 18, 2021 11:28 pm
The easy way is to turn on HTML emails and just add an
Code: Select all
<img src="https://example.com/path_to_image.png" />
Managed to get it to add the image into the HTML using this. Would still prefer it to add as an attachment for ease of saving but this will suffice for now.