skip to Main Content

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???

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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}');
          }
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search