Jump to:

23007 Posts in 11866 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Email doesnt send when I pass data in?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1347 Views
  • fordy
    Avatar
    Community Member
    46 Posts

    Email doesnt send when I pass data in? Link to this post

    Hello,

    I am trying to send send an email in response to a form submission. The email will send until I populate the email template with the data. Does anybody know why this might be?

    Here is my code...

    function EnquireAction($data, $form) {

       
       
        $submission = new Enquiry();
    $form->saveInto($submission);
    $submission->write();
       
       Debug::show($adminbody);
       
       $Full_name = $data['First_name']." ".$data['Surname'];
       $Wine = $data['Wine_name'].", ".$data['Vintage'].", ".$data['Region'].", ".$data['Colour'];
       
       $email = new Email(**FROM EMAIL**, **SEND TO**, "New Wine List Enquiry");
       $email->setTemplate('AdminWineEnquiry');

       $email->populateTemplate(
             array(
                'FullName' => $Full_name,
                'Email' => $data['Email'],
                'Telephone' => $data['Telephone'],
                'Wine' => $Wine,
                'Price' => $data['price'],
                'Desc' => $data['Desc']
             )
          );

       if($email->send()){
          Debug::show($email);
       }else{
          Debug::show("Not sent");
       }
       
    //Director::redirectBack(Director::baseURL(). "/thank-you");
       
    }

    The template is...

    Hi Ben,<br/><br/>
    <p>You have had a new enquiry from the wine list.</p>
    <p>From: $FullName</p>
    <p>Email: $Email</p>
    <p>Telephone: $Telephone</p>
    <p>Wine: $Wine</p>";
    <p>Price on website: &pound;$Price</p>
    <p>User comments: $Desc</p>
    <p>Regards<br/>
    The website</p>

  • Double-A-Ron
    Avatar
    Community Member
    603 Posts

    Re: Email doesnt send when I pass data in? Link to this post

    I had this exact problem a few weeks ago, but haven't had time to track down a specific bug, nor could I find on in Trac.

    My fix for this was to use the Email classes setTo() and setFrom() methods to set the headers AFTER the data is passed. http://api.silverstripe.org/default/Email.html#setFrom.

    I remember pumping the object on screen and seeing that the recipient field was blank if I didn't do that, even though it was present prior to the populateTemplate call.

    Aaron

  • fordy
    Avatar
    Community Member
    46 Posts

    Re: Email doesnt send when I pass data in? Link to this post

    Thanks for the help Double-a-ron. I had a go at that. Didnt seem to work

    I tried to sendPlain instead and this works. Will do me for the moment.

  • Double-A-Ron
    Avatar
    Community Member
    603 Posts

    Re: Email doesnt send when I pass data in? Link to this post

    Hi mate,

    I just dug out the exact code I was having the same issue with.

    // send an email to the customer
    $to = $this->OrderEmail;
    $subject = 'Order confirmation';
    $e = new $emailClass($to, $adminEmail, $subject);
    $e->populateTemplate($this);
    $e->populateTemplate(
    array(
    "Order" => $this,
    "Member" => $member
    )
    );

    // At this point, for some reason the subject and recipient go AWOL from the Object - Reset them!
    if(isset($subject)) $e->setSubject($subject);
    if(isset($to))$e->setTo($to);

    $e->send();

    The code is taken from the Ecommerce module. I'm willing to bet that if you output your email object on screen when you do this send, you find that the "to" data is blank.

    Aaron

  • biapar
    Avatar
    Forum Moderator
    435 Posts

    Re: Email doesnt send when I pass data in? Link to this post

    I wrote:

    $templateData = array(
    'WelcomeMessage' => $welcomeMsg, // Accessible in template via $WelcomeMessage
    'VarFirstName' => $SignupFirst,
    'VarEmail' => $SignupEmailAddress,
    'VarPassword' => $SignupPwd,
    'VarSurname' => $SignupSurn
    );
    $email->populateTemplate($templateData);


    and my SignUp.ss template is:

          <h3>Hi, $VarFirstName $VarSurname.</h3>
    <br/>

    <p>$WelcomeMessage</p>

    <ul>
    <li><strong>Email:</strong>$VarEmail</li>
    <li><strong>Password:</strong>$VarPassword</li>

    but my template vars on signup.ss are not populated. Only $WelcomeMessage. Why?

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