Jump to:

2012 Posts in 1438 Topics by 621 members

Form Questions

SilverStripe Forums » Form Questions » Custom contact form sending empty email

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

Page: 1
Go to End
Author Topic: 4160 Views
  • pingu
    Avatar
    Community Member
    75 Posts

    Custom contact form sending empty email Link to this post

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

  • pingu
    Avatar
    Community Member
    75 Posts

    Re: Custom contact form sending empty email Link to this post

    Hi guys,

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

  • Howard
    Avatar
    Community Member
    215 Posts

    Re: Custom contact form sending empty email Link to this post

    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!

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Custom contact form sending empty email Link to this post

    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.

    ?

  • petebd
    Avatar
    Community Member
    15 Posts

    Re: Custom contact form sending empty email Link to this post

    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

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Custom contact form sending empty email Link to this post

    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/

  • jack12
    Avatar
    Community Member
    1 Post

    Re: Custom contact form sending empty email Link to this post

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

    Thanks for sharing and good to notice this thread.

    ..
    email marketing

  • werehamster
    Avatar
    Community Member
    3 Posts

    Re: Custom contact form sending empty email Link to this post

    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.

    4160 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.