Wednesday, July 9, 2014

Formatting the email body in SNDSMTPEMM

sndsmtpemm email body

In December, 2013, I wrote a post about using the SNDSMTPEMM command to send emails from the IBM i, Email IFS files. It has become the most popular post on this blog, and the one that has generated the most interest.

Two of the most commonly asked questions has been:

  1. Can I send an email with a message in the email’s body and without an attachment?
  2. Can I format the text in the email’s body so it is not just plain text?

 

Can I send an email with a message in the email’s body and without an attachment?

Yes. You can send an email without an attachment. Just put the text you want to be in the email’s body in the NOTE field of the command, for example:

  SNDSMTPEMM RCP((my-address@email.com)) +
               SUBJECT('Alert: Purchase Order EDI') +
               NOTE('A purchase order has been received via +
                     EDI and processed')

Or if we want to make it a bit more complicated/useful:

01  PGM        PARM(&EMAIL &PONBR)
02  DCL        VAR(&EMAIL) TYPE(*CHAR) LEN(30) 
03  DCL        VAR(&PONBR) TYPE(*CHAR) LEN(10)
04  DCL        VAR(&NOTE) TYPE(*CHAR) LEN(200)

05  CHGVAR     VAR(&NOTE) +
                 VALUE('Purchase order ' || &PONBR |< +
                       ' has been received, via +
                       EDI, & processed')

06  SNDSMTPEMM RCP((&EMAIL)) + 
                 SUBJECT('Alert: PO received') + 
                 NOTE(&NOTE) 

07  ENDPGM

If you are sending an attachment you do not need to have text in the NOTE field, but you do when there is no attachment, as in the above examples.

Warning:  The documentation for this command states that "Up to 400 characters can be specified" in the NOTE field, but in my experience only the first 200 characters are sent.

 

Can I format the text in the email’s body so it is not just plain text?

I agree with the people who have asked me this question, the default format for the text in the NOTE field is pretty boring. It would make the email more appealing to the recipient if text in the body could be formatted. I can achieve this by using the CONTENT field in the command. The default value is *PLAIN, if I change the value to *HTML I can now use HTML to format the text. For example:

05  CHGVAR     VAR(&NOTE) +
                 VALUE('<font face="Arial"><h2><font +
                   color="red">Hello world</font></h2> +
                   This is an example of using <i>HTML</i> +
                   in the <b>body</b> of the email.</font>')

06   SNDSMTPEMM RCP((&EMAIL)) SUBJECT('Test email') +
                  NOTE(&NOTE) CONTENT(*HTML)

Alas with 200 characters there is not much you can say in the body of the email. But I can be use it to describe what an attachment is, or the parameters used to generate the data in the attached file.

 

You can learn more about the SNDSMTPEMM command from IBM’s command instructions here.

30 comments:

  1. I am unable to use the command. I use V5R9.
    I am getting following message: Command SNDSMTPEMM in library *LIBL not found.

    Do we need to install any patches from IBM to access this.

    ReplyDelete
    Replies
    1. As I mentioned in my first post about this command, Email IFS files:

      "SNDSMTPEMM has been introduced, via PTF, to IBM i 6.1 and 7.1"

      If your sever is using V5 then this command will not be available to you.

      Delete
  2. Simon, Wonderful tip. I enjoy your posts.
    1 question, the mail works internally, but not to outside domains. Any way to include those?

    ReplyDelete
  3. I have not been able to get this is work. I get QSMTPUSR in library *libl type *PNLGRP not found. I understand we need to usr WRKNAMSMTP, I dont know if that is set up correctly.

    ReplyDelete
    Replies
    1. Are you at release 6.1 or greater?
      Do you have the requisite PTF loaded?
      Could it be an authority problem that the command is there & you are not authorized to it?

      Delete
  4. You can get a 400 character Note by applying a PTF. It does work, but I can't find the PTF number at the moment

    ReplyDelete
  5. To get SNDSMTPEMM note field to use up to 400 chars apply the following PTF. Can be applied either immediate or delayed.
    V6R1 5761TC1 SI51161
    V7R1 5770TC1 SI51160

    ReplyDelete
  6. I have been using this command. It allows me to send email without attachment (without error message), but, it does not allow me to send email with attachment (the message is "Send E-mail Successful"). Any thoughts?

    ReplyDelete
    Replies
    1. You have not given any information about any error message you receive when you try to send an attachment. Without that it is hard to guess what might be your problem. Have you done what is described at the bottom of this post?

      Delete
    2. can the body be more then 400 char?

      Delete
    3. At present I believe the maximum is 400 characters. If needed any more than that then I would place the text in an attachment.

      Delete
  7. I'm trying to use a variable in cl pgm to have multiple email address some are *PRI and some *CC for the RCP parm
    DCL VAR(&RCP) TYPE(*CHAR) LEN(500)
    my value for field RCP is
    (xxxxx@xxxxxxxxxx.com *PRI) (yyyyy@yyyyyyyyy.net *PRI) (zzzzz@zzzzz.com *CC)
    but when I use this in the
    SNDSMTPEMM RCP((%TRIM(&RCP)))
    command it puts single quotes around the value and it causing an invalid email address
    SNDSMTPEMM RCP(('(xxxxx@xxxxxxx.com *PRI)
    (yyyyy@yyyyyyyyy.net *PRI) (zzzzz@zzzzzzz.com *CC)'))

    any way around this besides creating an RPG pgm to build the whole command

    ReplyDelete
    Replies
    1. I have to admit I have never used a %TRIM in that field. If you need to trim could you not into another variable and use that in the command?

      Delete
    2. You cannot use '(xxxxx@xxxxxxxxxx.com *PRI) (yyyyy@yyyyyyyyy.net *PRI) (zzzzz@zzzzz.com *CC)' as a variable value and then try to use the variable in a command. The command must see individual values for each subparameter. If you try to use a variable for all of the subparameters at the same time then the command uses the variable value for a single subparameter.

      Delete
    3. Esto funciona:

      CHGVAR VAR(&STRING) VALUE('SNDSMTPEMM RCP(' |< +
      &MAIL |< ' ) ' || 'SUBJECT(' || &CHR |< +
      &SUBJ |< &CHR || ') ' || 'NOTE(' || &CHR +
      |< &NOTE |< &CHR || ') ' || 'ATTACH((' || +
      &CHR |< &RUTA |< &CHR |< ' *OCTET *TXT))')

      CALL PGM(QCMDEXC) PARM(&STRING &LENGTH)

      Delete
    4. &CHR variable

      DCL VAR(&CHR) TYPE(*CHAR) LEN(2) VALUE(X'7D')

      Delete
  8. Can we use a carriage return in the Notes? I need to have the message in different lines on the mail

    ReplyDelete
    Replies
    1. If the content is HTML then <BR> is a carriage return/line feed.

      Delete

  9. Or if you want the content to be PLAIN text, you can define the EBCDIC hex carriage return line feed as X'0D25' as shown in the variable &MYCRLF below, then concatenate that value with your text...

    PGM
    DCL VAR(&BODY) TYPE(*CHAR) LEN(400) VALUE(' ')
    DCL VAR(&MYCRLF) TYPE(*CHAR) LEN(2) VALUE(X'0D25')
    CHGVAR VAR(&BODY) VALUE('Please see attached sample PDF +
    report, named "MyReport.pdf. |< &MYCRLF |< +
    &MYCRLF |< 'This e-mail is +
    a sample of PLAIN TEXT content with +
    forced CRLF after this paragraph.' |< +
    &MYCRLF |< &MYCRLF |< 'Jane Q. Programmer' |< +
    &MYCRLF |< '(999) 555-5555')
    SNDSMTPEMM RCP((johndoe@sampleemail.net) +
    (rpger@ipgmrs.net *CC)) +
    SUBJECT('Carriage Return Line Feed In E-Mail') +
    NOTE(&BODY) +
    ATTACH(('/MyPDFs/MYREPORT.PDF')) +
    CONTENT(*PLAIN)
    END: ENDPGM

    ReplyDelete
  10. Hello Simon, Greetings from Barcelona. I always enjoy your posts and they're great help for me, given the fact that i'm starting to work with ibm i like 3 months ago.

    Here's my question, is it posible to use a variable on the subject? like "Basic audit &sysname"?

    Hope you're doing fine, cheers!

    ReplyDelete
    Replies
    1. Firstly, let me say welcome to the world of IBM i. The word of advice I will give you as you start of journey on this operating system is to take time to "play". What I means by that is take time to experiment with things, like this.

      I would use a single variable in the subject field. Make it before hand. It could be something as simple as:

      CHGVAR VAR(&SUBJECT) +
      VALUE('Basic audit for ' || &SYSNAME)

      Then you can use &SUBJECT in the subject field.

      Good luck with your adventures.

      Delete
    2. I should have said you need to read this post to understand what the double pipes ( || ) do.

      Delete
  11. HELP!! This comnd always is me a "file not found" msg... even though I'm looking right at it! My syntax for the location of the document is '/Folder1/Folder2/Folder3/documentname.csv'
    Any ideas on whats wrong??

    ReplyDelete
    Replies
    1. The IFS is case sensitive make sure that the folders, and file, are the same case you see in the Ops Navigator.

      Delete
    2. SIMON!
      DAMN! THANX for the tip -- it's the only one I got --
      and I had HIGH hopes! My cases were not in agreement!
      I will pay attention to that in the future, so thanx!
      But it still fails. I don't get it. Anyway, thanx, again!

      "IFS File Not Found."
      Integrated File System File Not Found.
      Send E-mail Failed.
      Function check. TCP5092 unmonitored by EXCELTEST
      instruction X'0000'.

      SNDSMTPEMM RCP((RSTRATTON@ETHORN.COM)) SUBJECT('excel +
      report') +
      ATTACH(('/hornibmi/EXCEL/IT/EXPLOTS.CSV')) +
      CONTENT(*XML)

      Delete
    3. The only time I have seen a TCP5092 is when the user sending the email is not enrolled in the system directory.

      Adding to system directory

      Delete
  12. Hi , Is their a way we can get the email from specific email id. Any control we have on FROM: email id to tell command - SNDSMTPEMM.

    ReplyDelete

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.