【问题标题】:How to send a pdf save on s3 as attachment using Nodemailer如何使用Nodemailer将s3上的pdf保存为附件
【发布时间】:2019-12-05 20:06:46
【问题描述】:

我有发送带有 pdf 附件的电子邮件的功能,但是当我尝试发送本地没有文件的 pdf 时,电子邮件到达时没有任何附件...

async sendEmailWithAtt(email, subject, message, pathAtt) {
    const transporter = nodemailer.createTransport({
        host: process.env.EMAIL_HOST,
        port: process.env.EMAIL_PORT,
        auth: {
            user: process.env.EMAIL_USERNAME,
            pass: process.env.EMAIL_PASSWORD
        },
        attachments: [
            {
                filename: 'Document',
                path: pathAtt, // URL of document save in the cloud.
                contentType: 'application/pdf'
            }
        ]
    });

    const mailOptions = {
        from: process.env.EMAIL_USERNAME,
        to: email,
        subject: subject,
        text: message
    };

    await transporter.sendMail(mailOptions);
}

【问题讨论】:

    标签: javascript node.js pdf nodemailer


    【解决方案1】:

    Nodemailer documentation 指出,如果您想使用 URL,则必须使用 href 而不是为本地文件保留的 path

    如果文件不公开,您可以使用httpHeaders 属性传递一些标头。

    attachments: [
     {
         filename: 'Document',
         href: pathAtt, // URL of document save in the cloud.
         contentType: 'application/pdf'
      }
    ]
    

    除此之外,您在 .createTransport 中设置了 attachments,您应该将其放在消息中。

    const mailOptions = {
        from: process.env.EMAIL_USERNAME,
        to: email,
        subject: subject,
        text: message,
        attachments: [ /* ... */]
    };
    
    await transporter.sendMail(mailOptions);
    

    【讨论】:

    • 不起作用,我使用的是 Gmail,我需要在那里允许一些东西吗?
    • 定义“不起作用”。网址是公开的吗?
    • 在浏览器中打开href中的URL,可以看到文件吗?
    • 是的,如果我把网址放在浏览器上,文件就会打开,但是当我发送电子邮件时到达时没有任何附件:(
    • 检查更新的答案,您在错误的对象中使用了attachment
    【解决方案2】:
     function sendFile(){    
    
        let transporter = nodemailer.createTransport({
            host: '',//put email_host
            port: '', // put port 
            secure: true,
            auth: {
                user: '',//put user email
                pass: '' //put password
            }
        });
    
        
    
        let mailToUser = {
            from: 'demo@gmail.com', //replace with user_email
            to: 'test@gmail.com', // replace with that want to send it
            
            html: "Hello",
            attachments: [
                {
                    filename: `test.pdf`
                    path :`` //put S3 url
                    
                    
                }
            ]
    
        };
    
        transporter.sendMail(mailToUser, (error, info) => {
            if (error) {
                 console.log(error);
            }
            console.log('Message sent: %s', info);
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-05
      • 1970-01-01
      • 2016-06-13
      • 2011-06-29
      • 1970-01-01
      • 2016-03-30
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多