Postman本身不直接支持发送邮件,但可通过配置SMTP或调用API触发邮件发送,并在过程中设置邮件主题。以下是具体方法:
若需通过Postman直接发送带附件的邮件,需先配置SMTP服务器,再在请求体中指定邮件主题:
/opt/postman)。smtp.gmail.com)、端口(587或465);pm.test("Send email with attachment", function () {
var emailData = {
"to": "recipient@example.com",
"subject": "Test Email Subject", // 此处设置邮件主题
"text": "This is the email body.",
"attachment": [{"filename": "example.txt", "content": "Attachment content."}]
};
var request = pm.request.url({
method: "POST",
url: "https://your-smtp-server.com/send-email",
header: {"Content-Type": "application/json"},
body: {mode: "raw", raw: JSON.stringify(emailData)}
});
request.send();
});
将recipient@example.com替换为收件人地址,https://your-smtp-server.com/send-email替换为SMTP服务器地址,运行请求即可发送带指定主题的邮件。若需通过Postman调用第三方邮件API(如SendGrid、Mailgun),可在请求体中直接设置邮件主题:
https://api.example.com/send-email)。Content-Type: application/json(表示请求体为JSON格式)。{
"to": "recipient@example.com",
"subject": "Custom Email Subject", // 此处自定义邮件主题
"body": "This is the email content sent via Postman."
}
将recipient@example.com替换为收件人地址,https://api.example.com/send-email替换为实际API地址,点击Send即可触发邮件发送。若需通过Postman调用本地Python脚本发送邮件,可在脚本中设置主题,并通过Postman传递参数:
send_email.py):import requests
import sys
def send_email(to, subject, body):
url = "https://api.example.com/send-email" # 替换为实际API地址
headers = {"Content-Type": "application/json"}
payload = {"to": to, "subject": subject, "body": body}
response = requests.post(url, json=payload, headers=headers)
return response.json()
if __name__ == "__main__":
to = sys.argv[1] # 从命令行获取收件人地址
subject = sys.argv[2] # 从命令行获取邮件主题
body = sys.argv[3] # 从命令行获取邮件内容
print(send_email(to, subject, body))
/usr/local/bin/send_email.py),在Body中添加参数:{
"to": "recipient@example.com",
"subject": "Script-Based Email Subject", // 此处设置邮件主题
"body": "This email is sent via a local Python script."
}
运行请求后,Postman会调用脚本并发送带指定主题的邮件。