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.

General Questions /

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

Email doesnt send when I pass data in?


Reply


5 Posts   1429 Views

Avatar
fordy

Community Member, 46 Posts

29 November 2009 at 10:28pm

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>

Avatar
Double-A-Ron

Community Member, 604 Posts

1 December 2009 at 12:15am

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

Avatar
fordy

Community Member, 46 Posts

1 December 2009 at 9:34am

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.

Avatar
Double-A-Ron

Community Member, 604 Posts

1 December 2009 at 10:07am

Edited: 01/12/2009 10:09am

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

Avatar
biapar

Forum Moderator, 435 Posts

29 January 2010 at 1:58am

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?