使用 AMP HTML 邮件
AMP for Email 是 AMP 框架的子集,允许消息包含实时、交互式组件——图片轮播、可折叠区段、无需离开邮箱即可提交的表单——以及常规 HTML。此类消息会携带额外的 text/x-amp-html MIME 部分实现;支持 AMP 的客户端会渲染它,其他客户端显示普通的 HTML 正文。
Aspose.Email for C++ 通过 AmpMessage中提供一组组件类, MailMessage 子类,并在 Aspose::Email::Amp 命名空间。每个组件都派生自 AmpComponent,公开一个 Attributes 对象用于通用的 AMP 布局属性,并通过 AddAmpComponent,这也会注册组件所需的脚本。
以下示例使用以下头文件和命名空间:
#include <MailMessage.h>
#include <MailAddressCollection.h>
#include <SaveOptions.h>
#include <EmlSaveOptions.h>
#include <Amp/AmpMessage.h>
#include <Amp/LayoutType.h>
#include <Amp/AmpComponents/AmpAttributes.h>
#include <Amp/AmpComponents/AmpAnim.h>
#include <Amp/AmpComponents/AmpImage.h>
#include <Amp/AmpComponents/AmpFitText.h>
#include <Amp/AmpComponents/AmpTimeago.h>
#include <Amp/AmpComponents/AmpCarousel/AmpCarousel.h>
#include <Amp/AmpComponents/AmpAccordion/AmpAccordion.h>
#include <Amp/AmpComponents/AmpAccordion/Section.h>
#include <Amp/AmpComponents/AmpAccordion/SectionHeader.h>
#include <Amp/AmpComponents/AmpAccordion/SectionHeaderType.h>
#include <Amp/AmpComponents/AmpAccordion/SectionValue.h>
#include <Amp/AmpComponents/AmpForm/AmpForm.h>
#include <Amp/AmpComponents/AmpForm/FormField.h>
#include <Amp/AmpComponents/AmpForm/FormMethod.h>
#include <Amp/AmpComponents/AmpForm/FormTarget.h>
#include <system/io/file.h>
#include <system/collections/list_ext.h>
#include <system/object_ext.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Amp;
创建带图像的消息
从一个 AmpMessage,填写常规的地址字段和 HTML 回退正文,然后添加组件。 AmpAnim 嵌入一个动图 (amp-anim)以及 AmpImage 一个静态的(amp-img);两者在构造函数中接受宽度和高度,并且 LayoutType 在属性上选择元素的大小方式。
AmpMessage 覆盖基于流的 Save 重载,隐藏从 MailMessage一起传递;打开一个 FileStream 并将其与 SaveOptions 用于存储消息。
SharedPtr<AmpMessage> msg = MakeObject<AmpMessage>();
msg->set_From(u"sender@domain.com");
msg->get_To()->Add(u"receiver@domain.com");
msg->set_Subject(u"AMP newsletter");
// regular HTML fallback for clients that do not render AMP
msg->set_HtmlBody(u"<html><body> Hello AMP </body></html>");
// amp-anim: animated image
SharedPtr<AmpAnim> anim = MakeObject<AmpAnim>(800, 400);
anim->set_Src(u"https://example.com/images/animation.gif");
anim->set_Alt(u"Animated banner");
anim->set_Attribution(u"Illustration by Example Studio");
anim->get_Attributes()->set_Layout(LayoutType::Responsive);
anim->set_Fallback(u"offline");
msg->AddAmpComponent(anim);
// amp-img: static image
SharedPtr<AmpImage> img = MakeObject<AmpImage>(800, 400);
img->set_Src(u"https://example.com/images/photo.jpg");
img->set_Alt(u"Product photo");
img->get_Attributes()->set_Layout(LayoutType::Responsive);
msg->AddAmpComponent(img);
SharedPtr<IO::FileStream> stream = IO::File::Create(u"AmpImages.eml");
msg->Save(stream, SaveOptions::get_DefaultEml());
stream->Close();
轮播、适配文字和折叠面板
- AmpCarousel 显示一条图像条带;添加 AmpImage 对象到它的 图像 列表。
- AmpFitText 在两者之间缩放文本以填充其框 MinFontSize 和 MaxFontSize.
- AmpAccordion 保存可折叠的 Section 对象,每个由一个 SectionHeader 以及一个 SectionValue. 值要么是文本——作为一个装箱的
String- 或其他组件。
SharedPtr<AmpMessage> msg = MakeObject<AmpMessage>();
msg->set_HtmlBody(u"<html><body> Hello AMP </body></html>");
// amp-carousel: a strip of images
SharedPtr<AmpCarousel> carousel = MakeObject<AmpCarousel>(800, 400);
SharedPtr<AmpImage> first = MakeObject<AmpImage>(800, 400);
first->set_Src(u"https://example.com/images/slide1.jpg");
first->set_Alt(u"Slide 1");
first->get_Attributes()->set_Layout(LayoutType::Fixed);
carousel->get_Images()->Add(first);
SharedPtr<AmpImage> second = MakeObject<AmpImage>(800, 400);
second->set_Src(u"https://example.com/images/slide2.jpg");
second->set_Alt(u"Slide 2");
second->get_Attributes()->set_Layout(LayoutType::Fill);
carousel->get_Images()->Add(second);
msg->AddAmpComponent(carousel);
// amp-fit-text: text that scales to fit its box
SharedPtr<AmpFitText> text = MakeObject<AmpFitText>(u"Lorem ipsum dolor sit amet, has nisl nihil convenire et.");
text->get_Attributes()->set_Width(600);
text->get_Attributes()->set_Height(300);
text->get_Attributes()->set_Layout(LayoutType::Responsive);
text->set_MinFontSize(8);
text->set_MaxFontSize(16);
msg->AddAmpComponent(text);
// amp-accordion: collapsible sections
SharedPtr<AmpAccordion> accordion = MakeObject<AmpAccordion>();
accordion->set_ExpandSingleSection(true);
SharedPtr<Section> section = MakeObject<Section>();
section->set_Header(MakeObject<SectionHeader>(SectionHeaderType::h2, u"Section 1"));
section->set_Value(MakeObject<SectionValue>(ObjectExt::Box<String>(u"Content in section 1.")));
accordion->get_Sections()->Add(section);
section = MakeObject<Section>();
section->set_Header(MakeObject<SectionHeader>(SectionHeaderType::h2, u"Section 2"));
section->set_Value(MakeObject<SectionValue>(ObjectExt::Box<String>(u"Content in section 2.")));
accordion->get_Sections()->Add(section);
// a section may hold another component instead of plain text
SharedPtr<AmpImage> sectionImage = MakeObject<AmpImage>(800, 400);
sectionImage->set_Src(u"https://example.com/images/photo.jpg");
sectionImage->set_Alt(u"Photo");
sectionImage->get_Attributes()->set_Layout(LayoutType::Responsive);
section = MakeObject<Section>();
section->set_Header(MakeObject<SectionHeader>(SectionHeaderType::h2, u"Section 3"));
section->set_Value(MakeObject<SectionValue>(sectionImage));
accordion->get_Sections()->Add(section);
msg->AddAmpComponent(accordion);
SharedPtr<IO::FileStream> stream = IO::File::Create(u"AmpComponents.eml");
msg->Save(stream, SaveOptions::get_DefaultEml());
stream->Close();
添加表单
AmpForm 渲染一个 amp-form 其提交发送到 ActionXhr 端点。字段是 FormField 中的对象 字段集 列表;构造函数接受标签和 HTML 输入类型,没有标签的字段作为提交按钮。
SharedPtr<AmpMessage> msg = MakeObject<AmpMessage>();
msg->set_HtmlBody(u"<html><body> Subscribe to our newsletter </body></html>");
// amp-form: an interactive form submitted with XHR
SharedPtr<AmpForm> form = MakeObject<AmpForm>();
form->set_Method(FormMethod::Post);
form->set_ActionXhr(u"https://example.com/subscribe");
form->set_Target(FormTarget::Top);
SharedPtr<FormField> field = MakeObject<FormField>(u"Name:", u"text");
field->set_Name(u"name");
field->set_IsRequired(true);
form->get_Fieldset()->Add(field);
field = MakeObject<FormField>(u"Email:", u"email");
field->set_Name(u"email");
field->set_IsRequired(true);
form->get_Fieldset()->Add(field);
field = MakeObject<FormField>();
field->set_InputType(u"submit");
field->set_Value(u"Subscribe");
form->get_Fieldset()->Add(field);
msg->AddAmpComponent(form);
SharedPtr<IO::FileStream> stream = IO::File::Create(u"AmpForm.eml");
msg->Save(stream, SaveOptions::get_DefaultEml());
stream->Close();
加载并更新 AMP 消息
MailMessage::Load 识别出 text/x-amp-html 部分并返回一个 AmpMessage 实例。 DynamicCast_noexcept 区分这两种情况:它返回 nullptr 针对没有 AMP 部分的消息,而 DynamicCast 将抛出一个 InvalidCastException。然后可以将组件添加到现有的 AMP 正文中——这里是一个 AmpTimeago 元素,显示给定时刻是多久前——并再次保存消息。 AmpHtmlBody 提供对生成的 AMP 标记的访问。
SharedPtr<MailMessage> loaded = MailMessage::Load(u"AmpImages.eml");
// a message with a text/x-amp-html part is loaded as an AmpMessage; any other one gives nullptr here
SharedPtr<AmpMessage> ampMsg = DynamicCast_noexcept<AmpMessage>(loaded);
if (ampMsg != nullptr)
{
// amp-timeago: shows how long ago a moment happened
DateTime moment(2019, 9, 27, 1, 1, 1, DateTimeKind::Utc);
SharedPtr<AmpTimeago> timeago = MakeObject<AmpTimeago>(moment);
timeago->get_Attributes()->set_Width(600);
timeago->get_Attributes()->set_Height(300);
timeago->get_Attributes()->set_Layout(LayoutType::Fixed);
timeago->set_Locale(u"en");
timeago->set_Cutoff(600);
ampMsg->AddAmpComponent(timeago);
SharedPtr<IO::FileStream> stream = IO::File::Create(u"AmpUpdated.eml");
ampMsg->Save(stream, SaveOptions::get_DefaultEml());
stream->Close();
Console::WriteLine(u"AMP body length: " + Convert::ToString(ampMsg->get_AmpHtmlBody().get_Length()));
}