Wednesday 9 September 2015

Email multiple objects or attachments in ORACLE SMTP

Embedding multiple objects or attachments

Just as MIME requires some header information to declare how you're going to use it, it also wants to know when you are done.  Thus, if you embed multiple MIME blocks, each block is surrounded by the defined boundary tags.  The last block should have additional information in its tag to indicate the MIME blocks are done and normal email content will resume.  Many email clients are forgiving on this requirement and will simply read MIME blocks until they can't find anymore and don't really look for the end tag; but I don't recommend relying on this practice.  In the example above there is only one attachment so the first attachment is also the last and you can see the boundary is appended after the content and suffixed with two dashes.  The extra dashes are all that is needed to tell MIME you're done embedding blocks.  To send multiple attachments, it's simply a matter of writing the boundary before each block and describing what the content is.  As noted previously, the package provided below includes routines to make the multiple embeddings easier.  Its common practice for the MIME boundary to be a string of hexadecimal digits but it's not strictly necessary.  As seen in the next example, I've changed the boundary literally to the string "the boundary can be almost anything."


DECLARE
    v_connection             UTL_SMTP.connection;
    c_mime_boundary CONSTANT VARCHAR2(256) := 'the boundary can be almost anything';

    v_clob                   CLOB;
    v_len                    INTEGER;
    v_index                  INTEGER;
BEGIN
    SELECT encode_blob_as_clob(blobimage)
      INTO v_clob
      FROM my_images
     WHERE id = 1;

    v_connection := UTL_SMTP.open_connection(:p_smtp_server);
    UTL_SMTP.helo(v_connection, :p_domain);
    UTL_SMTP.mail(v_connection, :p_from);
    UTL_SMTP.rcpt(v_connection, :p_to);
    UTL_SMTP.open_data(v_connection);

    UTL_SMTP.write_data(v_connection, 'From: ' || :p_from || UTL_TCP.crlf);
    UTL_SMTP.write_data(v_connection, 'To: ' || :p_to || UTL_TCP.crlf);
    UTL_SMTP.write_data(v_connection, 'Subject: test blob' || UTL_TCP.crlf);
    UTL_SMTP.write_data(v_connection, 'MIME-Version: 1.0' || UTL_TCP.crlf);

    UTL_SMTP.write_data(
        v_connection,

No comments:

Post a Comment