我正在构建一个flutter web,我必须通过电子邮件将表单数据发送到我的Gmail电子邮件地址How Can I。请帮助我。我有用户"mailer 3.0.4"和flatter_email_sender:^2.2.2但他们都不起作用。。。这是我的代码:
// Perform login or signup
Future<void> _validateAndSubmitForInformationForm() async {
print('1');
final MailOptions mailOptions = MailOptions(
body: 'a long body for the email <br> with a subset of HTML',
subject: 'the Email Subject',
recipients: ['bc160201844@vu.edu.pk'],
isHTML: true,
bccRecipients: ['bc160201844@vu.edu.pk'],
ccRecipients: ['bc160201844@vu.edu.pk'],
// attachments: [
// 'path/to/image.png',
// ],
);
print('3');
await FlutterMailer.send(mailOptions);
print('2');
}
您可以使用类似SendGrid的东西从flutter mobile发送电子邮件,内容如下:对不起格式不正确。
import 'package:http/http.dart' as http;
class SendGridUtil {
static sendRegistrationNotification(String email) async {
Map<String, String> headers = new Map();
headers["Authorization"] =
"Bearer $$$SENDGRIDAPIKEY$$$";
headers["Content-Type"] = "application/json";
var url = 'https://api.sendgrid.com/v3/mail/send';
var response = await http.post(url,
headers: headers,
body:
"{n "personalizations": [n {n "to": [n {n "email": "jerrod@liftaixxx.com"n },n {n "email": "darran@gmailxxx.com"n }n ]n }n ],n "from": {n "email": "app@liftaixxx.com"n },n "subject": "New user registration",n "content": [n {n "type": "text/plain",n "value": "New user register: $email"n }n ]n }");
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
}
}
要从flutter web发送电子邮件,您可以使用类似firebase云的功能-这是一个在firebase auth:中创建新用户时执行的功能
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
const sgMail = require('@sendgrid/mail')
admin.initializeApp(functions.config().firebase);
exports.sendWelcomeEmail = functions.auth.user().onCreate(user => {
console.log("User with email created: " + user.email);
sgMail.setApiKey("$$$SENDGRIDKEY$$$");
const liftAiMsg = {
to: 'jerrod@liftaixxx.com',
from: 'app@liftaixxx.com',
subject: 'New user created',
text: 'New user created with email: ' +user.email,
html: "<strong>New user created with email: "+user.email+"</strong>",
};
sgMail.send(liftAiMsg);
const customerMsg = {
to: user.email,
from: 'app@liftaixxx.com',
subject: 'Welcome to LiftAI',
text: 'Welcome to LiftAI',
html: '<strong>Welcome to LiftAI!</strong>',
};
sgMail.send(customerMsg);
});
EDIT:这不起作用,因为SendGrid(和MailJet(被设计为只在服务器上工作,而不在客户端上工作。
正如@dazza500所说,你需要:
1( 在注册https://app.sendgrid.com/
2( 创建API密钥
3(可选:检查文档(https://sendgrid.com/docs/API_Reference/Web_API_v3/index.html)
4(并使用此代码(用API密钥替换SENDGRIDAPIKEY(:
import 'package:http/http.dart' as http;
class SendGridUtil {
static sendRegistrationNotification(String email) async {
Map<String, String> headers = new Map();
headers["Authorization"] =
"Bearer SENDGRIDAPIKEY";
headers["Content-Type"] = "application/json";
var url = 'https://api.sendgrid.com/v3/mail/send';
var response = await http.post(url,
headers: headers,
body:
"{n "personalizations": [n {n "to": [n {n "email": "jerrod@liftaixxx.com"n },n {n "email": "darran@gmailxxx.com"n }n ]n }n ],n "from": {n "email": "app@liftaixxx.com"n },n "subject": "New user registration",n "content": [n {n "type": "text/plain",n "value": "New user register: $email"n }n ]n }");
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
}
}
您可以用您的风格语言创建一个特定的(用例如的令牌保护的(WebAPI,然后用适当的参数对其进行简单的POST调用。
在Flutter中:
Future sendEmail(elementType elementoSent) async {
var body = "Blabla, " + elementoSent.prpoerties;
try {
await http.post(
"https://yourapiUrl.net/api/method",
headers: {
'Content-type': 'application/json',
'Accept': 'application/json'
},
body: jsonEncode({"emailbody": '$body'}));
} catch (e) {
print(e);
}
}
这是一个Web API代码组,例如(在C#中(:
// POST api/<EmailsController>
[HttpPost]
public void Post([FromBody] EmailsModel model)
{
if (model.Typemail == "1")
_emailSender.SendEmailtoUserAsync("mail@gmail.com", "mail object", model.EmailBody);
}
_emailSender.SendEmailtoUserAsync
使用特定的或外部的邮件服务,如MailJet或SendGrid。