To send an email with attachment ABAP

Tuesday, December 30, 2008

I learned how to do this from this link.
It uses a function module which you have to create.
But I added those required code inside the program itself and added the zipping feature.

This sends the e-mail immediately, so no scot is needed.

The program is tested and works fine just modify according to your needs.
Copy the code part and paste in the ABAP editor.
Good luck. :)

EDIT 06/01/09
According to my knowledge this works on SAP systems which has 4.6c higher versions.
A way to find out is by going to SE80 and entering the relevant class names and find out whether they are available in the system.
Thanks IndiaKing for pointing out the version issue.

Here is the source code link of the program for download.



data: binary_content type solix_tab.
data: xl_content type xstring .

con_tab type c value cl_abap_char_utilities=>horizontal_tab,
con_cret type c value cl_abap_char_utilities=>cr_lf.

data: xlcontent type xstring,
conlength type i,
conlengths type so_obj_len,
result_content type string,
wa_string type string, "This holds all of your data
dummy type string.

data: send_request type ref to cl_bcs.
data: text type bcsy_text.
data: document type ref to cl_document_bcs.
data: sender type ref to if_sender_bcs.
data: recipient type ref to if_recipient_bcs.
data: recipients type bcsy_smtpa.
data: bcs_exception type ref to cx_bcs.
data: sent_to_all type os_boolean.

data: e_r_page type ref to cl_rsr_www_page.
data: content_length type w3param-cont_len ,
content_type type w3param-cont_type,
return_code type w3param-ret_code .
data: html type standard table of w3html .
data: server type string ,
port type string .
data: wa_rec type ad_smtpadr .
data: bcs_message type string .

data: subject type so_obj_des.
data: sender_id type ad_smtpadr.
data: email type ad_smtpadr.

data: gv_file type string,
gv_zipfilehex type xstring,
go_zipper type ref to cl_abap_zip. "The zip folder object

concatenate 'Customer no.'
'Sales incoterms'
'Mat long text'
'Level 1 description'
'Level 2 description'
'Level 3 description'
'Valid from'
'Valid to'
into wa_string separated by con_tab.
concatenate con_cret con_tab wa_string into wa_string.
* After this keep on appending the data to the wa_string
data: i type i,
true type string.

clear: xl_content .
* convert the string data to xstring
call function 'SCMS_STRING_TO_XSTRING'
text = wa_string
* MIMETYPE = ' '
buffer = xl_content.
* FAILED = 1
* OTHERS = 2
if sy-subrc <> 0.

gv_file = 'Data.xls'. "Zip file name

create OBJECT go_zipper.
go_zipper->add( name = gv_file
content = xl_content )."If you have other content to be added to the zip folder, do it here

gv_zipfilehex = go_zipper->save( ).

concatenate 'Data list ' 'date' into subject .

sender_id = 'your_email@yourdomain.tld'.
email = 'customer@hisdomain.tld'.

refresh binary_content .

call function 'SCMS_XSTRING_TO_BINARY'
buffer = gv_zipfilehex
binary_tab = binary_content.

*refresh text .
clear result_content .
*The body message
'<p><font color="#000080">Dear Customer,</font></p>'
'<p><font color="#000080">Attached herewith is the data list you requested.</font></p>'

into result_content .
conlength = strlen( result_content ) .
conlengths = conlength .
call function 'SCMS_STRING_TO_FTEXT'
text = result_content
ftext_tab = text.

clear send_request .

send_request = cl_bcs=>create_persistent( ).

clear document .
document = cl_document_bcs=>create_document(
i_type = 'HTM'
i_text = text
i_length = conlengths
i_subject = subject ).

call method document->add_attachment
i_attachment_type = 'zip'
i_attachment_subject = 'Price List' "Attachment name
i_att_content_hex = binary_content.

*You can add multiple attachments like below

* CALL METHOD document->add_attachment
* i_attachment_type = 'zip'
* i_attachment_subject = 'Subject testing 2' "atta_sub
* i_att_content_hex = binary_content.
* CALL METHOD document->add_attachment
* i_attachment_type = 'zip'
* i_attachment_subject = 'Subject 3' "atta_sub
* i_att_content_hex = binary_content.

* add document to send request
call method send_request->set_document( document ).

clear sender .
sender = cl_cam_address_bcs=>create_internet_address( sender_id ).
call method send_request->set_sender
i_sender = sender.

clear wa_rec .

* add recipient with its respective attributes to send request
* you can add multiple recipients in the same mail by calling
* this method repeatedly.

clear recipient .
wa_rec = email.
recipient = cl_cam_address_bcs=>create_internet_address(
wa_rec ).

* call method send_request->add_recipient
* exporting
* i_recipient = recipient1
* i_express = 'X'.

* call method send_request->add_recipient
* exporting
* i_recipient = recipient2
* i_express = 'X'.

call method send_request->add_recipient
i_recipient = recipient
i_express = 'X'.

call method send_request->set_status_attributes
i_requested_status = 'E'
i_status_mail = 'E'.

call method send_request->set_send_immediately( 'X' ).
* ---------- send document ---------------------------------------

