在此代码发送电子邮件到特定的电子邮件,但我想发送电子邮件到当前用户的电子邮件我用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}');
}
}
有人能解决吗??
尝试以下代码:
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}');
}
}
}
}
您可以通过以下命令从Firebase Authentication获取当前用户的电子邮件地址:
if (FirebaseAuth.instance.currentUser != null) {
print(FirebaseAuth.instance.currentUser?.email);
}
也请参阅Firebase文档获取当前用户配置文件和Firebase的User
对象的参考文档。