on 2024 Aug 19 4:26 PM
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
Request clarification before answering.
Hi,
I have been looking at this again and I can confirm we still have issues using Exchange Server as the first SMTP server. We now send all email out via either a Linux MTA or through Mimecast, both of which forward onto Exchange and the result is the email displays correctly.
We spent hours playing with content build up and never manage to get Exchange / Outlook to display anything when Exchanged was used as the initial SMTP server.
The headers show it thinks the mail is text/plain every time so there is clearly something weird going on with the data it parses in the receipt. But short of putting wireshark on the exchange server we ran out of ideas.
Anyway this is a complete example that works although note some deliberate spaces in some html tags so they display here:
begin declare vBody long varchar; set "vBody" = 'Content-Type: multipart/mixed; boundary="mixed_boundary"\ \ ' || '--mixed_boundary\ ' || 'Content-Type: text/html; charset="utf-8"' || 'Content-Transfer-Encoding: 8bit\ \ ' || '< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">< html>< body style="font-family: consolas, monospace;"><h3 style="font-size:10.0pt; font-family:Arial,Sans-serif;">Email with multiple attachments</h3><p>This is an example email with multiple attachments.</p>\ ' || '<p>Some other line here.</p><img border=0 width=148 height=71 src="cid:50eef9fb" alt="image001.png"">< /body>< /html>\ \ ' || '--mixed_boundary\ ' || 'Content-Type: image/png;\ name="image001.png"\ ' || 'Content-Description: image001.png\ ' || 'Content-Disposition: inline; filename="image001.png";\ ' || 'Content-ID: <50eef9fb>\ ' || 'Content-Transfer-Encoding: base64\ \ ' || "base64_encode"("xp_read_file"('D:\\\\Scripts\\\\image001.png')) || '\ \ ' || '--mixed_boundary\ ' || 'Content-Type: application/pdf;\ name="DOC1.pdf"\ ' || 'Content-Transfer-Encoding: base64\ ' || 'Content-Disposition: attachment;\ filename="DOC1.pdf"\ \ ' || "base64_encode"("xp_read_file"('D:\\\\Scripts\\\\DOC1.pdf')) || '\ \ ' || '--mixed_boundary\ ' || 'Content-Type: application/pdf;\ name="DOC2.pdf"\ ' || 'Content-Transfer-Encoding: base64\ ' || 'Content-Disposition: attachment;\ filename="DOC2.pdf"\ \ ' || "base64_encode"("xp_read_file"('D:\\\\Scripts\\\\DOC2.pdf')) || '\ \ ' || '--mixed_boundary\ ' || 'Content-Type: application/pdf;\ name="DOC3.pdf"\ ' || 'Content-Transfer-Encoding: base64\ ' || 'Content-Disposition: attachment;\ filename="DOC3.pdf"\ \ ' || "base64_encode"("xp_read_file"('D:\\\\Scripts\\\\DOC3.pdf')) || '\ \ ' || '--mixed_boundary--'; call "xp_startsmtp"('example@domain.com','smtp.domain.com'); call "xp_sendmail"("recipient" = 'user@domain.com',"subject" = 'Multi Attachement Email',"message" = "vBody","content_type" = 'ASIS'); call "xp_stopsmtp"(); end
If you want to include a plain text version as well this should work:
begin declare vBody long varchar; set "vBody" = 'Content-Type: multipart/mixed; boundary="mixed_boundary"\ \ ' || '--mixed_boundary\ ' || 'Content-Type: multipart/related; boundary="related_boundary"\ \ ' || '--related_boundary\ ' || 'Content-Type: multipart/alternative; boundary="alternative_boundary"\ \ ' || '--alternative_boundary\ ' || 'Content-Type: text/plain; charset="us-ascii"\ ' || 'Content-Transfer-Encoding: 8bit\ \ ' || 'Plain Text says Hello\ \ ' || '--alternative_boundary\ ' || 'Content-Type: text/html; charset="utf-8"' || 'Content-Transfer-Encoding: 8bit\ \ ' || '< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">< html>< body style="font-family: consolas, monospace;"><h3 style="font-size:10.0pt; font-family:Arial,Sans-serif;">Email with multiple attachments</h3><p>This is an example email with multiple attachments.</p>\ ' || '<p>Some other line here.</p><img border=0 width=148 height=71 src="cid:50eef9fb" alt="image001.png"">< /body>< /html>\ \ ' || '--alternative_boundary--\ \ ' || '--related_boundary\ ' || 'Content-Type: image/png;\ name="image001.png"\ ' || 'Content-Description: image001.png\ ' || 'Content-Disposition: inline; filename="image001.png";\ ' || 'Content-ID: <50eef9fb>\ ' || 'Content-Transfer-Encoding: base64\ \ ' || "base64_encode"("xp_read_file"('D:\\\\Scripts\\\\image001.png')) || '\ \ --related_boundary--\ \ ' || '--mixed_boundary\ ' || 'Content-Type: application/pdf;\ name="DOC1.pdf"\ ' || 'Content-Transfer-Encoding: base64\ ' || 'Content-Disposition: attachment;\ filename="DOC1.pdf"\ \ ' || "base64_encode"("xp_read_file"('D:\\\\Scripts\\\\DOC1.pdf')) || '\ \ ' || '--mixed_boundary\ ' || 'Content-Type: application/pdf;\ name="DOC2.pdf"\ ' || 'Content-Transfer-Encoding: base64\ ' || 'Content-Disposition: attachment;\ filename="DOC2.pdf"\ \ ' || "base64_encode"("xp_read_file"('D:\\\\Scripts\\\\DOC2.pdf')) || '\ \ ' || '--mixed_boundary\ ' || 'Content-Type: application/pdf;\ name="DOC3.pdf"\ ' || 'Content-Transfer-Encoding: base64\ ' || 'Content-Disposition: attachment;\ filename="DOC3.pdf"\ \ ' || "base64_encode"("xp_read_file"('D:\\\\Scripts\\\\DOC3.pdf')) || '\ \ ' || '--mixed_boundary--'; call "xp_startsmtp"('example@domain.com','smtp.domain.com'); call "xp_sendmail"("recipient" = 'user@domain.com',"subject" = 'Multi Attachement Email',"message" = "vBody","content_type" = 'ASIS'); call "xp_stopsmtp"(); end
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for all the work you put in on this!
The SMTP server I'm using is from a Linux Shared Hosting account. I'm not an expert on this "stuff", but I'm guessing that wouldn't be Exchange.
Some problems we have the need and/or time to "put in what it takes" to solve, but not this one right now. We went ahead to create a temporary file for the attachment, and then referenced it with an "include_file" parameter to xp_sendmail. This works.
I'll keep your code handy in case we need to revisit this.
pcollins - regarding your comment, above, "we still have issues using Exchange Server as the first SMTP server" - I just ran into issues too.
I use xp_sendmail specifying parameters "message" and "include_file", using the exact same SQL Anywhere version (16.0.0.2798), and get different results depending on the SMTP server:
1) Using the SMTP Server of a Linux Shared Hosting account, the received email includes a body and attachment.
2) Using the SMTP Server of my customer, almost certainly Exchange, the received email has NO body but does have an attachment.
Fortunately, my customer will be happy with receiving two emails: one with an email body, and the other just an attachment.
If I had to get around this, I'm thinking of invoking a .NET DLL to send the email. I believe from SA16, at least, this would have to be an old .NET Framework DLL. I'm just thinking of a potential contingency.
User | Count |
---|---|
79 | |
29 | |
9 | |
9 | |
9 | |
7 | |
6 | |
6 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.