<p>You can do this with a simple <a href="https://craftcms.com/docs/plugins/introduction" rel="nofollow noreferrer">Craft plugin</a>.  You can generate the boilerplate plugin code <a href="https://pluginfactory.io" rel="nofollow noreferrer">here</a>.</p>

<p>Your form on the front-end will set its hidden action input to your plugin's <a href="https://craftcms.com/docs/plugins/controllers" rel="nofollow noreferrer">controller's</a> <a href="https://craftcms.com/docs/plugins/controllers#posting-to-controller-actions" rel="nofollow noreferrer">action</a>.</p>

<p>From there, you can use Craft's <a href="https://craftcms.com/classreference/models/EmailModel" rel="nofollow noreferrer">EmailModel</a> (along with <a href="https://craftcms.com/classreference/models/EmailModel#addAttachment-detail" rel="nofollow noreferrer"><code>addAttachment()</code></a>) and <a href="https://craftcms.com/classreference/services/EmailService" rel="nofollow noreferrer">EmailService</a>'s <a href="https://craftcms.com/classreference/services/EmailService#sendEmail-detail" rel="nofollow noreferrer"><code>sendEmail()</code></a> to send whatever email contents you wish.</p>

CC BY-SA 3.0
2017-04-19T22:05:03.853Z
I just spent a few days going through Mijingo's Craft Plugin Course.  So, I can now attack this and see if I can make it work.
Chip Shank
CC BY-SA 3.0
2017-04-23T14:39:42.983Z
Does addAttachment work ok on local host urls?  I have the following: 
$pubpath = /localhost:3000/uploads/pubfiles/Lampen2014.pdf   .................                    
$email->addAttachment($pubpath); ................                          
Email error: Could not access file: //localhost:3000/uploads/pubfiles/Lampen2014.pdf
Chip Shank
CC BY-SA 3.0
2017-04-23T14:45:06.320Z
As I typed I realized I needed to remove the localhost from the path to /uploads..... etc... and it works fine.  Now I just need to remove the first part, is there a quick truncate left method or something like that?  Thanks!
Chip Shank
CC BY-SA 3.0
2017-04-09T23:09:19.543Z
2017-04-09T23:12:51.343Z
2017-04-09T23:12:51.343Z
Brad Bell
<p>I am coming from a .net/C# background and we have recently moved to a Craft/Twig platform.  What is the best way to do this: Have a visitor come to a Craft webpage (Twig file), enter their email address in a text box, hit a button, and have an email message sent to them containing a PDF attachment that they selected from that page?</p>

<p>Is this functionality built-in to Craft or do I need a plugin?</p>

<emails>
What is the best way to generate email messages with attachments from Twig files?
# What is the best way to generate email messages with attachments from Twig files?

(Asked by: username_0 on 2017-04-09)

I am coming from a .net/C# background and we have recently moved to a Craft/Twig platform. What is the best way to do this: Have a visitor come to a Craft webpage (Twig file), enter their email address in a text box, hit a button, and have an email message sent to them containing a PDF attachment that they selected from that page?

Is this functionality built-in to Craft or do I need a plugin?

**Tags:** emails


---

## 1 answers

### Answer by username_1 on 2017-04-09. Score: 2 | Accepted answer

You can do this with a simple [Craft plugin](https://craftcms.com/docs/plugins/introduction). You can generate the boilerplate plugin code [here](https://pluginfactory.io).

Your form on the front-end will set its hidden action input to your plugin's [controller's](https://craftcms.com/docs/plugins/controllers) [action](https://craftcms.com/docs/plugins/controllers#posting-to-controller-actions).

From there, you can use Craft's [EmailModel](https://craftcms.com/classreference/models/EmailModel) (along with [`addAttachment()`](https://craftcms.com/classreference/models/EmailModel#addAttachment-detail)) and [EmailService](https://craftcms.com/classreference/services/EmailService)'s [`sendEmail()`](https://craftcms.com/classreference/services/EmailService#sendEmail-detail) to send whatever email contents you wish.

> **Comments:**
> **username_0**: I just spent a few days going through Mijingo's Craft Plugin Course.  So, I can now attack this and see if I can make it work.  
> **username_0**: Does addAttachment work ok on local host urls?  I have the following:  $pubpath = /localhost:3000/uploads/pubfiles/Lampen2014.pdf   .................                     $email->addAttachment($pubpath); ................                           Email error: Could not access file: //localhost:3000/uploads/pubfiles/Lampen2014.pdf  
> **username_0**: As I typed I realized I needed to remove the localhost from the path to /uploads..... etc... and it works fine.  Now I just need to remove the first part, is there a quick truncate left method or something like that?  Thanks!  

---

