通过SendGrid发送电子邮件

时间:2020-07-24 15:49:04

标签: javascript node.js sendgrid

我具有以下结构以SendGrid发送电子邮件:

import mail from '@sendgrid/mail';

function send(email_data) {
  mail.setApiKey(process.env.SENDGRID_API_KEY);

  const message = {
    to: email_data.to,
    from: email_data.from,
    subject: email_data.subject,
    text: email_data.text,
  };

  return mail.send(message);
}

export default send;

在我的控制器中,我按如下方式调用该方法:

const email_data = {
  to: 'myemail@hotmail.com',
  from: 'some_other_email@gmail.com',
  subject: 'Deposito realizado',
  text: 'Foi feito um deposito na sua conta ...',
};

mail.send(email_data);

但是向我显示以下错误:

(node:7963) UnhandledPromiseRejectionWarning: TypeError: _sendgrid2.default.send is not a function

2 个答案:

答案 0 :(得分:0)

'@sendgrid/mail'文件可能无法正常工作。您的错误消息:.default.send is not a function

答案 1 :(得分:0)

要呼叫const ModalScreen = () => ( <Modals.Navigator mode="modal"> <Modals.Screen name="Modal" component={Modal} options={({ route }) => ({ title: route.params.title, headerTransparent: true, gestureResponseDistance: { vertical: 500, }, })} /> </Modals.Navigator> ); const MainScreen = () => ( <Items.Navigator> <Items.Screen name="Main" component={Main} options={{ headerShown: false }} /> <Items.Screen name="Modal" component={ModalScreen} /> </Items.Navigator> ); // Navigating to Modal in another file. <TouchableOpacity onPress={() => navigation.navigate("Modal", { screen: "Modal", title: title, }) } > ,您必须在控制器中导入邮件

尝试一下:控制器

mail.send

发送网格

 import mail from '@sendgrid/mail';
 const email_data = {
   to: 'myemail@hotmail.com',
   from: 'some_other_email@gmail.com',
   subject: 'Deposito realizado',
   text: 'Foi feito um deposito na sua conta ...',
 };

 mail.send(email_data);