Sending HTML Email on Form Submission

In my index view file I have an enquiry form from which the user can receive eamil on form submission

In the form tag I have set the action as: <form method="post" action="site/thanks"

In controller I have created a function:

public function actionThanks()


		$headers="From:\r\nContent-type: text/html\r\nReply-To:";

		mail($_POST['txtEmail'],"Home Page Enquiry",$body,$headers);

		$this->render('error', $error);



I want to send an html email. The html message is an html file emailcontent.html. Do I need to make it a php file or keep it as html. Also, how do I pass it $_POST data coming from index view ? And use the html file content as $body in the mail() function. Do I need to create another function in the controller ? thanks.php is the view file to be shown when the email is sent.

Use renderPartial and return it as string,

$this->renderPartial('emailview', array(

  'from' => $_POST['from'],

  'enquiry' => $_POST['enquiry']

), true);


Message from <?php echo $from; ?><br />

Enquiry: <?php echo $enquiry; ?>

In controller I have tried this:

$body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

			<html xmlns="">


			<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

			<title>Request a call back : Emailer</title>


			<body style="background:#BA954E; margin:0 auto;padding:0px;font-family:Arial, Helvetica, sans-serif;">                                 

			<div style="width:100%;text-align:center;font-family:Arial, Helvetica, sans-serif;">

			<div style="width:640px;text-align:left;margin:0 auto; background:#3C4976; border:1px solid #ffffff; padding:25px 15px;margin-top:50px;">

			<div style="margin-bottom:20px;"><img src="" alt="" /></div>

			<div style="color:#363435; font-size:16px; font-weight:bold; background:#BA954E;padding:5px; border:1px solid #ffffff; color:#fff">Home Page Enquiry</div>

			<div style="color:#363435; padding:0px; margin:20px 0px 0px 0px; font-size:12px;">

			<table cellpadding="0" cellspacing="0" border="0" style="width:100%;">


			<td style="background-color:#BA954E;">

			<table cellpadding="2" cellspacing="2" style="width:100%; border:2px solid #3C4976; background-color:#BA954E; color:White;">


			<td style="background-color:#3C4976; padding:3px 10px; width:40%;">First Name</td>

			<td style="background-color:#3C4976; padding:3px 10px; width:60%;">'. $_POST['txtYourName'].'</td>



			<td style="background-color:#3C4976; padding:3px 10px; width:40%;">Last Name</td>

			<td style="background-color:#3C4976; padding:3px 10px; width:60%;">'.$_POST['txtYourName'].'</td>



			<td style="background-color:#3C4976; padding:3px 10px;">Email Address</td>

			<td style="background-color:#3C4976; padding:3px 10px;">'.$_POST['txtEmail'].'</td>










		$headers="From:\r\nContent-type: text/html\r\nReply-To:";

		mail($_POST['txtEmail'],"Home Page Enquiry",$body,$headers);



but I’m getting this error: Undefined index: txtYourName

In index.php I have <form method="post" action="site/thanks" onsubmit="return validate();"> and <input type="text" value="Name: *" size="35" name="txtYourName" id="txtYourName" …, then why am I getting this error ?

Did you put any text in the txtYourName input?

I would try using Firefox with Firebug to see what gets sent to the site.

This was because I was directly accessing that page by changing my url because the action="site/thanks" was taking me to localhost/mysite/site/thanks Not found page instead of mysite/index.php/site/thanks

Now that I’m using action=“index.php/site/thanks”, I get this error

mail() [<a href=‘function.mail’>function.mail</a>]: Failed to connect to mailserver at &quot;localhost&quot; port 25, verify your &quot;SMTP&quot; and &quot;smtp_port&quot; setting in php.ini or use ini_set()

Maybe it will work after I upload the files and test on server

This is a handy app for testing emails locally,