簡體   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