کار با ایمیل‌های AMP HTML

درباره AMP

AMP (Accelerated Mobile Pages) یک چارچوب منبع باز توسط Google است که از HTML، CSS و JavaScript ساخته شده است. این چارچوب امکان ساخت ایمیل‌های سبک وزن با زمان بارگذاری سریع‌تر را فراهم می‌کند. AMP بسیاری از مؤلفه‌ها مانند کاروسل‌ها، فرم‌ها، تصاویر و غیره را ارائه می‌دهد. علاوه بر سرعت، ایمیل‌های AMP به‌صورت کاملاً تعاملی داخل کلاینت ایمیل رندر می‌شوند و کاربران می‌توانند مستقیماً در ایمیل‌ها تعامل داشته باشند؛ به‌عنوان مثال فرم را پر کنند یا به نظرسنجی پاسخ دهند بدون اینکه کلاینت ایمیل را ترک کنند.

کار با ایمیل‌های AMP HTML

Aspose.Email ارائه می‌دهد AmpMessage که نمایانگر یک ایمیل AMP است. Aspose.Email کلاس‌های متعددی را برای افزودن مؤلفه‌ها به ایمیل‌های AMP مانند AmpImage, AmpCarousel, AmpAccordion, AmpForm, و غیره. قطعه کد زیر نشان می‌دهد که چگونه ایمیل‌های AMP را با استفاده از API Aspose.Email ایجاد، ذخیره و به‌روز کنید.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

AmpMessage msg = new AmpMessage();
msg.setHtmlBody("<html><body> Hello AMP </body></html>");

// Add an AmpAnim component
AmpAnim anim = new AmpAnim(800, 400);
anim.setSrc("https://example.com/images/animation.gif");
anim.setAlt("Test alt");
anim.setAttribution("The Go gopher was designed by Renee French");
anim.getAttributes().setLayout(LayoutType.Responsive);
anim.setFallback("offline");
msg.addAmpComponent(anim);

// Add an AmpImage component
AmpImage img = new AmpImage(800, 400);
img.setSrc("https://example.com/images/photo1.jpg");
img.setAlt("Test alt");
img.getAttributes().setLayout(LayoutType.Responsive);
msg.addAmpComponent(img);

// Add an AmpCarousel component holding three images
AmpCarousel car = new AmpCarousel(800, 400);

img = new AmpImage(800, 400);
img.setSrc("https://example.com/images/photo2.jpg");
img.setAlt("Test 2 alt");
img.getAttributes().setLayout(LayoutType.Fixed);
car.getImages().add(img);

img = new AmpImage(800, 400);
img.setSrc("https://example.com/images/photo3.jpg");
img.setAlt("Test alt");
img.getAttributes().setLayout(LayoutType.Responsive);
car.getImages().add(img);

img = new AmpImage(800, 400);
img.setSrc("https://example.com/images/photo4.jpg");
img.setAlt("Test 3 alt");
img.getAttributes().setLayout(LayoutType.Fill);
car.getImages().add(img);

msg.addAmpComponent(car);

// Add an AmpFitText component
String loremIpsum = "Lorem ipsum dolor sit amet, has nisl nihil convenire et, "
        + "vim at aeque inermis reprehendunt.";
AmpFitText txt = new AmpFitText(loremIpsum);
txt.getAttributes().setWidth(600);
txt.getAttributes().setHeight(300);
txt.getAttributes().setLayout(LayoutType.Responsive);
txt.setMinFontSize(8);
txt.setMaxFontSize(16);
msg.addAmpComponent(txt);

// Add an AmpAccordion component with three sections
AmpAccordion acc = new AmpAccordion();
acc.setExpandSingleSection(true);

Section sec = new Section();
sec.setHeader(new SectionHeader(SectionHeaderType.h2, "Section 1"));
sec.setValue(new SectionValue("Content in section 1."));
acc.getSections().add(sec);

sec = new Section();
sec.setHeader(new SectionHeader(SectionHeaderType.h2, "Section 2"));
sec.setValue(new SectionValue("Content in section 2."));
acc.getSections().add(sec);

// A section can hold a component instead of text
img = new AmpImage(800, 400);
img.setSrc("https://example.com/images/photo1.jpg");
img.setAlt("Test alt");
img.getAttributes().setLayout(LayoutType.Responsive);

sec = new Section();
sec.setHeader(new SectionHeader(SectionHeaderType.h2, "Section 3"));
sec.setValue(new SectionValue(img));
acc.getSections().add(sec);

msg.addAmpComponent(acc);

// Add an AmpForm component
AmpForm form = new AmpForm();
form.setMethod(FormMethod.Post);
form.setActionXhr("https://example.com/subscribe");
form.setTarget(FormTarget.Top);

FormField field = new FormField("Name:", "text");
field.setName("name");
field.setRequired(true);
form.getFieldset().add(field);

field = new FormField("Email:", "email");
field.setName("email");
field.setRequired(true);
form.getFieldset().add(field);

field = new FormField();
field.setInputType("submit");
field.setValue("Subscribe");
form.getFieldset().add(field);

msg.addAmpComponent(form);

msg.save(dataDir + "AmpTest_1.eml");

// An AMP message loaded back from disk can be extended with further components
MailMessage savedMsg = MailMessage.load(dataDir + "AmpTest_1.eml");
if (savedMsg instanceof AmpMessage) {
    AmpMessage ampMsg = (AmpMessage) savedMsg;

    AmpTimeago time = new AmpTimeago(new Date());
    time.getAttributes().setWidth(600);
    time.getAttributes().setHeight(300);
    time.getAttributes().setLayout(LayoutType.Fixed);
    time.setLocale("en-US");
    time.setCutoff(600);
    ampMsg.addAmpComponent(time);

    ampMsg.save(dataDir + "AmpTest_2.eml");
}