Adding fields to contact_us.php

Open to all! Ask other shopowners for help.
Post Reply
LeeFoster
Contributor
Posts: 263
Joined: Sun Feb 28, 2021 9:41 pm
Phoenix Version: v1.0.8.20
Has thanked: 1 time
Been thanked: 5 times

Adding fields to contact_us.php

Post 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?


Join The Code Co-op to get access to your library in the Code Co-op Forum
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Adding fields to contact_us.php

Post 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.
LeeFoster
Contributor
Posts: 263
Joined: Sun Feb 28, 2021 9:41 pm
Phoenix Version: v1.0.8.20
Has thanked: 1 time
Been thanked: 5 times

Re: Adding fields to contact_us.php

Post 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?
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Adding fields to contact_us.php

Post 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.
LeeFoster
Contributor
Posts: 263
Joined: Sun Feb 28, 2021 9:41 pm
Phoenix Version: v1.0.8.20
Has thanked: 1 time
Been thanked: 5 times

Re: Adding fields to contact_us.php

Post 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.
LeeFoster
Contributor
Posts: 263
Joined: Sun Feb 28, 2021 9:41 pm
Phoenix Version: v1.0.8.20
Has thanked: 1 time
Been thanked: 5 times

Re: Adding fields to contact_us.php

Post 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?
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Adding fields to contact_us.php

Post 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.
LeeFoster
Contributor
Posts: 263
Joined: Sun Feb 28, 2021 9:41 pm
Phoenix Version: v1.0.8.20
Has thanked: 1 time
Been thanked: 5 times

Re: Adding fields to contact_us.php

Post 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.
LeeFoster
Contributor
Posts: 263
Joined: Sun Feb 28, 2021 9:41 pm
Phoenix Version: v1.0.8.20
Has thanked: 1 time
Been thanked: 5 times

Re: Adding fields to contact_us.php

Post 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.


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply