Pages

Showing posts with label ABAP Mail send with attachement. Show all posts
Showing posts with label ABAP Mail send with attachement. Show all posts

Tuesday, December 15, 2009

Sending mail from SAP + Attachment as ADOBE form

1: Call form:

TRY .
* First get name of the generated function module
CALL FUNCTION 'FP_FUNCTION_MODULE_NAME'
EXPORTING
i_name = p_formname
IMPORTING
e_funcname = lv_form_name.
CATCH cx_fp_api_repository INTO obj_root.
wa_err-person = p_mailtab-perid.
wa_err-desc = obj_root->get_text( ).
APPEND wa_err TO it_err.
CLEAR wa_err.
ENDTRY.


2: Set output params. open Job for processing:


* Set output parameters and open spool job
wa_outputparams-nodialog = c_x. " suppress printer dialog popup
wa_outputparams-preview = space. " launch print preview
wa_outputparams-getpdf = c_x.

CALL FUNCTION 'FP_JOB_OPEN'
CHANGING
ie_outputparams = wa_outputparams
EXCEPTIONS
cancel = 0
usage_error = 0
system_error = 0
internal_error = 0
OTHERS = 0.
* Processing should continue inspite of errors
* so made Sy-Subrc as 0

* Set form language and country (->form locale)
wa_docparams-langu = lc_language.
wa_docparams-country = lc_country.


3: Call the Form. Also error from an adobe form can be caught in following way:


* Pass Data to the Form Interface
CALL FUNCTION lv_form_name
EXPORTING
/1bcdwb/docparams = wa_docparams
t_stud_data = wa_mailtab1
IMPORTING
/1bcdwb/formoutput = wa_pdfobject
EXCEPTIONS
usage_error = 1
system_error = 2
internal_error = 3
OTHERS = 4.

CLEAR wa_err.
* This FM captures any Error which has resulted during
* Form generation
CALL FUNCTION 'FP_GET_LAST_ADS_ERRSTR'
IMPORTING
e_adserrstr = wa_err-desc.


5: Convert PDF to SOLIX:

This is most important for PDF as an attachment


* Convert PDF Object to type SOLIX
CALL METHOD cl_document_bcs=>xstring_to_solix
EXPORTING
ip_xstring = wa_pdfobject-pdf
RECEIVING
rt_solix = it_attachment.


6: Mail sending part:

* Initialize Mail creation
TRY.
obj_send_request = cl_bcs=>create_persistent( ).

* Set sender Email address
obj_sender =
cl_cam_address_bcs=>create_internet_address( lc_id_from ).
obj_send_request->set_sender( i_sender = obj_sender ).

7: set email IDs:


* Get Recipient's Email IDs

MOVE p_mailtab-email TO lv_recv_id.
obj_recipient =
cl_cam_address_bcs=>create_internet_address( lv_recv_id ).
obj_send_request->add_recipient(
i_recipient = obj_recipient
i_express = c_x
).



8: Attaching the Doc and sending mail:


" create documents
obj_document = cl_document_bcs=>create_document(
i_type = lc_type_raw " RAW document format
i_text = it_contents
i_subject = lv_subject
).

obj_document->add_attachment(
i_attachment_type = lc_type_pdf " add PDF attachment
i_attachment_subject = 'Official Notice'(046)
i_att_content_hex = it_attachment
).
obj_send_request->set_document( obj_document ).
**********************************************************************

" send email
obj_send_request->set_send_immediately( c_x ).
lv_ret = obj_send_request->send( ). "#EC NEEDED

CATCH cx_bcs INTO obj_root.
wa_err-person = p_mailtab-perid.
wa_err-desc = obj_root->get_text( ).
APPEND wa_err TO it_err.
CLEAR wa_err.
p_return = 1.
ENDTRY.

COMMIT WORK.


* Close spool job
CALL FUNCTION 'FP_JOB_CLOSE'
EXCEPTIONS
usage_error = 0
system_error = 0
internal_error = 0
OTHERS = 0.
* Should not throw Errors and processing should continue

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'