Flutter "Mailer" API Multiple Image Attachments
You need to wrap your file with FileAttachment
..attachments.add(FileAttachment(_image1))
Laegh
Updated on December 14, 2022Comments
-
Laegh over 1 year
I'm making a simple app for getting personal information from the user and number of images to send them through backend mail API with a one click of a button. So far, I can get and send the FormData through mail but I couldn't figure it out the how to send an array of images.
I have tried several API's but "Mailer" seems to best for SMTP. As for the code, I tried to convert the "File" class to String or List but none of those have worked for me. I'am not a intermediate coder so be kind with me :)
That's how I get the images using "image_picker"
File _image1; Future getImage1Camera() async { var image1 = await ImagePicker.pickImage(source: ImageSource.camera); setState(() { _image1 = image1; }); }
And the "mailer" code
void _mailer() async{ if(!_formKey.currentState.validate()){ return; }else{ _formKey.currentState.save(); } String gmailUsername = '**'; String gmailPassword = '**'; final smtpServer = gmail(gmailUsername, gmailPassword); final ceSendMail = Message() ..from = Address(gmailUsername, '') ..recipients.add('recipent') ..subject = 'Test' ..text = 'Plain Text' ..html = ''//Form Data ..attachments.add(_image1);//TODO: User input images try { final sendReport = await send(cekSendMail, smtpServer); print('Message sent: ' + sendReport.toString()); } on MailerException catch (e) { print('Message not sent.'); for (var p in e.problems) { print('Problem: ${p.code}: ${p.msg}'); } } // Create a smtp client that will persist the connection var connection = PersistentConnection(smtpServer); // Send the message await connection.send(cekSendMail); // close the connection await connection.close(); }
This is the error I get and whatever I try it's always the "type" error.
The argument type 'File' can't be assigned to the parameter type 'Attachment'.
So, how can I get multiple image files from user and send through mail API?