Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Attachment on email generated from a form


Reply

7 Posts   2538 Views

Avatar
mschiefmaker

5 September 2009 at 11:28am Community Member, 187 Posts

Hi
I have a form which generates a response email to the submitter but I want this to include an attachment but I am not sure how to do this. I have included into the code file for the form but the email arrives without an attachment:

$From = $data['Email'];
$To = $data['Email'];
$Subject = "blah balh" ;
      $Attachment = "assets/Uploads/blahblah.pdf";
$email = new Email($From, $To, $Subject,'',$Attachment);

Does anyone know? Is there any documentation that might help me?

Thakns

MM

Avatar
mschiefmaker

5 September 2009 at 11:44am Community Member, 187 Posts

Apparently it should be an array of filenames

i.e. $Attachment = array("blahblah.pdf");

but then where do I put the blahblah.pdf file so that it knows where to get it from?

Thanks

MM

Avatar
mschiefmaker

5 September 2009 at 5:16pm Community Member, 187 Posts

Ok, so I was heading off down the wrong track. I think I am on the right on now but still no cigar.

There is a method in the email class, attachFile but I can't get it to work. I call it

$To = $data['Email'];
$Subject = "blahblah" ;
      $email = new Email($From, $To, $Subject);
//set template
$email->setTemplate('MFEmail');
//populate template
$email->populateTemplate($data);
   //attach file
   $email->attachFile("blahblah.pdf");
//send mail
$email->send();

but still no attachment on the email. Whats missing/wrong? I can't find any documentation on this either. Am happy to work it out for myself if I just knew where to look

Thanks

MM

Avatar
Willr

5 September 2009 at 6:56pm Forum Moderator, 5511 Posts

attachFile() is correct. This takes the filename from the base url so if your pdf is in your assets folder you would need to do

$email->attachFile('assets/file.pdf');

Avatar
mschiefmaker

6 September 2009 at 6:28pm Community Member, 187 Posts

Thanks, I thought I had already tried this but no. It works a treat.

MM

Avatar
luuk

8 September 2014 at 8:44am Community Member, 6 Posts

Is there a way to make this dynamic?

I have a file upload field which i would like to attach to a email.

Avatar
Willr

8 September 2014 at 9:30pm Forum Moderator, 5511 Posts

Yes this is possible. Have a look at UserForms for a reference

https://github.com/silverstripe/silverstripe-userforms/blob/master/code/model/UserDefinedForm.php#L924

And the email at
https://github.com/silverstripe/silverstripe-userforms/blob/master/code/model/UserDefinedForm.php#L974