【问题标题】:How to send direct email by Flutter web如何通过 Flutter web 发送直接电子邮件
【发布时间】:2020-06-19 17:36:47
【问题描述】:

我正在构建一个颤动的网络,我必须通过电子邮件将表单数据发送到我的 Gmail 电子邮件地址,我该怎么做。请帮我。 我有用户“mailer 3.0.4”和flutter_email_sender:^2.2.2 但他们都没有工作...... 这是我的代码:

  // Perform login or signup
  Future<void> _validateAndSubmitForInformationForm() async {
    print('1');
    final MailOptions mailOptions = MailOptions(
      body: 'a long body for the email <br> with a subset of HTML',
      subject: 'the Email Subject',
      recipients: ['bc160201844@vu.edu.pk'],
      isHTML: true,
      bccRecipients: ['bc160201844@vu.edu.pk'],
      ccRecipients: ['bc160201844@vu.edu.pk'],
//      attachments: [
//        'path/to/image.png',
//      ],
    );
    print('3');

    await FlutterMailer.send(mailOptions);
    print('2');
  }

【问题讨论】:

    标签: flutter flutter-dependencies flutter-web


    【解决方案1】:

    您可以使用 SendGrid 之类的工具从 Flutter Mobile 发送电子邮件,内容如下:抱歉格式错误。

    import 'package:http/http.dart' as http;
    
    class SendGridUtil {
      static sendRegistrationNotification(String email) async {
        Map<String, String> headers = new Map();
        headers["Authorization"] =
            "Bearer $$$SENDGRIDAPIKEY$$$";
        headers["Content-Type"] = "application/json";
    
        var url = 'https://api.sendgrid.com/v3/mail/send';
        var response = await http.post(url,
            headers: headers,
            body:
                "{\n          \"personalizations\": [\n            {\n              \"to\": [\n                {\n                  \"email\": \"jerrod@liftaixxx.com\"\n                },\n                {\n                  \"email\": \"darran@gmailxxx.com\"\n                }\n              ]\n            }\n          ],\n          \"from\": {\n            \"email\": \"app@liftaixxx.com\"\n          },\n          \"subject\": \"New user registration\",\n          \"content\": [\n            {\n              \"type\": \"text\/plain\",\n              \"value\": \"New user register: $email\"\n            }\n          ]\n        }");
        print('Response status: ${response.statusCode}');
        print('Response body: ${response.body}');
      }
    }
    

    要从 Flutter Web 发送电子邮件,您可以使用类似 firebase 云功能的功能 - 这是在 firebase auth 中创建新用户时执行的功能:

    import * as functions from 'firebase-functions';
    import * as admin from 'firebase-admin';
    const sgMail = require('@sendgrid/mail')
    
    admin.initializeApp(functions.config().firebase);
    
    exports.sendWelcomeEmail = functions.auth.user().onCreate(user => {
    
    console.log("User with email created: " + user.email);
    
    sgMail.setApiKey("$$$SENDGRIDKEY$$$");
    const liftAiMsg = {
      to: 'jerrod@liftaixxx.com',
      from: 'app@liftaixxx.com',
      subject: 'New user created',
      text: 'New user created with email: ' +user.email,
      html: "<strong>New user created with email:  "+user.email+"</strong>",
    };
    
    sgMail.send(liftAiMsg);
    
    const customerMsg = {
      to: user.email,
      from: 'app@liftaixxx.com',
      subject: 'Welcome to LiftAI',
      text: 'Welcome to LiftAI',
      html: '<strong>Welcome to LiftAI!</strong>',
    };
    
    sgMail.send(customerMsg);
    
    
    });
    

    【讨论】:

    • 我们为这段代码使用了哪个包。对不起,我不明白。从'firebase-functions'导入*作为函数;从'firebase-admin'导入*作为管理员; const sgMail = require('@sendgrid/mail') 对于这些导入
    • 这些是可通过 npm 获得的节点包
    【解决方案2】:

    编辑:这行不通,因为 SendGrid(和 MailJet)被设计为只能在服务器上工作,而不是在客户端上工作。

    正如@dazza500 所说,您需要:

    1) 注册https://app.sendgrid.com/
    2) 创建 API 密钥
    3) 可选:查看文档 (https://sendgrid.com/docs/API_Reference/Web_API_v3/index.html)
    4) 并使用此代码(将 SENDGRIDAPIKEY 替换为您的 API 密钥):

    import 'package:http/http.dart' as http;
    
    class SendGridUtil {
      static sendRegistrationNotification(String email) async {
        Map<String, String> headers = new Map();
        headers["Authorization"] =
            "Bearer SENDGRIDAPIKEY";
        headers["Content-Type"] = "application/json";
    
        var url = 'https://api.sendgrid.com/v3/mail/send';
        var response = await http.post(url,
            headers: headers,
            body:
                "{\n          \"personalizations\": [\n            {\n              \"to\": [\n                {\n                  \"email\": \"jerrod@liftaixxx.com\"\n                },\n                {\n                  \"email\": \"darran@gmailxxx.com\"\n                }\n              ]\n            }\n          ],\n          \"from\": {\n            \"email\": \"app@liftaixxx.com\"\n          },\n          \"subject\": \"New user registration\",\n          \"content\": [\n            {\n              \"type\": \"text\/plain\",\n              \"value\": \"New user register: $email\"\n            }\n          ]\n        }");
        print('Response status: ${response.statusCode}');
        print('Response body: ${response.body}');
      }
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用您的风格语言创建特定的(例如使用令牌保护的)WebAPI,然后使用适当的参数对其进行简单的 POST 调用。

      在颤振中:

      Future sendEmail(elementType elementoSent) async {
          var body = "Blabla, " + elementoSent.prpoerties;
      
          try {
              await http.post(
                  "https://yourapiUrl.net/api/method",
                  headers: {
                      'Content-type': 'application/json',
                      'Accept': 'application/json'
                  },
                  body: jsonEncode({"emailbody": '$body'}));
          } catch (e) {
              print(e);
          }
      }
      

      这是一个 Web API 代码块,例如(在 C# 中):

      // POST api/<EmailsController>
      [HttpPost]
      public void Post([FromBody] EmailsModel model)
      {
          if (model.Typemail == "1")
              _emailSender.SendEmailtoUserAsync("mail@gmail.com", "mail object", model.EmailBody);
      }
      

      _emailSender.SendEmailtoUserAsync 使用特定的或外部的邮件服务,例如 MailJet 或 SendGrid。

      【讨论】:

        猜你喜欢
        • 2021-01-10
        • 1970-01-01
        • 1970-01-01
        • 2016-04-08
        • 1970-01-01
        • 1970-01-01
        • 2011-11-14
        • 1970-01-01
        • 2020-03-04
        相关资源
        最近更新 更多