کار با ایمیلهای AMP HTML
AMP برای ایمیل زیرمجموعهای از چارچوب AMP است که اجازه میدهد پیام شامل مؤلفههای زنده و تعاملی باشد - اسلایدرهای تصویر، بخشهای قابل باز و بسته شدن، فرمهایی که بدون ترک صندوقپست ارسال میشوند - در کنار HTML معمولی آن. چنین پیامی یک text/x-amp-html بخش MIME پیادهسازی میکند؛ مشتریانی که از AMP پشتیبانی میکنند آن را رندر میکنند، بقیه بدنهٔ HTML معمولی را نمایش میدهند.
Aspose.Email برای 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 اشیاء در Fieldset فهرست؛ سازنده برچسب و نوع ورودی 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()));
}