如何通过Node.js使用Postmark发送PDF及邮件附件?
如何用Postmark发送带PDF、图片等附件的邮件?
当然可以,Postmark完全支持发送PDF、图片这类自定义附件,你只需要在请求的Attachments数组中配置好附件的相关信息就行。
附件配置说明
每个附件对象需要包含三个核心字段:
Name:收件人看到的附件文件名(比如"appointment-detail.pdf")Content:文件内容的Base64编码字符串ContentType:文件对应的MIME类型(比如PDF用"application/pdf",JPG图片用"image/jpeg")
修改后的代码示例
假设你用Node.js环境,以下是添加了PDF和图片附件的完整代码:
const fs = require('fs'); // 读取文件并转成Base64编码 const pdfContent = fs.readFileSync('./appointment-detail.pdf').toString('base64'); const imageContent = fs.readFileSync('./clinic-logo.jpg').toString('base64'); await client.sendEmail({ "From": "mywebsite@support.com", "To": email, "Subject": "Appointment Confirmation", "Attachments": [ { "Name": "appointment-detail.pdf", "Content": pdfContent, "ContentType": "application/pdf" }, { "Name": "clinic-logo.jpg", "Content": imageContent, "ContentType": "image/jpeg" } ], "TextBody": ` Dear ${fullname}, Your appointment has been confirmed with the following details: Title: ${title} Date: ${event_date.toDateString()} Time: ${start_time} - ${end_time} Purpose: ${purpose} Thank you for using our service. Best regards, Your Appointment Team if you encounter any problem or concern, please email myemail `, "MessageStream": "outbound" });
注意事项
- 确保Base64编码的内容没有多余的换行或空格,否则可能导致附件损坏
- 单个附件大小不能超过10MB,整封邮件(含所有附件)的总大小上限为20MB
- MIME类型要和文件实际格式匹配,否则邮件客户端可能无法正常解析附件
内容的提问来源于stack exchange,提问作者Stykgwar
相关产品推荐
相关产品推荐

