通过React Native中的mailgun发送电子邮件中的附件

问题描述 投票:4回答:1

我正在使用react native来构建移动应用程序,但是当我尝试发送电子邮件时遇到问题。它不发送附件。要发送电子邮件,我正在使用mailgun和axios。

import image from '../../assets/image.jpg';

axios({
      method: 'post',
      url: `${config.MAILGUN.baseUrl}/${config.MAILGUN.domain}/messages`,
      auth: {
        username: 'api',
        password: config.MAILGUN.apiKey
      },
      params: {
        from,
        to,
        subject,
        text,
        attachment: image
      }
    }, {
        'Content-type': 'multipart/form-data'
      }).then(
        response => {
          console.log('=========== RESPONSE =============', response);
          this.setState({ ...EQ_SATE });
        },
        reject => {
          console.log('=========== REJECT =============', reject);
        }
      )

此外,我试图发送这样的附件,也没有工作

attachment: {
   path: image,
   filename: test.jpg
}
api react-native attachment axios mailgun
1个回答
0
投票

您可以使用此代码使用fetch执行此操作

const imageUri = '<YOUR IMAGE URI>'

try {
  const formData = new FormData()
  formData.append('to', '<A TO ADDRESS>')
  formData.append('from', '<A FROM ADDRESS>')
  formData.append('text', 'Your content of email')
  formData.append('attachment', { uri: imageUri, name: 'image.png', type: 'image/png' })

  const data = await fetch(`https://api.mailgun.net/v3/<YOUR DOMAIN>/messages`, {
    method: 'POST',
    headers: {
      'Authorization': '<YOUR AUTH>',
      'Content-Type': 'multipart/form-data'
    },
    body: formData,
  })
} catch (err) {
  console.log("failed", err)
}
© www.soinside.com 2019 - 2024. All rights reserved.