21311 Posts in 5739 Topics by 2604 members
| Go to End | Next > | |
| Author | Topic: | 1729 Views |
-
Defining email attachement as a variable

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
-
Re: Defining email attachement as a variable

11 September 2009 at 11:33am
so it should be $filename->FileName not with the . as I originally quoted it but still not working.
-
Re: Defining email attachement as a variable

15 September 2009 at 7:03am
I am still scratching my head over this one. Has anyone got any ideas?
Thanks
MM
-
Re: Defining email attachement as a variable

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 filterIt works as
$filename = DataObject::get_by_id("MonkeysFist", 6);
$emailToSubmiter->attachFile('assets/Uploads/mf_attachments/ItsMyLifeCounselling.pdf', $filename->MFFileName); -
Re: Defining email attachement as a variable

16 September 2009 at 9:28pm Last edited: 16 September 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 -
Re: Defining email attachement as a variable

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.phpAlso have you checked your headers to be sure some MTA or virus scanner isn't stripping your attachments along the way?
-
Re: Defining email attachement as a variable

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
-
Re: Defining email attachement as a variable

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
| 1729 Views | ||
| Go to Top | Next > |

