Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Sending ALV report(As a attachment ) through EMail

Former Member
0 Kudos

Dear Friends,

Could anyone please help me to solve this problem.

I need to send ALV report <b>as an attachment</b> to External mail-id as well as SAP USER ID..The user wants to schedule this ABAP program in background, at the end the final output should be send to his ID. I facing problem to locate correct Functional Module....

I would like to thank, if anyone give some idea on this issue also if possible a sample code.

Thanks in advance

With Reg

Babu

5 REPLIES 5

Former Member
0 Kudos

HI Kamal Babu

This Question has been answered in earlier post.

Here is the link.

Have a look at this Thread.

Cheers,

Vijay Raheja

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

If you are scheduling this as a background job, SM36 provides functionality to email the spool to a recipent. When setting up your job, click the recipent button. Here you can specify an email address. When the report is ran it will send the output as an attachment to the email address. Of course, you must have your SapConnect configured. In our case, we had to do some additional work to have the attachment come as PDF file. If you are in a 46c system, then you will probably have to do the same. We had to setup a new format type to handle the conversion from ABAP list to PDF. There is a note about it. I will have to get back to you on that later.

Welcome to SDN!!.

Please remember to award points for helpful answers and mark your posts as solved when your issue has been resolved. Thanks.

Regards,

Rich Heilman

Former Member
0 Kudos

HI Kamal Babu

After sending your ABAP List to the spool request (SP01), you can use the SAP standard spool PDF conversion program.

RSTXPDFT4 - This report program converts ABAP List to PDF files.

Or Just try this sample code:

-


  • After running the background jobs, send the spool to the SAP users Office Mail Box.

REPORT ZMAILSPOOL.

INCLUDE LBTCHDEF.

  • Include the Business Object Repository object

INCLUDE <CNTN01>.

DATA ABAPNM LIKE SY-REPID.

DATA BEGIN OF LOCAL_JOB.

INCLUDE STRUCTURE TBTCJOB.

DATA END OF LOCAL_JOB.

DATA BEGIN OF LOCAL_STEP_TBL OCCURS 10.

INCLUDE STRUCTURE TBTCSTEP.

DATA END OF LOCAL_STEP_TBL.

  • Data declarations for the mail recipient

DATA RECIPIENT TYPE SWC_OBJECT.

DATA RECIPIENT_OBJ LIKE SWOTOBJID.

SWC_CONTAINER CONTAINER.

  • Data declarations for the background job

PARAMETERS ABAPNAME(20) DEFAULT 'ZTEST1'.

PARAMETERS EMPFNAME LIKE SY-UNAME DEFAULT SY-UNAME.

ABAPNM = ABAPNAME.

LOCAL_JOB-JOBNAME = ABAPNAME.

  • Generate recipient object (see report RSSOKIF1 for an example)

      • 1. Create the recipient:

IF EMPFNAME <> SPACE.

"** 1.1 Generate an object reference to a recipient object

SWC_CREATE_OBJECT RECIPIENT 'RECIPIENT' SPACE.

"** 1.2 Write the import parameters for method

"** recipient.createaddress into the container

"** and empty the container

SWC_CLEAR_CONTAINER CONTAINER.

"** Set address element (internal user 'JSMITH')

SWC_SET_ELEMENT CONTAINER 'AddressString' EMPFNAME.

"** Set address type (internal user)

SWC_SET_ELEMENT CONTAINER 'TypeId' 'B'.

"** 1.3 Call the method recipient.createaddress

SWC_CALL_METHOD RECIPIENT 'CreateAddress' CONTAINER.

"** Issue any error message generated by a method exception

IF SY-SUBRC NE 0.

MESSAGE ID SY-MSGID TYPE 'E' NUMBER SY-MSGNO.

ENDIF.

SWC_CALL_METHOD RECIPIENT 'Save' CONTAINER.

SWC_OBJECT_TO_PERSISTENT RECIPIENT RECIPIENT_OBJ.

ENDIF.

      • Recipient has been generated and is ready for use in a

      • background job

CALL FUNCTION 'JOB_OPEN'

exporting

  • DELANFREP = ' '

  • JOBGROUP = ' '

jobname = LOCAL_JOB-JOBNAME

  • SDLSTRTDT = NO_DATE

  • SDLSTRTTM = NO_TIME

IMPORTING

JOBCOUNT = LOCAL_JOB-JOBCOUNT

EXCEPTIONS

CANT_CREATE_JOB = 1

INVALID_JOB_DATA = 2

JOBNAME_MISSING = 3

OTHERS = 4

.

if sy-subrc <> 0.

WRITE: / 'JOB_OPEN PROBLEM ', SY-SUBRC.

