FM to Validate Email Address in SAP

Use FM to valid email address in SAP.

FM name: SX_INTERNET_ADDRESS_TO_NORMAL

class zcl_email definition
  public
  final
  create public .

  public section.

    class-methods bapiret2_from_sy
      returning
        value(rs_bapiret2) type bapiret2 .
    class-methods validate_email
      importing
        !email_type    type sx_addr_type default 'INT'
        !email_address type sx_addr
      returning
        value(return)  type bapiret2 .
  protected section.
  private section.
endclass.

class zcl_email implementation.

  method bapiret2_from_sy.
    data: msg type bapiret2.

    msg-id         = sy-msgid.
    msg-number     = sy-msgno.
    msg-type       = sy-msgty.
    msg-message_v1 = sy-msgv1.
    msg-message_v2 = sy-msgv2.
    msg-message_v3 = sy-msgv3.
    msg-message_v4 = sy-msgv4.

    message id sy-msgid type sy-msgty number sy-msgno
       with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
       into msg-message.

    rs_bapiret2  = msg .

  endmethod.


  method validate_email.

    data address_unstruct               type sx_address.

    address_unstruct-type    = email_type .
    address_unstruct-address = email_address .

    call function 'SX_INTERNET_ADDRESS_TO_NORMAL'
      exporting
        address_unstruct    = address_unstruct
      exceptions
        error_address_type  = 1
        error_address       = 2
        error_group_address = 3.

    if sy-subrc ne 0 .
      return = bapiret2_from_sy( ) .
    endif.

  endmethod.
endclass.

report to test

report ztest1.

parameters : email type sx_addr obligatory .

start-of-selection .

  data(return) = zcl_email=>validate_email( 
                     exporting email_address = email ) .
  if return-type ca 'EAX' .
    write : return-message .
  else.
    write : 'email address: ' && email && ' ok' .
  endif.

Leave a Reply