Skip to main content
Known Participant
December 2, 2009
Question

cfmail using cfset

Hi everyone,

I've been racking my brain on this for hours and I still haven't been able to figure it out.  I feel like it should be fairly simple.

I've got a form that asks a user for an email address.  When the form is submitted, the data is sent to an email address.  If the user gives an email address, I want the recipient to be able to reply by hitting "reply" in their email client.  Here's my code:

<cfoutput>
<cfif '#Form.EmailAddress#' neq ''>
<cfset EmailFrom = '#Form.FirstName# #Form.LastName#<#Form.EmailAddress#>'><cfelse>
<cfset EmailFrom = '#Form.FirstName# #Form.LastName#'>
</cfif>
</cfoutput>

<cfmail to="recipient" from="#EmailFrom#" subject="Appointment Request">

Everything's working at about 75%. If a user provides an email address, the form process works perfectly.  However, if the user does not provide an email address, I simply want the "from" field in the email to output the first and last name.  If not email address is provided, the process still works, but the first and last names are not output in the email "from" field.  I get "<unknown sender>".

Does anyone have suggestions?  I also tried a cfif statement in the cfmail string, but CF throws an error when I try to the run the script.

Thanks!

    This topic is closed to new replies. Start a new post to keep the conversation going.

    7 replies

    ilssac
    Inspiring
    December 2, 2009

    The system is checking for an email address, not just a name.

    Translate
    Participating Frequently
    December 2, 2009

    Right before your cfmail tag and right before you close your cfoutput put in #EmailFrom# <cfabort> and see what is there.  I think the problem is in this line

    <cfif '#Form.EmailAddress#' neq ''>

    Take away the single quotes from the left side.  For that matter, you don't need the pound signs either.

    Translate
    straffenpAuthor
    Known Participant
    December 2, 2009

    mchedester,

    Thanks for the tip.  Even with quotes and pound signs gone, there is still no name in the "from" field.

    Translate
    Inspiring
    December 2, 2009

    As Ian implied, you need a valid email address in the From attribute of a cfmail tag.  Therefore, not only do you have to check that the form field is not empty, you have to ensure it's a valid email address.  You can do that on the form with validate="email" and on the action page with isValid().

    Translate