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

[eluser]Arjen van Bochoven[/eluser]
Maybe a feature request item:

I found it is not possible sending email atttachments that are not files. I want to send a .csv file that is generated from form data. I found it somewhat clunky to generate an intermediary file to get the attachment to be sent. It would be nice if the CI email class would accept a string or array as attachment.

Something like:

Code:
$this->email->addstringattachment( string, filename )

It is hard to subclass Email.php to add this feature because fetching file attachments is hardcoded into _build_message().

Anyone solved this?
#2

[eluser]Colin Williams[/eluser]
It has to be a file for sendmail or whichever email program on your server to actually attach it. Perhaps the email class could handle the file creation, but IDK, how hard is it to really do it yourself?

Code:
$csv_data = "Paul,Peter,Jacob\nJohn,Paul,Rogers";
$filename = 'path/to/new/file.csv';
if(file_put_contents($filename, $csv_data))
{
   $this->email->attach($filename);
}
#3

[eluser]Arjen van Bochoven[/eluser]
If it is not necessary, why create a file? I have the data in a variable, the email class reads in the data in a variable, why not skip the file creation? Also, if I have to create a file, I have to make sure I have a place where I can write it.

I got the idea from the email class I used before: html.mime.mail found on html mime mail

One of the features is: "Add attachments or embed images from files, or PHP strings (eg for images/files held in databases)"
#4

[eluser]Daniel Walton[/eluser]
I agree that this would make a good feature. Mandatory file creation would just hit the filesystem I/O unnecessarily.
#5

[eluser]Colin Williams[/eluser]
Does a file not have to be created at some point though? Whether you or the Email Lib does it, it's gonna happen, no?
#6

[eluser]Arjen van Bochoven[/eluser]
No file creation necessary. An attachment is just a base64 encoded string, seperated by a multipart boundary.
#7

[eluser]Colin Williams[/eluser]
Thanks, Arjen. I was hoping to be clarified on that. I admit ignorance in this realm, so this is good to know!

So, back to the issue at hand, this appears to be a rather trivial task. If you require it right now, go ahead and add the method (or overload the attach() method) to application/libraries/MY_Email.php. Once you get it tested and working (really, really well) open up a bug with the proposed fix/feature addition. I guarantee it will get into core at some point, but in the meantime, the community can benefit from it.
#8

[eluser]Arjen van Bochoven[/eluser]
Overloading Email.php is not going to be an easy task. As I noted in my first post, attachment handling is done in _build_message(), which is one of the largest handlers in the class. It would literally mean rewriting a large part of the Email class.

I would like to give it a shot, but I'm pressed for time at the moment (aren't we all?).

Thanks for your replies,

Arjen




Theme © iAndrew 2016 - Forum software by © MyBB