HTML custom email notifications in SAP Cloud Applications Studio
Some time ago I created a document about how to send custom email notification with attachment in SAP Cloud Applications Studio How to send an email with attachments in SAP Cloud Application Studio.
The body text in this example was plain text.
Now I want to show you how to send an HTML email.
Step 1.
Implement the Event-BeforeSave for your business object.
Step 2.
Write the following ABSL code:
import ABSL;
import AP.FO.Activity.Global as prop;
import AP.Common.GDT as GlobalDataTypes;
import BASIS.Global; //for the pdf
import DocumentServices.Global;
var elEmailRoot : elementsof EmailActivity;
var elEmailParty: elementsof EmailActivity.Party;
var instEmail;
var emailBody;
var emailSubject;
var elEmailTxtColl: elementsof EmailActivity.TextCollection;
var elEmailTxtCollTxt: elementsof EmailActivity.TextCollection.Text;
var elEmailTxtCollTxtCntnt: elementsof EmailActivity.TextCollection.Text.TextContent;
var instEmailTxtColl;
var instEmailTxtCollTxt;
var instEmailTxtCollTxtCntnt;
var instEmailAttFld;
//Email instance creation
emailSubject = "Html test";
elEmailRoot.Name.content = emailSubject;
instEmail = EmailActivity.Create(elEmailRoot);
//Add parties
elEmailParty.PartyKey.PartyID.content = "8000000102";
instEmail.MessageToParty.Create(elEmailParty);
elEmailParty.PartyKey.PartyID.content = "8000000020";
instEmail.MessageToParty.Create(elEmailParty);
//Create att folder
instEmailAttFld = instEmail.AttachmentFolder.Create();
//Add attachment1 (html body)
var doctype : GlobalDataTypes:DocumentTypeCode;
var binaryObject : BinaryObject;
var docDesc : GlobalDataTypes:Description;
var docName : GlobalDataTypes:LANGUAGEINDEPENDENT_Name;
var docAltName : GlobalDataTypes:LANGUAGEINDEPENDENT_Name;
var bin : BinaryObject.content;
docName = "body.html";
doctype.content = "10001";
binaryObject.mimeCode = "text/html";
binaryObject.content = Binary.ParseFromString("
<html><head></head><body><div style=\”font-weight:bold;\”>test</div><body></html>");
instEmailAttFld.CreateFile(doctype, docName, docAltName, docDesc, binaryObject);
//Add attachment 2(pdf)
var FormTemplateLanguage = "E";
var PDF : BinaryObject;
var FormTemplateCode : OutputRequestFormTemplateCode;
FormTemplateCode.content = "Y6Y527NH_PMH9V"; //Code is Form Template Header Code
// Reuse Service Call
PDF = OutputManagementUtilities.GetPDF(this,FormTemplateCode,FormTemplateLanguage);
docName = "test2.pdf";
doctype.content = "10001";
binaryObject = PDF;
instEmailAttFld.CreateFile(doctype, docName, docAltName, docDesc, binaryObject);
instEmail.Send();
Step 3.
Here you go!
Hi Alessandro,
Thanks for this tutorial. I am trying to send an HTML email (without attachments) and this is not working (using the exact same code as you are). We are on 1405, maybe something has changed?
I opened up a topic as well: Send HTML email from C4C?
Can you help me out?
Thanks!
Yes unfortunately sometimes the backend antivirus system block html email. 🙁 we can't do anything about it. Let's wait for next releases.
Hello Alessandro,
I cannot succeed in putting my body.html as a body text element.
When I receive the emails, the document is still an attachment and is not added to the body of my email.
I exactly used your code above. Is there any step to do in order to add this attachment as a body and not as an attachment of the email?
Thank you very much for your attention.
Best regards.
Jacques-Antoine
Hi Jacques, it depends on the email client (gmail is able to visualize it like a normal html email) however i did not find a way to remove the html from the attachments 🙁
Hello Alessandro and thank you for your permanent help!
Even if I send the email to my iCloud account or Gmail account, it is the same. I have no body added, only an attachment.
When you say: "however i did not find a way to remove the html from the attachments", I can see that the "Test" you put in your binary object is appearing as a body text.
Am I wrong?
Because that is exactly what I need. I do not care if there is still an html attachment as long as I can retrieve the text I did put in it, directly in the email's body.
I raised another thread in order to discuss that:
Thank you very much for your attention.
Bets regards.
Jacques-Antoine
Hi, yes I was able to see the attachment as a body with Gmail and with Outlook. Try to remove the text (leave only the html body)
As usual, you were right! 😉
Best regards
JA
Hi Alessandro,
I have tried above code. But I got this Error.
Error: The document you want to upload has the MIME type text/html. Documents with this MIME type are not allowed to be uploaded according to the settings made in your solution configuration.
I hope, I miss some configuration. Can you help me to solve it?
Regards,
sankaran
Hi San Ka Ran
You need to add the mime type to the config.
goto Business config WC -> first Implementation->fine tune -> search for Mime .
Add the mime type code in this setting .
this setting.
hi Swapnil,
Thanks. I have configured as you said. Even I got an same issue. Please help me to solve this issue.
Configuration and Error window Screen Shots :
Did you restart the solution after saving the configuration?
Yes I did....
disable the mime type check please
yep I have done. But still facing same problem.
Check if the mime settings are in project, if not add
Check also other options: is your domain allowed to send?
Disable signing and encryption
hi, Thank you so much.
I have checked all the things. Yep all the setup are configured successfully. Even I'm facing same problem.
In that case better if you raise an incident to SAP 🙁
Thanks @alessandro. I'll 🙂
@Alessandro, In Other system, I have tried as you said. It is working perfectly. Thanks a lot. I think problem in previous system. So I have raised an incident to SAP. once again thanks. 🙂 🙂 🙂
Dear Alessandro,
HTML mail was successfully send in development system.
when i was configured in customer tenant. i got this error message while run the script
"The document you want to upload has the MIME type text/html. Documents with this MIME type are not allowed to be uploaded according to the settings made in your solution configuration"
also can you guide me where to find the Disable signing and encryption
Regards,
Sankaran
Hi there is a screenshot in my previous post
Hi Alessandro,
I have a requirement to send an attachment (PDF Doc which is available on my system hard disk), can I do this without using the HTML body as attachment? If it is not then what I have to use as HTML body in that case.
And also Can I get this PDF from one of my drives on the system? any syntax for the same will be helpful if. Thanks.
Regards
Hanu
Hi! You cannot add an attachment to an email from your hard drive. The document needs to be available in the C4C System in one business object that you are processing as a binaryobject.
Hi Alessandro,
How to add CC, BCC in scripting?
Use this for BCC:
var elEmailBCCParty: elementsof EmailActivity.Party;
elEmailBCCParty.PartyKey.PartyID.content = "123";
instEmail.BlindCopyMessageToParty.Create(elEmailBCCParty);
Use this for CC:
var elEmailCCParty: elementsof EmailActivity.Party;
elEmailCCParty.PartyKey.PartyID.content = "12345";
instEmail.CopyMessageToParty.Create(elEmailCCParty);
Hi Godsall. Thanks a lot. it is possible to add more than one cc / Bcc at a time?
You can create me than one.
var elEmailBCCParty: elementsof EmailActivity.Party;
elEmailBCCParty.PartyKey.PartyID.content = "123";
instEmail.BlindCopyMessageToParty.Create(elEmailBCCParty);
elEmailBCCParty.PartyKey.PartyID.content = "123456789";
instEmail.BlindCopyMessageToParty.Create(elEmailBCCParty);
thanks a lot godsall.. I have one more doubt. How to set custom "From" Address instead of using default "Address"?
using the activity email you cant you need to use the "absl" version (check my other blogs)
Hi All,
Since the EmailActivity is now deprecated, I am trying to make use of Activity BO to perform send mail, but could not achieve the same. i have these points unanswered:
Please guide.
Thanks,
Srihari
Hello everyone,
any updates on this?
Thanks and kind regards
Lars