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

Defining email attachement as a variable


Go to End
Reply

10 Posts   2226 Views

Avatar
mschiefmaker

9 September 2009 at 7:47pm Community Member, 187 Posts

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

Avatar
mschiefmaker

11 September 2009 at 11:33am Community Member, 187 Posts

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

Avatar
mschiefmaker

15 September 2009 at 7:03am Community Member, 187 Posts

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

Thanks

MM

Avatar
mschiefmaker

16 September 2009 at 8:04pm Community Member, 187 Posts

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

Avatar
mschiefmaker

16 September 2009 at 9:28pm (Last edited: 16 September 2009 9:28pm), Community Member, 187 Posts

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

Avatar
dalesaurus

17 September 2009 at 3:13am Community Member, 283 Posts

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?

Avatar
mschiefmaker

18 September 2009 at 9:17am Community Member, 187 Posts

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

Avatar
mschiefmaker

19 September 2009 at 2:36pm Community Member, 187 Posts

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

Go to Top