In this hands on example, you are going to add email functionality to the contact form.
Tags Used: <cfmail>
- Open up the /www/contact.cfm file in your code editor.
- First we need to create a <cfmail> tag. Locate the line of code which reads 'Form submitted successfully!' on or around line 59.
Before the close tag but still inside the <cfelse> tag, create a <cfmail> tag with the following attributes:
- from: #form.email#
- to: email@example.com
- subject: Contact Request
- type: HTML
Once the tag is created, we need to create our email body. After the <cfmail> tag, write the following HTML code:
<h2>Contact Request</h2> <p> From: #form.contactName# (#form.email#) </p> <p> #form.message# </p>
- After the closing </p> tag, create a closing </cfmail> tag.
- Once the <cfmail> is created, set up the mail server in the ColdFusion Administrator. In your browser, navigate to your ColdFusion Administrator and login. The URL will most likely be http://localhost:8500/CFIDE/administrator/.
- Click on the 'Mail' link on the left hand side under 'Server Settings'.
- In the 'Mail Server' box, enter 'Localhost' and click 'Submit Changes'. All mail will now go through Localhost, which will allow for viewing in the 'undeliverable' folder.
- Open up the /www/contact.cfm page in your browser.
- Test the contact form and make sure the email is being sent. Fill out all the form fields and click 'Submit'.
- Go back to the Mail page in the ColdFusion Administrator.
- Click on the 'View Undeliverable Mail' button, which is under the Mail Spool Settings Heading.
- The first email in the list should be the email that was just generated by the Contact form. Click on it, if not already highlighted, and review the email body below the list. You have now successfully sent an email. When you have access to a mail server, you can update the mail settings in the ColdFusion Administrator and it will be sent out to the intended parties.