[SOLVED]attachment swiftmailer from database

hello all,

im pretty new in using yii, can anyone please tell me how to make attachment from (blob field) in my database so when i send an email using swiftmailer that file from my database can be an attachment of the email. i already succeed in sending email using swiftmailer without attachment. but now i want attach file from database too, i already read how to attach file from specified folder from server but doesnt meet with my case that i want take a file from database…





$user=User::model()->findByPk(Yii::app()->user->id);

$message = new YiiMailMessage();

$message->setTo(array($user->email=>$user->userfullname));

$message->setFrom(array('crazysasshy@gmail.com'=>'staff support Bee'));

$message->setSubject('[#'.$model->ticketno.']'.$model->subject);

$message->setBody($pesan);

$message->attach

$message->attach(Swift_Attachment::fromPath( /*What to write here?<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />*/);

$numsent = Yii::app()->mail->send($message);



thanks

save the file to a temporary location, then after sent delete the temporary file

thanks Gustavo,

i already tried that and i can make the attachments, huraaayyy,

but… somehow i cant delete the temporary file…

here is my code:




	if($file!=null){

		$filename = $attachment->attfilename; //temporary file name

		$Content = $attachment->attdata;

		$handle = fopen($filename, 'x+');

		fwrite($handle, $Content);

	        fclose($handle);   


                $message->attach(Swift_Attachment::fromPath($filename)); //this is succeed

	        $delete ="C:/xampp/htdocs/yii/Support Suite/".$filename."";   //seems to be fail

		unlink($delete);

	}



i try "$delete = $filename;"

doesnt work either, maybe u know why? Thanks :D

the file is in use, you must delete after you send the email




$filename = $attachment->attfilename; //temporary file name

file_put_contents($filename,$attachment->attdata);

$message->attach(Swift_Attachment::fromPath($filename)); //this is succeed

//after send

unlink($filename);



thanks for ur reply :)

yeah u re right,

but i got another problem, permission denied, i search google and try everything such chmod etc, but still not working, im using windows 7, do u know why?




$numsent = Yii::app()->mail->send($message);

						if($file!=null){

						   	chmod($filename,0770);

							$a ="C:/xampp/htdocs/yii/Support Suite/".$filename."";

							unlink($a);

						}



thanks a lot :)

I wouldn’t know

apache must have permission to write to the file

try first change its owner to the apache user

also, looks like you are not deleting the right file

you use $filename for attachment, to file_put_contents, to chmod

and then you delete "C:/xampp/htdocs/yii/Support Suite/".$filename.""

be sure you are deleting the correct file

thanks again for ur reply :)

im sure im deleting the correct file,

i guess after sending a message the file is not closed,

here if i comment the "sending code", deleting the file is successfull without any permission denied




	$numsent = Yii::app()->mail->send($message);//the sending code i comment

		if($file!=null){

			chmod($filename,0755);

	        	$a =$filename;

			unlink($a);

		}



yeah im still working on it :)

i end up to delete the file not after i send an email, but after some period of time using try catch, well maybe a cron job is a gud option, but because i still working in localhost, i will try it later

problem solved…:D

Hi, I have to decide what extension to use for sending emails with PDF attachments, what do you recommend?

tnx