Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Custom contact form sending empty email


Go to End


8 Posts   6779 Views

Avatar
pingu

Community Member, 75 Posts

2 April 2009 at 1:15am

Hi all,

I've got a custom email form that's sending empty emails. I'm guessing I'm not setting the template correctly - would appreciate some help with this. The template is located under the /email/ folder under mytheme/templates.

function doSubmit($postedData, $form) {
$subject = "Website Enquiry: ".$postedData["Subject"];
$email = new Email();
$email->setSubject($subject);
$email->ss_template = "ContactPageEmail";
$email->setFrom('noreply@mydomain.com');
$email->setTo('to@mydomain.com');
$email->populateTemplate($postedData);
$email->send();

$form->sessionMessage('Thanks for contacting us. We\'ll get in touch with you as soon as possible!');
Director::redirectBack();
}

Avatar
pingu

Community Member, 75 Posts

6 April 2009 at 11:43am

Hi guys,

Any ideas on this??
My email is still sending empty :(

Avatar
Howard

Community Member, 215 Posts

6 April 2009 at 12:00pm

Try this, change:

$email->ss_template = "ContactPageEmail";
to
$email->setTemplate('ContactPageEmail');

and if that doesn't work put the template in /mysite/templates/

Hope that helps!

Avatar
Carbon Crayon

Community Member, 598 Posts

27 April 2009 at 3:16am

Edited: 27/04/2009 3:17am

Hi guys

has anyone found a solution to this yet? I can't get the templates to work in any way, I just get an empty email.

Has anyone got email templates working in 2.3X? I'm trying to write a tutorial on creating a contact form but without templates it makes it pretty crude.

?

Avatar
petebd

Community Member, 16 Posts

27 April 2009 at 6:14pm

This kind of works for me:

I put the following method in my Page_Controller:

function SendEmail($request) { 
$subject = "Website Enquiry: ".$request["Subject"]; 
$email = new Email(); 
$email->setSubject($subject); 
$email->setTemplate("ContactPageEmail"); 
$email->setFrom('noreply@mydomain.com'); 
$email->setTo('to@mydomain.com'); 
//$email->populateTemplate($postedData); 
$email->send();

$form->sessionMessage('Thanks for contacting us. We\'ll get in touch with you as soon as possible!'); 
Director::redirectBack(); 
}

Then I put the following template code in mysite/templates/ContactPageEmail.ss:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h2>$Subject</h2>
</body>
</html>

And if I go to url http://localhost/silverstripe/home/sendemail it blows up at the actual sending (as I don't have mail setup correctly) but it does create the template correctly as I checked both in Eclipse debugger and as you can see from the trace output on the page:

mail(to@mydomain.com,Website Enquiry: , This is a multi-part message in MIME format. ------=_NextPart_1393414 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Insert title here Website Enquiry: ------=_NextPart_1393414 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html><!-- template D:/wamp/www/silverstripe/mysite/templates/ContactPageEmail.ss --> <head> <meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3DISO-8859-1"> <title>Insert title here</title> </head> <body> =09<h2>Website Enquiry: </h2> </body> </html><!-- end template D:/wamp/www/silverstripe/mysite/templates/ContactPageEmail.ss --> ------=_NextPart_1393414--,MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_1393414" Content-Transfer-Encoding: 7bit From: noreply@mydomain.com X-Mailer: SilverStripe Mailer - version 2006.06.21 (Sent from "localhost") X-Priority: 3 X-SilverStripeBounceURL: localhost/SilverStripe/Email_BounceHandler X-SilverStripeSite: mysite )
Line 157 of Mailer.php

You'll note that I didn't yet call populateTemplate() on the email. If I get time I'll see if I can put together a full working version today.

Pete

Avatar
Carbon Crayon

Community Member, 598 Posts

4 May 2009 at 4:30am

Thanks Pete

I descovered that the template has to be in themes/yourtheme/templates for the email to work.

Anyway all is fine now, you can see the tutorial here: http://www.ssbits.com/creating-a-simple-contact-form/

Avatar
jack12

Community Member, 1 Post

26 August 2010 at 5:05am

Thanks petebd.. the solution you have provided works so good for me.

Thanks for sharing and good to notice this thread.

..
email marketing

Avatar
werehamster

Community Member, 3 Posts

10 October 2010 at 9:08pm

I've also discovered that the template file needs to be an HTML file, plain text doesn't appear to work as an email template
so the following in your ContactEmail.ss file

From: $Name ($Email)
Message: $Comments

Results in an empty email. But using

<html>
<body>
<pre>
From: $Name ($Email)
Message: $Comments
</pre>
</body>
</html>

Appears to work correctly.

Hope this helps someone.