Search code examples
csvcoldfusioncfmail

Changing a txt attachment to csv format in coldfusion


I am working with a ColdFusion file which will send a email with an attachment when a user exports data from a system. Currently the attachment is in .txt format which is difficult to interpret and is therefore quite unhelpful. I am trying to figure out how I can change this so the attachment sent in the email is in .csv format. This is the section of code that I believe needs to be altered:

    <cfset FileName = "#request.uncPath#\IncidentSummarySystem\data\" & #variable.UserName# & "-" &#DateFormat(now(), "ddmmyyyy")# & "-" & #TimeFormat(now(), "HHmmss")# & ".txt">  <cffile action="Write" file=#FileName# output="#FileData#">
<cfmail to="#rsUsers.qryUserEmail#" from="IncidentSummarySystem@" subject="Incident Summary System Export File" mimeattach="#FileName#">
    DO NOT REPLY TO THIS E-MAIL.
    Incident Summary System:  Your report is attached as a text (.txt) file.
</cfmail>
<script language="JavaScript">
    window.alert ("The information you have requested will be sent to you via e-mail.");
</script>
<cffile action="Delete" file="#FileName#">

Is it possible to change the attachment file type to .csv and how would I go about doing this?


Solution

  • Instead of using mimeattach, use cfmailparam to attach the file. If it's a simple CSV file with a .txt extension, change it to .csv when you create it. Then, specify the mime-type in the type attribute of cfmailparam to application/vnd.ms-excel.

    <cfmail 
        to = "recipient"
        subject = "message subject"
        from = "sender"
            more attributes... >
        <cfmailparam 
            contentID = "content ID"
            disposition = "disposition type"
            file = "filename" 
            type ="media type">
        ...
    </cfmail>