繁体   English   中英

发送电子邮件到当前用户电子邮件

[英]send email to current user email in flutter

在此代码中,将电子邮件发送到特定电子邮件,但我想将电子邮件发送到当前用户电子邮件,我将我的项目与 firebase 连接。

      sendMail() async {
      String username = 'example@gmail.com';
      String password = 'axneqgkraxm';

      final smtpServer = gmail(username, password);

      final message = Message()
    ..from = Address(username, 'testing')
    ..recipients.add('uu500oi@gmail.com')
    ..subject = 'Receipt :: ${DateTime.now()}'

    ..text = 'This is the plain text.\nThis is line 2 of the text part.'

    ..html = html
        .replaceFirst('{{title}}', 'this will be the title')
        .replaceFirst('{{price}}', '20')
        .replaceFirst('{{hours}}', '2:00 PM')
        .replaceFirst('{{description}}', 'this is description');

     try {
     final sendReport = await send(message, smtpServer);

    print('Message sent: ' + sendReport.toString());
      } on MailerException catch (e) {
    print(e);
    print('Message not sent.');

    for (var p in e.problems) {
      print('Problem: ${p.code}: ${p.msg}');
          }
      }

谁能解决???

您可以通过 Firebase 身份验证获取当前用户的电子邮件地址:

if (FirebaseAuth.instance.currentUser != null) {
  print(FirebaseAuth.instance.currentUser?.email);
}

另请参阅有关获取当前用户配置文件的 Firebase 文档和 Firebase 的User对象的参考文档。

试试下面的代码:

sendMail() async {
  if (FirebaseAuth.instance.currentUser != null) {
    String username = 'example@gmail.com';
    String password = 'axneqgkraxm';

    final smtpServer = gmail(username, password);

    final message = Message()
      ..from = Address(username, 'testing')
      ..recipients.add(FirebaseAuth.instance.currentUser?.email)
      ..subject = 'Receipt :: ${DateTime.now()}'
      ..text = 'This is the plain text.\nThis is line 2 of the text part.'
      ..html = html
        .replaceFirst('{{title}}', 'this will be the title')
        .replaceFirst('{{price}}', '20')
        .replaceFirst('{{hours}}', '2:00 PM')
        .replaceFirst('{{description}}', 'this is description');

    try {
      final sendReport = await send(message, smtpServer);

      print('Message sent: ' + sendReport.toString());
    } on MailerException catch (e) {
      print(e);
      print('Message not sent.');

      for (var p in e.problems) {
        print('Problem: ${p.code}: ${p.msg}');
      }
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM