We can use a smartform, but this will send the email as a PDF attachment (or as plain text if we setup the smtp node in transaction SCOT accordingly).
Another method is to create and send the email completely in ABAP by adding all the lines of text (or html) to an email contents table and send it out.
The disadvantages of these two methods are that they do not allow a business user to setup and maintain the email contents and layout himself or the end result is not a nice looking html email.
Let’s see how we can use mail forms to send a flexible, easy to use and nice looking html mail.
Create attribute contextThe first step is to create an attribute context for the form in order to use dynamic texts. We need this for the personalisation part of the mail form. The attribute context defines which variables we can use in the mail form to enter dynamic data.
In our case we want to know the complaint number (transaction ID), description and posting date. We don’t need to add business partner data like first and last name, because they are standard available in the context. In our project however, we made an exception to this rule because we added a field for salutation (title_let) like ‘Dear sir’ or ‘Dear madam’. We didn’t want to add the logic for this field each time in each form again and again, so we’ve added this logic in our BAdI implementation which is discussed below.
In SE11 we create a structure with our fields.
We now go to customizing:
SPRO > Customer Relationship Management > Marketing > Marketing Planning and Campaign Management > Personalized Mail > Maintain Attribute Contexts for Mail Forms.
Create context Z_COMPLAINT like this.
Go to level ‘Structure’ and add an entry for ZERMS_COMPLAINT and check ‘All fields’.
Create mail formNow we can create our mail form. Logon to the webUI and go to Marketing > Mail Forms.
Create mail form Z_COMPLAINT_RECEIPT_MAIL like this. Choose our Attribute Context just created before.
Now start writing the email in the text editor. Each time you want to insert one of our variables of our context, click the ‘Attribute’ button. In the popup select ‘Additional Fields’, select the variable and click ‘Insert’.
It’s also possible to insert attributes in the subject line. In our case we added the Transaction ID which is visible as %SAP_A1.
In the text editor you may need to switch to html mode now end then to change some html tags to format the email precisely how you want it. In our case we created the following end result.
Create BAdI implementation
Because we will use the Action Framework to send out this email, we need to create a BAdI implementation for BAdI definition EXEC_METHODCALL_PPF. In our case we can use the SAP standard delivered implementation CRM_COMPL_MAIL as a starting point.
Go to transaction SE19 and copy CRM_COMPL_MAIL to YCRM_HTML_MAIL. Don’t forget to enter YCRM_HTML_MAIL as the filter value and activate the BAdI.
The method does three things: fill the attributes in our context, compose the email and send it. The last two things are already taken care of, we only need to make some changes for our context. This is done in method get_context_attributes, which is called by get_attributes in line 172 of our main execute method.
In get_context_attributes we need to replace the standard code for structure CRMS_COMPL_MAIL_FIELDS with code to fill our own attributes in ZERMS_COMPLAINT. For example like this:
Create action and conditionWe are now ready to define our action and related condition.
SPRO > Customer Relationship Management > Basic Functions > Actions > Actions in Transaction > Change Actions and Conditions > Define Action Profiles and Actions.
Define an action with processing type ‘Method Call’ and choose ‘YCRM_HTML_MAIL’ as method like this:
Now it’s important to add two variables to the container. Click on the ‘Change Definition’ icon next to ‘Processing Parameters’. Add two elements to the container as follows:
Element MAIL_FORM as type CRMT_PML_ID with initial value Z_COMPLAINT_RECEIPT_MAIL.
Do the same for REPLY_EMAIL as type AD_SMTPADR and value firstname.lastname@example.org.
Our BAdI implementation reads these elements from the container so it’s important to enter the names exactly as defined here. The last step is to create a condition for this action. By the way: you can choose to fill the values for the mail form and sender here in the action definition, but you can also fill them in the condition.
Final resultWe are finally ready to test our solution. We create a complaint and receive the following confirmation email:
We now have a nice html email confirmation! And if our business user needs to change the content or layout of the email, for example to add a temporary promotion, he can do it himself without the need of a change request procedure and involvement of an SAP CRM consultant!