ELSE.

WRITE: / 'JOB OPEN SUCCESS',

LOCAL_JOB-JOBNAME, LOCAL_JOB-JOBCOUNT.

endif.

CALL FUNCTION 'JOB_SUBMIT'

exporting

  • ARCPARAMS =

authcknam = SY-UNAME

  • COMMANDNAME = ' '

  • OPERATINGSYSTEM = ' '

  • EXTPGM_NAME = ' '

  • EXTPGM_PARAM = ' '

  • EXTPGM_SET_TRACE_ON = ' '

  • EXTPGM_STDERR_IN_JOBLOG = 'X'

  • EXTPGM_STDOUT_IN_JOBLOG = 'X'

  • EXTPGM_SYSTEM = ' '

  • EXTPGM_RFCDEST = ' '

  • EXTPGM_WAIT_FOR_TERMINATION = 'X'

jobcount = LOCAL_JOB-JOBCOUNT

jobname = LOCAL_JOB-JOBNAME

  • LANGUAGE = SY-LANGU

  • PRIPARAMS = ' '

REPORT = ABAPNM

  • REPORT = 'ZTEST1'

  • VARIANT = ' '

  • IMPORTING

  • STEP_NUMBER = LOCAL_JOB-STEPCOUNT

EXCEPTIONS

BAD_PRIPARAMS = 1

BAD_XPGFLAGS = 2

INVALID_JOBDATA = 3

JOBNAME_MISSING = 4

JOB_NOTEX = 5

JOB_SUBMIT_FAILED = 6

LOCK_FAILED = 7

PROGRAM_MISSING = 8

PROG_ABAP_AND_EXTPG_SET = 9

OTHERS = 10

.

if sy-subrc <> 0.

WRITE: / 'JOB_SUBMIT PROBLEM ', LOCAL_JOB-JOBCOUNT,

LOCAL_JOB-JOBNAME, ABAPNAME, SY-SUBRC.

endif.

IF EMPFNAME <> SPACE.

CALL FUNCTION 'JOB_CLOSE'

exporting

  • AT_OPMODE = ' '

  • AT_OPMODE_PERIODIC = ' '

  • CALENDAR_ID = ' '

  • EVENT_ID = ' '

  • EVENT_PARAM = ' '

  • EVENT_PERIODIC = ' '

jobcount = LOCAL_JOB-JOBCOUNT

jobname = LOCAL_JOB-JOBNAME

  • LASTSTRTDT = NO_DATE

  • LASTSTRTTM = NO_TIME

  • PRDDAYS = 0

  • PRDHOURS = 0

  • PRDMINS = 0

  • PRDMONTHS = 0

  • PRDWEEKS = 0

  • PREDJOB_CHECKSTAT = ' '

  • PRED_JOBCOUNT = ' '

  • PRED_JOBNAME = ' '

  • SDLSTRTDT = NO_DATE

  • SDLSTRTTM = NO_TIME

  • STARTDATE_RESTRICTION = BTC_PROCESS_ALWAYS

STRTIMMED = 'X'

  • TARGETSYSTEM = ' '

  • START_ON_WORKDAY_NOT_BEFORE = SY-DATUM

  • START_ON_WORKDAY_NR = 0

  • WORKDAY_COUNT_DIRECTION = 0

RECIPIENT_OBJ = RECIPIENT_OBJ

  • TARGETSERVER = ' '

  • DONT_RELEASE = ' '

  • IMPORTING

  • JOB_WAS_RELEASED = 'X'

EXCEPTIONS

CANT_START_IMMEDIATE = 1

INVALID_STARTDATE = 2

JOBNAME_MISSING = 3

JOB_CLOSE_FAILED = 4

JOB_NOSTEPS = 5

JOB_NOTEX = 6

LOCK_FAILED = 7

OTHERS = 8

.

IF ( SY-SUBRC <> 0 ).

WRITE: / 'Job_Close Problem:', ABAPNAME, SY-SUBRC.

ELSE.

WRITE: / 'Job_Close done'.

ENDIF.

ELSE. "no empfname

...

ENDIF.

-


Cheers,

Vijay Raheja

0 Kudos

Hi Vijay, I have a similar problem to the one being discussed.

I want spool output from a batch job to be mailed to external recipients.

Your solution I had already stumbled on in SAP helps (job open, submit via job, job close using a recipient object) and it is 95% of what I want.

It sends the spool as an attachment but I have no control over the actual body of the email being attached to or even the title of the email. Any idea how I can put text into the email being sent or how to set the title to one of my choice?

Regards

Neil Woodruff

pp Greg Wedlock

