Skip to main content
July 21, 2009
Question

CFMAIL Attached vs. embedded (cfmail, multipart/mixed)

  • July 21, 2009
  • 2 replies
  • 6965 views

I want to send a mail which has a PDF attachment and an embedded image. When I send them both with cfmailparam (with correct disposition) they arrive in the email as attachments in the body...you don't get the little email paperclip attachment icon.

When I tried using mimeattach for the PDF it did work!...but the image denoted with cfmailparam also came through like that. So I looked the email headers of both and the good one was Content-Type: multipart/mixed; while the bad one was "multipart/related".

So how can I make cfmailparam send it as multipart/mixed? (if that's even the right question...maybe I should stick with mimeattach and do something else?)

Thanks Folks' - Randy

    This topic has been closed for replies.

    2 replies

    Participant
    December 29, 2011

    Has somebody found a solution for this problem?

    I have tried this:
    "What you really need for your problem is both. You need an outer MIME message of type multipart/mixed which contains the attachment and an inner part. The inner part should be of type multipart/related and should contain the text message and the inline image. I am not sure whether it is possible to construct a message like that in CF, you would have to nest cfmailpart tags inside eachother..  Jochem"

    But that doesn't seems to work either.

    Participant
    January 3, 2012

    I never found a solution using cfmail.  I had to resort to java instead.

    I have all of my file information (including content) in a query called qFiles and loop through that in the code below.

    <cfscript>

        // config will stay this way

        emailServer = serverAddressHere;

        emailServerAccount = "";

        emailServerPwd = "";

        //  set email variables

        vSentFrom = sentFromEmailHere;

        vSubjectText = subjectHere;

        recipientsTo = listToArray( SentToListHere );

        recipientsCC = listToArray( CCListHere );

        recipientsBCC = listToArray( BCCListHere );

        // set javamail properties

        props = createObject("java", "java.util.Properties").init();

        props.put("javax.mail.smtp.host", emailServer);

       

        // get static recipient types

        recipientType = createObject("java", "javax.mail.Message$RecipientType");

       

        // create the session for the smtp server

        mailSession = createObject("java", "javax.mail.Session").getInstance(props);

       

        // create a new MIME message

        mimeMsg = createObject("java", "javax.mail.internet.MimeMessage").init(mailSession);

       

        // create the to and from e-mail addresses

        addrFrom = createObject("java", "javax.mail.internet.InternetAddress").init(vSentFrom);

        for (cfIdx = 1; cfIdx LTE arrayLen( recipientsTo ); cfIdx++)

        {   

            addrTo[cfIdx] = createObject("Java", "javax.mail.internet.InternetAddress").init( recipientsTo[cfIdx] );

            // add a recipient

            mimeMsg.addRecipient(recipientType.TO, addrTo[cfIdx]);

        }

        if(ArrayLen(recipientsCC)){

            for (cfIdx = 1; cfIdx LTE arrayLen( recipientsCC ); cfIdx++)

            {

                addrCC[cfIdx] = createObject("Java", "javax.mail.internet.InternetAddress").init( recipientsCC[cfIdx] );

                // add a recipient

                mimeMsg.addRecipient(recipientType.CC, addrCC[cfIdx]);

            }

        }

        if(ArrayLen(recipientsBCC)){

            for (cfIdx = 1; cfIdx LTE arrayLen( recipientsBCC ); cfIdx++)

            {

                addrBCC[cfIdx] = createObject("Java", "javax.mail.internet.InternetAddress").init( recipientsBCC[cfIdx] );

                // add a recipient

                mimeMsg.addRecipient(recipientType.BCC, addrBCC[cfIdx]);

            }

        }

       

        // build message

        // set who the message is from

        mimeMsg.setFrom(addrFrom);

        // set the subject of the message

        mimeMsg.setSubject(vSubjectText);

       

        // create multipart message: only needed if you're including both plain/text and html

        // or using attachments

        multipart = createObject("java", "javax.mail.internet.MimeMultipart").init();

       

        // specifies that the message contains both inline text and html, this is so that

        // images given a cid will show up when rendered by the e-mail client

        multipart.setSubType("mixed");

        // create html text multipart

        oHtml = createObject("java", "javax.mail.internet.MimeBodyPart").init();

        // add the html content (the setText() method shortcut/only works for "plain/text")

        oHtml.setContent( emailContentHTML, "text/html");

        // add the body part to the message

        multipart.addBodyPart(oHtml);

       

        // loop over files to attach to the email

        for ( intRow = 1 ; intRow LTE qFiles.RecordCount ; intRow = (intRow + 1))

        {

        // set file info to vars

        fileContent = qFiles["fileContent"][intRow];

        fileMimeType = qFiles["mimeType"][intRow];

        fileName = rereplace( qFiles["filename"][intRow] , '(?!\.[^.]*$)\W' , '' , 'all' );

        if(len(fileName) > 50)

        {fileName = mid(fileName,1,44) & right(fileName,find('.',reverse(fileName)));}

        // attach an inline binary object

        att = createObject("java", "javax.mail.internet.MimeBodyPart").init();

        // create an octet stream out of the binary file

        os = createObject("java", "org.apache.axis.attachments.OctetStream").init(fileContent);

        // we now convert the octet stream into the required data source. using an octet stream

        // allows us pass in any binary data as a file attachment

        osds = createObject("java", "org.apache.axis.attachments.OctetStreamDataSource").init("", os);

        // initialize the data handler using the data source

        dh = createObject("java", "javax.activation.DataHandler").init(osds);

        // pass in the binary object to the message--javamail will handle the encoding

        // based on the headers

        att.setDataHandler(dh);

        // define this binary object as a PDF

        att.setHeader("Content-Type", fileMimeType);

        // make sure the binary data gets converted to base64 for delivery

        att.setHeader("Content-Transfer-Encoding", "base64");

        // specify the binary object as an attachment

        att.setHeader("Content-Disposition", "attachment");

        // define the name of the file--this is what the filename will be in the e-mail client

        att.setFileName(fileName);

        // add the body part to the message

        multipart.addBodyPart(att);

        }

        //end loop through qFiles

        

        // place all the multi-part sections into the body of the message

        mimeMsg.setContent(multipart);

       

        // in this section we'll build the message into a string. you could dump

        // the string to a file in most SMTP server's queue file for delivery

        // this is exactly what would be pass to the SMTP server

       

        // create a bytearray for output

        outStream = createObject("java", "java.io.ByteArrayOutputStream");

        // create a budder for the output stream

        outStream.write(repeatString(" ", 1024).getBytes());

        // save the contents of the message to the output stream

        mimeMsg.writeTo(outStream);

        // save the contents of the message to the sMailMsg variable

        sMailMsg = outStream.toString();

        // reset the output stream (for stability)

        outStream.reset();

        // close the output stream

        outStream.close();

       

        // create a transport to actually send the message via SMTP

        transport = mailSession.getTransport("smtp");

        // connect to the SMTP server using the parameters supplied; use

        // a blank username and password if authentication is not needed

        transport.connect(emailServer, emailServerAccount, emailServerPwd);

       

        // send the message to all recipients

        transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());

        // close the transport

        transport.close();

        </cfscript>

    Participating Frequently
    November 5, 2012

    I just wanted to bump this back to the top and see if anyone else had figured out a solution for this with cfmail? We have clients using iPhone/iPads, etc that are experiencing issues where they do not see attachments. In addition, clients like Thunderbird don't include the attachments when forwarding messages. We'd like to get this resolved.

    Dileep_NR
    Inspiring
    July 22, 2009

    Hi,

    Cloud you please try the following,

    <cfmail .....           type="text/html">
                           
        <cfmailparam file="./attachments/my.pdf" disposition="attachment">    
        <cfmailparam file="./attachments/thumbnails/test.jpg"
                    disposition="inline" contentID="image1">   
          
            <img src="cid:image1"><br>
       
           
           
        </cfmail>

    ------

    Please attach the code set that you have tried

    July 22, 2009

    Thank you for the quick reply... is it possible this is something to do with the host or CF admin config?

    I tried your code (it seemed to be close to what I had) exactly as here...

    ====================================

    <cfmail to="...." from="...." subject="mime test3">
     
          <cfmailparam file="#ExpandPath('./....the path....')#" disposition="attachment">   
        <cfmailparam file="#ExpandPath('./...the path...')#" disposition="inline" contentID="image1">  
         
            <img src="cid:image1"><br>
           
            Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
    </cfmail>

    ====================================

    I also tried multipart,...and also with type="text/html" in the CFMail header.... all of them come through with the file in the body and with Content-Type:multipart/related.

    Now, on your email to me, from catchme_dileep, there was the paperclip showing and I again confirmed the Content-Type: multipart/mixed;

    So what determines that Content-Type?  When I send from my host it's multipart/related, from you it's multipart/mixed. (file attached showing your email and mine...you get the paperclip....important because I'm told Outlook doesn't seem the attachments in the body).

    (What's mime is yours?)

    July 22, 2009

    After further, and further, with the very great help from my webhost, (YoHost , Brett, always remarkably quick to reply and helpful. 4 stars), it seems that when a contentID is specified the email comes through with a multipart/related (which causes the attached file to be accessible only after opening the email...Eudora shows the paper clip, Yahoo doesn't, but regardless the file doesn't end up on the desktop as with multipart/mixed.)

    I even tried specifying explicitly as such (but no dice)...

      <cfmailpart type="multipart/mixed">
          <cfmailparam file="#ExpandPath('hello.pdf')#" disposition="attachment">   
        <cfmailparam file="#ExpandPath('folder.gif')#" disposition="inline" contentID="image1">  
         
            <img src="cid:image1"><br>
           
            Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
         </cfmailpart>

    So that's that for now I think ...enough time invested for all and I'll want to hear sometime perhaps from the folks at the source.