代码之家  ›  专栏  ›  技术社区  ›  bp123

从表单发送电子邮件附件

  •  0
  • bp123  · 技术社区  · 8 年前

    我正试图通过meteor应用程序中的表单发送一封带有附件的电子邮件。发送电子邮件很好,它可以工作,但是,我不知道如何发送附件。我的表单允许用户附加文件,但是,我不知道如何将其传递给服务器以作为附件发送。

    我看过流星 documentation 这也没什么帮助。

    它指向 mailcompser 4 .

    当我安慰的时候。在显示名称的服务器上记录附件。它说它需要一条路,但我不知道那是什么。

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

    路径: 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 回复  |  直到 4 年前
        1
  •  0
  •   Harry Adel    8 年前

    英俊的威尔森来营救:

    Meteor的默认电子邮件包3在 场景。确保附件数组遵循中列出的选项 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/ 目录