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.

Archive /

Our old forums are still available as a read-only archive.

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

Having trouble using the Email class


Go to End


5 Posts   3589 Views

Avatar
spullen

14 Posts

1 August 2007 at 5:03am

Hello, I am trying to create a custom form and send the information in an email using the email class.

$body = "<p>You've received a contact us submission from the website</p>";
		$body .= "<p>Name: {$data['Name']}<br />";
		$body .= "Title: {$data['Title']}<br />";
		$body .= "Company: {$data['Company']}<br />";
		$body .= "Phone: {$data['Phones']}<br />";
		$body .= "Fax: {$data['Fax']}<br />";
		$body .= "Email: {$data['Email']}<br />";
		$body .= "Phone: {$data['Phone']}<br />";
		$body .= "Comments: {$data['Comments']}<br />";
		$body .= "Product 1: {$products[$data['Product1']]}<br />";
		$body .= "Product 2: {$products[$data['Product2']]}<br /><br />";
		
		
		//print 'email: '.Email::getAdminEmail().'<br><br>';
		$email = new Email('no-reply@marketingmessages.com', Email::getAdminEmail(), 'Website Contact Submission', $body);
		
		$email->sendPlain();

That is the code I have and when I run the page I get the following errors.
FATAL ERROR: None of these templates can be found: GenericEmail.ss
At line 53 in C:\wamp\www\marketing_messages\sapphire\core\SSViewer.php

user_error(None of these templates can be found: GenericEmail.ss,512)
user_error at line 53 of SSViewer.php

SSViewer->__construct(GenericEmail)
__construct at line 148 of Email.php

Email->parseVariables()
parseVariables at line 101 of Email.php

Email->debug()
debug at line 117 of ContactUs.php

I don't know if I am using the email class correctly. Thank you in advance for any help.

Avatar
elijahlofgren

Google Summer of Code Hacker, 222 Posts

1 August 2007 at 6:49am

Hi spullen,

I ran into that same problem when trying to fix this bug: Errors when Importing Recipients to Newsletter Mailing List

I added the attached GenericEmail.ss file to sapphire/templates/email/ in order to fix that problem.

I appleid the fix on the gsoc branch so future versions of SilverStripe it should not have this problem:
------------------------------------------------------------------------
r38503 | elofgren | 2007-07-11 23:20:12 -0500 (Wed, 11 Jul 2007) | 1 line

BUGFIX: Add missing 'GenericEmail.ss' template which is used in RecipientImportField::notifyChanges() as part of fix for bug 'Errors when Importing Recipients to Newsletter Mailing List' Reported here: http://www.silverstripe.com/bugs/flat/1470
------------------------------------------------------------------------

You may want to add GenericEmail.ss mysite/templates/email so that if you make any changes, they will not be overwritten when you upgrade.

Hope this helps,

Elijah Lofgren

Avatar
spullen

14 Posts

1 August 2007 at 6:58am

Thank you for your reply. My question now is am I using the email class correctly to send the email? I generated the body and did a print on it and that was fine. Now am I called the new Email correctly? and did I use $email->sendPlain() correctly to send a plain text email? Thank you for the GenericEmail.ss download I am sure this had something to do with it, I will try it now.

Avatar
elijahlofgren

Google Summer of Code Hacker, 222 Posts

1 August 2007 at 8:01am

Hi spullen,

> Thank you for your reply. My question now is am I using the email class correctly to send the email? I generated the body and did a print on it and that was fine. Now am I called the new Email correctly? and did I use $email->sendPlain() correctly to send a plain text email? Thank you for the GenericEmail.ss download I am sure this had something to do with it, I will try it now.

You're welcome. I just tested your code and it works fine here. You'll want to delete the unneeded HTML code from GenericEmail.ss since you are not sending an HTML email.

Have a great day,

Elijah

Avatar
spullen

14 Posts

1 August 2007 at 9:00am

I figured out what our problem was here on our end. We are in a shared hosting environment which means that the sendmail would not work correctly. No mail would get sent out by PHP. So, if you want to set SilverStripe up in a shared hosting environment I would recommend using the PHP Mailer Class. This can be found here:

http://phpmailer.sourceforge.net/

This is a class that uses SMTP to send the email instead of the sendmail.

In order to get this working all I did was pull down the zip for the library, unzip it and stick it in the mysite/code folder. Then I used the following code on the page I wanted to send the email on, which in my case was mysite/code/ContactUs.php(altered slightly of course) which can be found on the front page of the website above to send the email.

require("phpmailer/class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();                                   // send via SMTP
$mail->Host     = "smtp1.site.com;smtp2.site.com"; // SMTP servers
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "jswan";  // SMTP username
$mail->Password = "secret"; // SMTP password

$mail->From     = "from@email.com";
$mail->FromName = "Mailer";
$mail->AddAddress("josh@site.com","Josh Adams"); 
$mail->AddAddress("ellen@site.com");               // optional name
$mail->AddReplyTo("info@site.com","Information");

$mail->WordWrap = 50;                              // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz");      // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); 
$mail->IsHTML(true);                               // send as HTML

$mail->Subject  =  "Here is the subject";
$mail->Body     =  "This is the <b>HTML body</b>";
$mail->AltBody  =  "This is the text-only body";

if(!$mail->Send())
{
   echo "Message was not sent <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";

This was the only work around I could find to manually send an email in a shared hosting environment with SilverStripe.