AMP HTML 이메일 작업
AMP for Email은 AMP 프레임워크의 하위 집합으로, 메시지에 이미지 캐러셀, 접을 수 있는 섹션, 메일함을 떠나지 않고 제출되는 폼 등 실시간 인터랙티브 컴포넌트를 일반 HTML와 함께 포함할 수 있게 합니다. 이러한 메시지는 추가적인 text/x-amp-html MIME 파트; AMP를 지원하는 클라이언트는 이를 렌더링하고, 그 외 클라이언트는 일반 HTML 본문을 표시합니다.
Aspose.Email for C++는 이를 통해 구현됩니다 AmpMessage, a 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()));
}