Home > Back-end >  send email to current user email in flutter
send email to current user email in flutter

Time:12-20

In this code send email to specific email but I want send email to current user email I connect my project with firebase.

      sendMail() async {
      String username = '[email protected]';
      String password = 'axneqgkraxm';

      final smtpServer = gmail(username, password);

      final message = Message()
    ..from = Address(username, 'testing')
    ..recipients.add('[email protected]')
    ..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}');
          }
      }

anyone can solve it???

CodePudding user response:

You can get the email address of the current user from Firebase Authentication with:

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

Also see the Firebase documentation on getting the current user profile and the reference document for Firebase's User object.

CodePudding user response:

Try the following code:

sendMail() async {
  if (FirebaseAuth.instance.currentUser != null) {
    String username = '[email protected]';
    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}');
      }
    }
  }
}
  • Related