FM to Validate Email Address in SAP

Use FM to valid email address in SAP.

FM name: SX_INTERNET_ADDRESS_TO_NORMAL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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(returntype 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

1
2
3
4
5
6
7
8
9
10
11
12
13
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