Coco
Coco

Reputation: 87

Send mail from Flutter Web

Hi I want to send mail to the registered email from Flutter Web. I wish to send the random security code to the new registered user for their first time login. The recipient email is taken from the TextFormField. I have referred the mailer pub.dev and the example code. Below is the code I added and the result I got. I didn't do any other configuration for this implementation.

                    final smtpServer = gmail('email address', 'password');
                    final message = Message()
                      ..from = Address('email address', 'Your Name')
                      ..recipients.add(_emailText.text)
                      ..subject = 'Test Dart Mailer library :: 😀 :: ${DateTime.now()}'
                      ..text = 'This is the plain text.\nThis is line 2 of the text part.'
                      ..html = "<h1>Test</h1>\n<p>Hey! Here's some HTML content</p>";

                    try {
                      final sendReport = await send(message, 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}');
                      }
                    }

                    var connection = PersistentConnection(smtpServer);

                    // Send the first message
                    await connection.send(message);

                    // close the connection
                    await connection.close();

This is the error I got:

Error: Unsupported operation: Socket constructor
    at Object.throw_ [as throw] (http://localhost: )
    at Function._connect (http://localhost: )
    at Function.connect (http://localhost: )
    at connection.Connection.new.connect (http://localhost: )
    at connect.next (<anonymous>)
    at runBody (http://localhost: )
    at Object._async [as async] (http://localhost: )
    at connection.Connection.new.connect (http://localhost: )
    at connect (http://localhost: )
    at connect.next (<anonymous>)
    at runBody (http://localhost: )
    at Object._async [as async] (http://localhost: )
    at Object.connect (http://localhost: )
    at send (http://localhost: )
    at send.next (<anonymous>)
    at runBody (http://localhost: )
    at Object._async [as async] (http://localhost: )
    at Object.send (http://localhost: )
    at signUpForm._SignUpFormState.new.<anonymous> (http://localhost: )
    at Generator.next (<anonymous>)
    at http://localhost:
    at _RootZone.runUnary (http://localhost: )
    at _FutureListener.thenAwait.handleValue (http://localhost: )
    at handleValueCallback (http://localhost: )
    at Function._propagateToListeners (http://localhost: )
    at _Future.new.[_completeWithValue] (http://localhost: )
    at async._AsyncCallbackEntry.new.callback (http://localhost: )
    at Object._microtaskLoop (http://localhost: )
    at _startMicrotaskLoop (http://localhost: )
    at http://localhost: 

I couldn't find any relevant solution on it. Did I miss any thing or there is easier way to implement this? Hope someone can help. Thanks a lot!

Upvotes: 1

Views: 936

Answers (1)

Tarik Huber
Tarik Huber

Reputation: 7388

Even you use Flutter when you generate a Web App using it everything that moves things happens with JavaScript. I would strongly recomment not to put any email credentials to JavaScript code!!!

To awoid that just create a callable Firebase function that you can call from your native app and Web app to send the email. You can have there a payload with the email content and the function also gets the auth data in the context. You also get a response if the sending failed or successed. In the callable cloud function you can then use normal JS code and packages to send the email like nodemailer.

Upvotes: 2

Related Questions