How do I send email to user (with pdf attachment) when a form is submitted?

I have a simple form (firstname, lastname, email). When the form is completed I'd like to send an email with an attached pdf to the entered email address.

I tried entering the code below into the controller.php but no email was sent as far as I can see.
Any ideas very much appreciated.

public function action_send()
      $bodyHTML = '<p>This is the body of the email.</p>';
      $mh = Loader::helper('mail');
      $mh->to([email protected]'); 
      $mh->from([email protected]'); 
      $mh->setSubject(t('Test email'));

View Replies:
mhawke replied on at Permalink Reply
Unfortunately, the core mail functiions cannot send attachments so the solution would be more involved. There are several good form Form add-ons in the marketplace that will do this (I think).
PJSAndo replied on at Permalink Reply
Hello mhawake,


I'll have a look around for an add on to do the job.

PJSAndo replied on at Permalink Reply
Hello mhawke,

Is there any reason why the code above should not work for sending an email (without an attachment) to the person who has completed the form? I might just have to provide a link to the pdf in the email as I can't find a 5.7 add on that will do it for me.

mhawke replied on at Permalink Reply
I may have spoken too soon regarding the ability to send attachments. Let me have a look under the hood and see what's there.
PJSAndo replied on at Permalink Reply
I just found this.

Using this, I have an email sending on page load. Just need to get it to send on form submit. Is there a public function that I can use for this?
PJSAndo replied on at Permalink Reply
I may just send a download link with the email. But I'm having a few other problems with the fields.

At the moment I have a form in view.php that is completed by the user. Fields include Firstname, Surname, and Email. How do I...

1/ include the Firstname and Surname in the body of the email, and send the email to the Email field? I'm guessing here I need to use this->????

2/ include a link to the brochure url in the body? I can define and display the url of the brochure field using the following code, but I can't figure out how to include this in the Body of the email.

$brochureurls = $tour->xpath('custom_fields/field[name="brochureurl"]/value');
                    foreach ($brochureurls as $brochureurl) { 
               echo $brochureurl

3/ send the email only when a successful form completion has been made and submitted? - I guess I need to use a public function like send() if something like that exists.

Here's the current code in controller.php
$mail = Loader::helper('mail');
        $mail->setBody('Please download your brochure here - my link goes here');
   $mail->to([email protected]');
   $mail->from([email protected]');

Thanks in advance
mhawke replied on at Permalink Reply
I'm a bit confused. Where is the view.php file you speak of?

Have you given any thought to using an 'External Form' block? Many developers don't like using it because it's not tied into the concrete5 dashboard but I have successfully used it lots of times. There is a very rudimentary 'Test' form already in this block and you can look at the view and controller at /public_html/concrete/blocks/external_form/form.