call method send_request->send(
i_with_error_screen = 'X'
result = sent_to_all ).
if sent_to_all = 'X'.
*APPEND 'Mail sent successfully ' TO return .
commit work.
catch cx_bcs into bcs_exception.
bcs_message = bcs_exception->get_text( ).
*APPEND bcs_message TO return .

Intel Wireless Pro 3945abg APs not detected!

Sunday, August 3, 2008

Hello there got the above problem?
He he happy surfing trying to look for solutions.

Me too had the same problem.
All you have to do is
be the super user (sudo wouldn't do)
"sudo su -"
"alias wlan0 iwl3945"
"options iwl3945 disable_hw_scan=1"
lines inside /etc/modprobe.d/iwl3945

those are the only lines that should be inside iwl3945 file
now reboot. :)

What that does is, it disables the hardware scanning and enables the software
scan. Hardware scan is the fastest and so it is the default.
This bug can be permanently removed by using a patch
If you are so much interested in that(like me :) )
Then go here.

If that doesn't work too then you might have to revert back to the old ipw3945
Try this to do that if you are using ubuntu. But I think it is same for others as well
if you can figure out where the files are.

NetWeaver 2004s SR2 installation. Whew. :)

Saturday, August 2, 2008

About a month or two ago I did a NetWeaver 2004s SR2 installation ABAP+JAVA stack.
I encountered many errors during that.
Mostly because I was inexperienced about the SAP installation and how the SAP is implemented.

Below is a little guide to the installation.
I installed SAP NetWeaver on Solaris.
Even though first I disagreed to use Solaris as the OS, later I found out that it is much more stable, server software wise.
And there is much more support from the SAP side to solaris installations as well.
Solaris installation is so smooth.
So you don't have to worry about any errors during the installation.
Prepare the disk spaces the way you want

Create a home partition with enough space for installation dvds to be copied (/export/home)
So create a partition with at least 10GB to hold the installation dvds.
After creating the relevant partitions use the rest of the partition for the oracle db.
Oracle is the database that you are going to hold your data so you got to think of your future requirements and allocate accordingly.
For my installation I used the rest of the partition after creating other partitions since I had a 500GB hard disk.
It was about 300GB or something. :)
So there should be six partitions

2-swap <-- this should have twice the amount of RAM you have installed in your server

After installing the solaris OS get the 1.4.2_17 java 64 bit version from the sun site and install it in /usr/jdk
Copy the 8 DVDs that you have to install.
Then set the JAVA_HOME variable in /etc/profile (export JAVA_HOME=path of the java home not the binary)
Then get the java policy file for 1.4.2 (you'll need it during the installation).
Edit the /etc/project for shared memory purposes.
Put the sap_inst home in the /etc/profile file.
Now start the installation using the sapinst in the MasterDVD.
You can do a prerequisite check if you want there is an option in the main installation screen for that.
When the installation asks for the password use the same password that you have for the root.
The installation will ask to create the user home directories.
You'll have to create them in the /export/home directory.
There will be two new users added to the /etc/passwd file SIDadm and oraSID where SID is your system ID.
At about the phase 14 it will ask you to install the oracle db by switching to the user oraSID.
First allow more than one client to connect to your xserver by executing 'xhost +' command and then in a terminal su oraSID and go to the folder where sapinst is telling where the oracle installation folder is then ./RUNINSTALLER.
This folder is extracted to /oracle by sapinst The oracle home and oracle sid must be set before running the ora installation.
It will do a prerequisite check before the installation and tell you the errors that it encountered.
After successfully installing oracle, install the latest OPatch application and patch the oracle to the latest version using that app.
Then resume the sapinst from where it stopped.
I got some error regarding memory saying not enough space could not fork process. So it is best to have at least 8gb during the installation if you are doing a netweaver java+abap one (Thanks Anthony).
And there was another error about creating java users.
So if that error comes you have to put java_startup/signal_handling = 0 line to the default.pfl file in the profile directory and put the same line in the /tmp/sapinst/Usercheck.pfl file as well.

And another error occurred saying j2ee_admin is locked.
If that error comes up you have to manually unlock the user by logging in to the netweaver sap using the GUI interface.
To log in to the netweaver you must first first enable sap* user by invoking the configtool which is in the j2ee directory.
Then you might have to restart the cluster.
To do that use the web console http://sid:50013.
After that every thing should go smoothly.

Why the change is easy when you are doing it simple

Wednesday, June 18, 2008

Recently I was assigned a tasked to write a particular program using the OO language called ABAP.
ABAP is a language invented by SAP AG to customize it's applications. Like any other programmer (can't say most of them are like that though :) )I just started the coding part and in about a 4-5 days I completed it and gave it for testing.
Now starts the real deal. The testing person found many bugs ( Not to mention that I could not believe that a person like me actually made those errors. Because some of them were too simple. You know what i mean). A certain error required the flow of the program to be changed but the problem was that if i did the change it would require me to change some other places as well. Resulting in a major modification to the program.
Which might result in creating more bugs than previously occurred.

Anyways my point is, if you can do a thing with simple concepts and maybe convert the hard problems or matters in to simple things, a change or a major modification or a transition might not cost you soooo much than you might expect.