Jump to:

22977 Posts in 11806 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Attachment on email generated from a form

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

Page: 1
Go to End
Author Topic: 2276 Views
  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Attachment on email generated from a form Link to this post

    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

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Attachment on email generated from a form Link to this post

    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

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Attachment on email generated from a form Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Attachment on email generated from a form Link to this post

    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');

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Attachment on email generated from a form Link to this post

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

    MM

    2276 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.