Former Member
0 Kudos

Hi kamal Babu

You can send an external mail via SAP will depends whether your SAP system have been configured and link to the external mail server like MS Exchange or Lotus notes.

Try this sample code also....

-


REPORT ZSENDEXTERNAL.

DATA: OBJPACK LIKE SOPCKLSTI1 OCCURS 2 WITH HEADER LINE.

DATA: OBJHEAD LIKE SOLISTI1 OCCURS 1 WITH HEADER LINE.

DATA: OBJBIN LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE.

DATA: OBJTXT LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE.

DATA: RECLIST LIKE SOMLRECI1 OCCURS 5 WITH HEADER LINE.

DATA: DOC_CHNG LIKE SODOCCHGI1.

DATA: TAB_LINES LIKE SY-TABIX.

  • Creation of the document to be sent

  • File Name

DOC_CHNG-OBJ_NAME = 'SENDFILE'.

  • Mail Subject

DOC_CHNG-OBJ_DESCR = 'Send External Mail'.

  • Mail Contents

OBJTXT = 'Minimum bid : $250000'.

APPEND OBJTXT.

OBJTXT = 'A representation of the pictures up for auction'.

APPEND OBJTXT.

OBJTXT = 'was included as attachment.'.

APPEND OBJTXT.

DESCRIBE TABLE OBJTXT LINES TAB_LINES.

READ TABLE OBJTXT INDEX TAB_LINES.

DOC_CHNG-DOC_SIZE = ( TAB_LINES - 1 ) * 255 + STRLEN( OBJTXT ).

  • Creation of the entry for the compressed document

CLEAR OBJPACK-TRANSF_BIN.

OBJPACK-HEAD_START = 1.

OBJPACK-HEAD_NUM = 0.

OBJPACK-BODY_START = 1.

OBJPACK-BODY_NUM = TAB_LINES.

OBJPACK-DOC_TYPE = 'RAW'.

APPEND OBJPACK.

  • Creation of the document attachment

  • (Assume that the data in OBJBIN is in BMP format)

*OBJBIN = ' \O/ '. APPEND OBJBIN.

*OBJBIN = ' | '. APPEND OBJBIN.

*OBJBIN = ' / \ '. APPEND OBJBIN.

*DESCRIBE TABLE OBJBIN LINES TAB_LINES.

*OBJHEAD = 'PICTURE.BMP'.

*APPEND OBJHEAD.

    • Creation of the entry for the compressed attachment

*OBJPACK-TRANSF_BIN = 'X'.

*OBJPACK-HEAD_START = 1.

*OBJPACK-HEAD_NUM = 1.

*OBJPACK-BODY_START = 1.

*OBJPACK-BODY_NUM = TAB_LINES.

*OBJPACK-DOC_TYPE = 'BMP'.

*OBJPACK-OBJ_NAME = 'PICTURE'.

*OBJPACK-OBJ_DESCR = 'Representation of object 138'.

*OBJPACK-DOC_SIZE = TAB_LINES * 255.

*APPEND OBJPACK.

  • Completing the recipient list

RECLIST-RECEIVER = 'youremail@sap.com'.

RECLIST-REC_TYPE = 'U'.

APPEND RECLIST.

*RECLIST-RECEIVER = 'SAPUSERNAME'.

*RECLIST-REC_TYPE = 'P'.

*APPEND RECLIST.

  • Sending the document

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

DOCUMENT_DATA = DOC_CHNG

PUT_IN_OUTBOX = 'X'

TABLES

PACKING_LIST = OBJPACK

OBJECT_HEADER = OBJHEAD

CONTENTS_BIN = OBJBIN

CONTENTS_TXT = OBJTXT

RECEIVERS = RECLIST

EXCEPTIONS

TOO_MANY_RECEIVERS = 1

DOCUMENT_NOT_SENT = 2

OPERATION_NO_AUTHORIZATION = 4

OTHERS = 99.

CASE SY-SUBRC.

WHEN 0.

WRITE: / 'Result of the send process:'.

LOOP AT RECLIST.

WRITE: / RECLIST-RECEIVER(48), ':'.

IF RECLIST-RETRN_CODE = 0.

WRITE 'The document was sent'.

ELSE.

WRITE 'The document could not be sent'.

ENDIF.

ENDLOOP.

WHEN 1.

WRITE: / 'No authorization for sending to the specified number',

'of recipients'.

WHEN 2.

WRITE: / 'Document could not be sent to any recipient'.

WHEN 4.

WRITE: / 'No send authorization'.

WHEN OTHERS.

WRITE: / 'Error occurred while sending'.

ENDCASE.

-


Cheers,

Vijay Raheja