Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YiiMail sending attachment

Tags:

yii

In my Project i am using YiiMail extension to send mail to the users. in which i am attaching a file. but the problem is its not possible to send the mail using the attachment. my mail code is given below.

$this->email->setBody('<p>'.$email.'-'.$name.'-'.$details.'</p>', 'text/html');
$this->email->from = "[email protected]";
$this->email->setSubject('Direct Request');
$this->email->attach(CUploadedFile::getInstanceByName('fileupload'));
$this->email->setTo(array($emailId => '[email protected]'));

with this code the mail is not sending and error message is showing. Argument 1 passed to Swift_Mime_SimpleMessage::attach() must implement interface Swift_Mime_MimeEntity, instance of CUploadedFile given

what is reason this error is showing and any solution for this. thanks in advance

like image 462
Jaison Justus Avatar asked Feb 22 '23 13:02

Jaison Justus


1 Answers

You need to convert your file attachment to a SwiftMailer Swift_Mime_MimeEntity type. CUploadedFile::getInstanceByName('fileupload') returns a CUploadedFile class, which SwiftMailer does not know how to handle. More on Swift attachments here.

I have not tested this, but you will need to do something like this:

$uploadedFile = CUploadedFile::getInstanceByName('fileupload'); // get the CUploadedFile
$uploadedFileName = $uploadedFile->tempName; // will be something like 'myfile.jpg'
$swiftAttachment = Swift_Attachment::fromPath($uploadedFileName); // create a Swift Attachment
$this->email->attach($swiftAttachment); // now attach the correct type

Good luck!

like image 58
thaddeusmt Avatar answered Apr 25 '23 18:04

thaddeusmt