Jump to:

23490 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Defining email attachement as a variable

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 2
Go to End
Author Topic: 2202 Views
  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Defining email attachement as a variable Link to this post

    I want to change the attachment that is sent on an automated email by selecting the filename from a table. In simple form I want to do this.

    $filename = DataObject::get_by_id("MonkeysFist","ID=1");
       $emailToSubmiter->attachFile('assets/Uploads/mf_attachments/'$filename.FileName, '$filename.FileName');

    I know I have a couple of problems with this because even if I use
    $emailToSubmiter->attachFile('assets/Uploads/mf_attachments/test.pdf, $filename.FileName);

    The filename is given as FileName rather than its actual value.

    Any thoughts, thanks

    MM

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Defining email attachement as a variable Link to this post

    so it should be $filename->FileName not with the . as I originally quoted it but still not working.

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Defining email attachement as a variable Link to this post

    I am still scratching my head over this one. Has anyone got any ideas?

    Thanks

    MM

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Defining email attachement as a variable Link to this post

    There were two problems with it
    1. The use of the property name FileName. Changed this to MFFileName
    2. I was not using get_by_id properly. I was writing it as a filter

    It works as

    $filename = DataObject::get_by_id("MonkeysFist", 6);
    $emailToSubmiter->attachFile('assets/Uploads/mf_attachments/ItsMyLifeCounselling.pdf', $filename->MFFileName);

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Defining email attachement as a variable Link to this post

    So I have solved part of the problem but I need to make the filepath variable as well so instead of
    $filename = DataObject::get_by_id("MonkeysFist", 7);
        $emailToSubmiter->attachFile('assets/Uploads/mf_attachments/ItsMyLifeCounselling.pdf', $filename->MFFileName);

    It needs to look something like this
    $filename = DataObject::get_by_id("MonkeysFist", 7);
        $emailToSubmiter->attachFile('assets/Uploads/mf_attachments/'$filename->MFFileName, $filename->MFFileName);

    but this errors with "Parse error: syntax error, unexpected T_VARIABLE in /var/www/html/SilverStripe/userforms/code/UserDefinedForm.php on line 404" so I tried

    $filename = DataObject::get_by_id("MonkeysFist", 7);
        $emailToSubmiter->attachFile('assets/Uploads/mf_attachments/'.$filename->MFFileName, $filename->MFFileName);
    it doesn't error but I no longer get an attachment.

    Any ideas how I should format it?

    Thanks
    MM

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Defining email attachement as a variable Link to this post

    Well the attachFile function runs file_get_contents() on what you pass and the encoding is done in /sapphire/email/Mailer.php.

    -First try getting the path of your file with $filename->getFullPath();
    -Second, be sure to specify a MIME type as the third arg of attachFile($filename, $attachedFilename = null, $mimetype = null)

    If you have an older version of php, try mime_content_type(): http://us3.php.net/manual/en/function.mime-content-type.php
    In 5.3+ use Fileinfo(): http://us3.php.net/manual/en/ref.fileinfo.php

    Also have you checked your headers to be sure some MTA or virus scanner isn't stripping your attachments along the way?

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Defining email attachement as a variable Link to this post

    Sorry I shouldn't have said I no longer get the attachment. It is there but it is not accessible as (I am assuming) the filepath is not correct. I changed it to

    $filename = DataObject::get_by_id("MonkeysFist", 6);
    $filepath = "assets/Uploads/mf_attachments/$filename->MFFilename";
    $emailToSubmiter->attachFile($filepath, $filename->MFFileName);

    and it works

    Thanks

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Defining email attachement as a variable Link to this post

    Spoke to soon.

    There is an appropriately named attachment but its is only 1K in size i.e. there is no actual file there so the filename is still wrong.

    Any thoughts on what to try next?

    Thanks

    MM

    2202 Views
Page: 1 2
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.