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: promo@elysianrealestate.com\r\nContent-type: text/html\r\nReply-To: promo@elysianrealestate.com";

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

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

		$this->render('thanks');

	}	

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);



emailview.php




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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

			<html xmlns="http://www.w3.org/1999/xhtml">

			<head>

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

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

			</head>

			<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="http://www.elysianrealestate.com/images/logo.png" 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%;">

			<tr>

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

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

			<tr>

			<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>

			</tr>

			<tr>

			<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>

			</tr>

			<tr>

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

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

			</tr>

			</table>

			</td>

			</tr>

			</table>

			</div>

			</div>

			</div>

			</body>';

		$headers="From: promo@elysianrealestate.com\r\nContent-type: text/html\r\nReply-To: promo@elysianrealestate.com";

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

		$this->render('thanks');

	}

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, http://smtp4dev.codeplex.com/