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   2247 Views

Avatar
mschiefmaker

Community Member, 187 Posts

9 September 2009 at 7:47pm

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

Community Member, 187 Posts

11 September 2009 at 11:33am

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

Avatar
mschiefmaker

Community Member, 187 Posts

15 September 2009 at 7:03am

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

Thanks

MM

Avatar
mschiefmaker

Community Member, 187 Posts

16 September 2009 at 8:04pm

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

Community Member, 187 Posts

16 September 2009 at 9:28pm

Edited: 16/09/2009 9:28pm

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

Community Member, 283 Posts

17 September 2009 at 3:13am

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

Community Member, 187 Posts

18 September 2009 at 9:17am

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

Community Member, 187 Posts

19 September 2009 at 2:36pm

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