Welcome Guest, Not a member yet? Register   Sign In
Codeigniter Email attachment from a string
#1

[eluser]LAMP Coder[/eluser]
Hi, I was wondering if it was possible to attach a file using the email library from a string variable?
I have the attachment stored in a variable and i want to assign that and name of the file to the email attach method

i.e.
Code:
$this->email->attach('data','filename.pdf');

Is that possible?
#2

[eluser]iloveci[/eluser]
you might want to place a string that contains the location of the file. it will work then.
#3

[eluser]LAMP Coder[/eluser]
[quote author="iloveci" date="1288088027"]you might want to place a string that contains the location of the file. it will work then.[/quote]

I don't think you understand my question. The file itself doesn't exist. It's stored in a variable and is a dynamic pdf file which is modified from php.
#4

[eluser]Johnathan1707[/eluser]
I had to do this recently, what I done was saved the file somewhere temporarily, attached it then on email success I deleted the file. Worked a treat for me.
#5

[eluser]LAMP Coder[/eluser]
I was hoping that I won't have to do it. I guess I'll just extend the email class
#6

[eluser]Unknown[/eluser]
If anyone was interested, I have done something like this recently for *.ics files (vCalendar attachments)

I generate .ics calendar files using a heredoc in a function, so I end up with an ics file in a string

Code:
$calendar_attachment= <<<VCAL
BEGIN:VCALENDAR
PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN
VERSION:2.0
METHOD:PUBLISH
BEGIN:VEVENT
ORGANIZER:MAILTO:{$mail_to}
DTSTART: {$d_start}
DTEND: {$d_end}
LOCATION: {$location}
TRANSP:OPAQUE
SEQUENCE:0
UID: {$uid}
DTSTAMP: {$vStamp}
DESCRIPTION: {$description}
SUMMARY: {$summary}
PRIORITY:5
X-MICROSOFT-CDO-IMPORTANCE:1
CLASS:PUBLIC
BEGIN:VALARM
TRIGGER:-PT10080M
ACTION:DISPLAY
END:VALARM
END:VEVENT
END:VCALENDAR
VCAL;


The changes I made to Email.php are in the following pastie: pastie.org/1650998

The changes I made are on these lines:
Line 83 to 87
Line 180 to 183
Line 412 to 419
Line 680 to 682
Line 688 to 691
Line 1189 to 1209
Line 1215
Line 1221

The function is string_attach and the usage is like this when building an email:

Code:
$this->email->string_attach($string_file, $file_name, $mime , $disposition ) ;

Example:
Code:
$mime = "text/x-vCalendar";
$this->email->string_attach($calendar_attachment, "Session.ics", $mime, "attachment");

I have only had time for limited testing - so far only the html emails have been tested and seem to work as expected. I hope this is useful, or that someone will be able to build on what I did. Apologies if the code is a little hack-like, I'm not all that experienced with PHP. I hope this will suffice until someone is able to make a neater version
#7

[eluser]Unknown[/eluser]
@PraetorianXX: Good work! I took what you did and cleaned it up a little so it reuses more of the existing code. Basically in my version the _attach_name, _attach_type, etc arrays are shared by both file attachments and string attachments, a new array _attach_source identifies whether the attachment source is file or string, and _attach_content stores the file contents of string attachments.

I've kept the same function signature as you, ie
Code:
$this->email->string_attach($str_file, $filename, $mime, $disposition);

Mine is based on Codeigniter 2.0. Look for ~aidanf in the file for my changes.
http://pastie.org/2111065




Theme © iAndrew 2016 - Forum software by © MyBB