cancel
Showing results for 
Search instead for 
Did you mean: 

xp_sendmail with inline attachment

dhkom
Participant
0 Kudos
566

I'm having trouble getting the following xp_sendsmtp documentation example working: 'This example uses SMTP to send inline HTML formatted message with a signature and two attachments, one of which is a ZIP file.'

The slightly modified example, with obvious placeholders, is shown below.

Suggestions or insights welcome. Thank you!


begin
  declare l_rc                  integer;
    declare s_content                           long varchar;
  --
    SET s_content = 
        'Content-Type: multipart/mixed; boundary="xxxxx";\\n' ||
        'This part of the email should not be shown.  If this ' ||
        'is shown then the email client is not MIME compatible\\n\\n' ||
        '--xxxxx\\n' ||
        'Content-Type: text/html;\\n' ||
        'Content-Disposition: inline;\\n\\n' ||
        'Plain text.' ||
        /* xp_read_file( '\\\\temp\\\\johndoe.sig.html' ) || */
        '--xxxxx\\n' ||
        'Content-Type: application/zip; name="test.zip"\\n' ||
        'Content-Transfer-Encoding: base64\\n' ||
        'Content-Disposition: attachment; filename="test.zip"\\n\\n' ||
        base64_encode( xp_read_file( 'c:\\\\temp\\\\test.zip' ) ) ||
        '\\n\\n' ||
        '--xxxxx--\\n'
    ; 
    --
    -- Port 2525 is for non-ssl email
    l_rc = call xp_startsmtp (
        smtp_sender = '(sender email)',
        smtp_server = '(mail server)',
        smtp_port = 2525,
        smtp_sender_name = 'John Doe',
        smtp_auth_username = '(sender email)',
        smtp_auth_password = '(password)'
    );
    --
  l_rc = call xp_sendmail (
        recipient = 'joe@contoso.com', 
        subject = 'Test sending email with attachment from SQL Anywhere', 
        "message" = s_content,
        content_type = 'ASIS',
        include_file = '\\\\temp\\\\bla.sql'
    );
    --
  l_rc = call xp_stopsmtp ();
end
go

View Entire Topic
PCollins
Participant

We have had all sorts of issues with include_file in recent years, particularly when the emails are received / handled by Exchange/Outlook, the problem being content isn't shown.

These days we now don't use include_file and attached everything "manually" using repeated part sections in the email content build up

dhkom
Participant
0 Kudos

Thanks. Per my comment above, "content isn't shown" is consistent with what we are running into. "include_file" works fine for us if the "content" value string is just text, including HTML.

It would be great if you could post some examples of "manually using repeated part sections in the email content build up". We're not expert on this. We want the email message to be HTML content to display in a body, plus an attachment file, ideally without having to create the file and then use "include_file" to attach it.

PCollins
Participant

This is a subset of a procedure that emails various documents which are held in variables (Doc1, Doc2, Doc3) with inline image in the signature.

I'm not saying it is perfect but it works 😉

Hope this helps (my forum foo formatting isn't very good)

set "vBody" = 'Content-Type: multipart/mixed; boundary="xxxxx";\
'
  || 'This part of the email should not be shown.  If this '
  || 'is shown then the email client is not MIME compatible\
\
'
  || '--xxxxx\
'
  || 'Content-Type: text/html;\
'
  || 'Content-Disposition: inline;\
\
'
  || '`<html>\
<head></head>\
<body>

Docs received

These docs have been received.

\ ' || '

Doc1: ' || "Doc1" || '.

'` || "xp_read_file"('D:\\\\system-email-sig.htm') || '--xxxxx\ ' || 'Content-Type: application/png; name="image001.png"\ ' || 'Content-Transfer-Encoding: base64\ ' || 'Content-Disposition: attachment; filename="image001.png"\ \ ' || "base64_encode"("xp_read_file"('D:\\\\image001.png')) || '\ \ '; if "Doc1" is not null then set "vBody" = "vBody" || '--xxxxx\ ' || 'Content-Type: application/pdf; name="Doc1.pdf"\ ' || 'Content-Transfer-Encoding: base64\ ' || 'Content-Disposition: attachment; filename="Doc1.pdf"\ \ ' || "base64_encode"("Doc1") || '\ \ ' end if; if "Doc2" is not null then set "vBody" = "vBody" || '--xxxxx\ ' || 'Content-Type: application/pdf; name="Doc2.pdf"\ ' || 'Content-Transfer-Encoding: base64\ ' || 'Content-Disposition: attachment; filename="Doc2.pdf"\ \ ' || "base64_encode"("Doc2") || '\ \ ' end if; if "Doc3" is not null then set "vBody" = "vBody" || '--xxxxx\ ' || 'Content-Type: application/pdf; name="Doc3.pdf"\ ' || 'Content-Transfer-Encoding: base64\ ' || 'Content-Disposition: attachment; filename="Doc3.pdf"\ \ ' || "base64_encode"("Doc3") || '\ \ ' end if; set "vBody" = "vBody" || '--xxxxx--\ '; call "xp_startsmtp"('noreply@domain.comm','mail.domain.com'); call "xp_sendmail"("recipient" = 'ops@domain.com',"subject" = 'Duplicate PreAlert processed',"message" = "vBody","content_type" = 'ASIS'); call "xp_stopsmtp"();
VolkerBarth
Contributor
0 Kudos

Oops, I had thought a pre-tag would be of help but it apparently does not work well with code with included HTML tags...