从表单发送电子邮件附件

时间:2018-10-02 05:33:00

标签: reactjs meteor

我正在尝试从流星应用程序中的表单发送带有附件的电子邮件。发送电子邮件很好,可以正常工作,但是,我对如何发送附件一无所知。我的表单使用户能够附加文件,但是,我不知道如何将文件传递给服务器以附件形式发送。

我已经看过流星documentation,但没有帮助。

它指向mailcompser 4

当我在服务器上console.log附件时,名称出现。它说它需要一条路,但是,我不知道那是什么。

有人可以告诉我我做错了什么吗?

路径:client

class EmailForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
    };

    this.fileInput = React.createRef();
  }

  handleSubmit(event) {
    event.preventDefault();

    const errors = jobApplicationValidation(this.state);

    const attachments = [
      {
        fileName: this.fileInput.current.files[0].name,
      },
    ];

    const attachments = [];

    attachments.push(this.fileInput.current.files[0]);

    Meteor.call(
      'sendEmail',
      this.props.email,
      this.props.myEmail,
      this.props.subject,
      this.props.text,
      this.fileInput.current.files[0],
    );
  }

  render() {
    return (
      <Form>
        <input
          type="file"
          ref={this.fileInput}
        />
        <Button onClick={this.handleSubmit}>Apply</Button>
      </Form>
    );
  }
}

路径:Server

Meteor.methods({
  sendJobApplicationEmail(to, from, subject, text, attachments) {
      // Make sure that all arguments are strings.
    check([to, from, subject, text], [String]);

    this.unblock();

    Email.send({ to, from, subject, text, attachments });
  },
});

1 个答案:

答案 0 :(得分:0)

帅气的威尔森来营救:

  

Meteor的默认电子邮件程序包3在其后面的mailcomposer 14   场景。确保您的附件数组遵循以下内容中列出的选项   mailcomposer的附件文档40。在您的示例中,您引用的是   PDF,因此您可能希望附件数组看起来像   喜欢:

 ... attachments: [   {
     fileName: 'book.pdf',
     filePath: '/home/hexen/Downloads/book.pdf',   }, ]

https://forums.meteor.com/t/meteor-email-with-attachment/23026

但是我们在评论中的伙伴们是正确的,您需要一种将这些文件上传到服务器的方法,ostrio:files是那里最好的软件包之一。

以下是我们应用程序中的一个示例,可以指导您进行操作:

MailQueue.sendMail({
            to: payslip.employee.email,
            from: 'Payroll System <no-reply@fslabs.net>',
            subject: `Payslip of ${payslip.payrollDate}`,
            html: `Hello ${payslip.employee.full_name}, <br /><br />Here is your payslip for payroll of ${payslip.payrollDate}<br /><br />Thanks.`,
            attachments: [{ path: `'/tmp/${payslip.employee.full_name}.zip'` }],
          });

首先,我们依靠Meteor Mailer,因为它允许我们自定义邮件发送选项(没关系,您不必使用它)。我们在服务器上创建文件,存储在/tmp/目录中。