← Cloudflare Email Service / email-service / examples / email-sending
Указание получателей
Email Service позволяет указывать получателей несколькими способами: несколько получателей, CC и BCC, именованные адреса, с помощью привязка Workers, REST API, или SMTP. Суммарное количество адресов во всех to, cc, а также bcc не должно превышать 50. См. Лимиты.
Несколько получателей
const response = await env.EMAIL.send({
to: ["[email protected]", "[email protected]", "[email protected]"],
from: { email: "[email protected]", name: "Newsletter Team" },
subject: "Monthly Newsletter",
html: "<h1>This month's updates</h1>",
text: "This month's updates",
});curl "https://api.cloudflare.com/client/v4/accounts/{account_id}/email/sending/send" \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"to": ["[email protected]", "[email protected]"],
"from": { "address": "[email protected]", "name": "Newsletter Team" },
"subject": "Monthly Newsletter",
"html": "<h1>This month'\''s updates</h1>",
"text": "This month'\''s updates"
}'Укажите всех получателей в To заголовок и передайте один --mail-rcpt флаг для каждого адреса.
cat > mail.txt <<EOF
From: Newsletter Team <[email protected]>
To: [email protected], [email protected]
Subject: Monthly Newsletter
This month's updates
EOF
curl --ssl-reqd \
--url "smtps://smtp.mx.cloudflare.net:465" \
--user "api_token:<API_TOKEN>" \
--mail-from "[email protected]" \
--mail-rcpt "[email protected]" \
--mail-rcpt "[email protected]" \
--upload-file mail.txtCC и BCC
const response = await env.EMAIL.send({
to: "[email protected]",
cc: ["[email protected]"],
bcc: ["[email protected]"],
from: "[email protected]",
replyTo: "[email protected]",
subject: "Order Confirmation #12345",
html: "<h1>Your order is confirmed</h1>",
text: "Your order is confirmed",
});curl "https://api.cloudflare.com/client/v4/accounts/{account_id}/email/sending/send" \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"to": "[email protected]",
"cc": ["[email protected]"],
"bcc": ["[email protected]"],
"from": "[email protected]",
"reply_to": "[email protected]",
"subject": "Order Confirmation #12345",
"html": "<h1>Your order is confirmed</h1>",
"text": "Your order is confirmed"
}'Добавьте Cc заголовок для получателей CC. Получатели BCC добавляются как --mail-rcpt флагов, но отсутствуют в заголовках.
cat > mail.txt <<EOF
From: [email protected]
To: [email protected]
Cc: [email protected]
Reply-To: [email protected]
Subject: Order Confirmation #12345
Your order is confirmed
EOF
curl --ssl-reqd \
--url "smtps://smtp.mx.cloudflare.net:465" \
--user "api_token:<API_TOKEN>" \
--mail-from "[email protected]" \
--mail-rcpt "[email protected]" \
--mail-rcpt "[email protected]" \
--mail-rcpt "[email protected]" \
--upload-file mail.txtИменованные получатели
Указывайте отображаемое имя вместе с адресом для отправителя и получателей.
const response = await env.EMAIL.send({
to: { email: "[email protected]", name: "Jane Doe" },
from: { email: "[email protected]", name: "Support Team" },
subject: "Welcome!",
html: "<h1>Thanks for joining!</h1>",
text: "Thanks for joining!",
});curl "https://api.cloudflare.com/client/v4/accounts/{account_id}/email/sending/send" \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"to": { "address": "[email protected]", "name": "Jane Doe" },
"from": { "address": "[email protected]", "name": "Support Team" },
"subject": "Welcome!",
"html": "<h1>Thanks for joining!</h1>",
"text": "Thanks for joining!"
}'Используйте Display Name <address> форму в From и To заголовки.
cat > mail.txt <<EOF
From: Support Team <[email protected]>
To: Jane Doe <[email protected]>
Subject: Welcome!
Thanks for joining!
EOF
curl --ssl-reqd \
--url "smtps://smtp.mx.cloudflare.net:465" \
--user "api_token:<API_TOKEN>" \
--mail-from "[email protected]" \
--mail-rcpt "[email protected]" \
--upload-file mail.txtСмешанные обычные и именованные получатели
Комбинируйте обычные и именованные адреса в одном и том же to поле.
const response = await env.EMAIL.send({
to: ["[email protected]", { email: "[email protected]", name: "Jane Doe" }],
from: "[email protected]",
subject: "Team update",
html: "<h1>Monthly update</h1>",
text: "Monthly update",
});curl "https://api.cloudflare.com/client/v4/accounts/{account_id}/email/sending/send" \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"to": [
"[email protected]",
{ "address": "[email protected]", "name": "Jane Doe" }
],
"from": "[email protected]",
"subject": "Team update",
"html": "<h1>Monthly update</h1>",
"text": "Monthly update"
}'cat > mail.txt <<EOF
From: [email protected]
To: [email protected], Jane Doe <[email protected]>
Subject: Team update
Monthly update
EOF
curl --ssl-reqd \
--url "smtps://smtp.mx.cloudflare.net:465" \
--user "api_token:<API_TOKEN>" \
--mail-from "[email protected]" \
--mail-rcpt "[email protected]" \
--mail-rcpt "[email protected]" \
--upload-file mail.txtСледующие шаги
- API Workers : полный
send()справочник. - REST API : отправляйте через HTTPS.
- SMTP : отправляйте из любого клиента с поддержкой SMTP.
- Вложения писем : отправляйте PDF, встроенные изображения и загруженные файлы.