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

How to use PHP Mailer in SS


Go to End


4 Posts   21169 Views

Avatar
nomen

Community Member, 52 Posts

12 March 2008 at 11:37pm

Edited: 03/06/2008 8:33pm

I was having some problems to work mail function in a shared hosting.
See my message
So I decided to use PHP Mailer in SS, and here are the steps to do it.

1) Download PHP Mailer
I downloaded phpmailer for php5

2) Uncompress it to your SS project root.
Rename the uncompressed directory, something like PHPMailer_v2.X.Y, to phpmailer

3) Open your _config.php file
Add this code in the file:


/* { PHPMAILER */
$path = Director::baseFolder().'/phpmailer/';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once 'class.phpmailer.php';
global $phpmailer;
$phpmailer = new PHPMailer();
$phpmailer->CharSet = "UTF-8";
$phpmailer->IsSMTP(); // send via SMTP
$phpmailer->Host = "smtp.example.com"; // SMTP servers
$phpmailer->SMTPAuth = true; // turn on SMTP authentication
$phpmailer->Username = "username"; // SMTP username
$phpmailer->Password = "password"; // SMTP password
$phpmailer->From = "username@example.com";
$phpmailer->FromName = "My From Text";
/* PHPMAILER } */

This is the basical configuration of PHP Mailer.

4) Open sapphire/core/Email.php
Go to send function and search this line:

$result = htmlEmail($to, $this->from, $subject, $this->body, $this->attachments, $this->plaintext_body, $headers);

replace the line with this lines:

global $phpmailer;
if ($phpmailer)
$result = htmlEmail_phpMailer($to, $this->from, $subject, $this->body, $this->attachments, $this->plaintext_body, $headers);
else
$result = htmlEmail($to, $this->from, $subject, $this->body, $this->attachments, $this->plaintext_body, $headers);


5) Add this new function to Email class:

function htmlEmail_phpMailer($to, $from, $subject, $htmlContent) {

global $phpmailer;

$to = validEmailAddr($to);
$phpmailer->ClearAddresses();
$phpmailer->AddAddress($to);
$phpmailer->IsHTML(true); // send as HTML
$phpmailer->Subject = $subject;
$phpmailer->Body = $htmlContent;
if(!$plainContent)
$plainContent = Convert::xml2raw($htmlContent);
$phpmailer->AltBody = $plainContent;

if(!$phpmailer->Send())
return false;
else
return true;
}

Now SS will send mails using PHP Mailer.
I only test it with forms and it works good. I test it on SS 2.2.1
Maybe it has some errors (any help to solve them is welcomed), but for me it works great.

Hope this helps someone.

Avatar
JamesinCO

Community Member, 14 Posts

21 September 2008 at 3:43am

Edited: 21/09/2008 3:46am

OK ... I tried this and get this error.

Parse error: syntax error, unexpected T_CLASS, expecting T_FUNCTION in /home/dsamwm/public_html/sapphire/core/Email.php on line 412

I presume that I modified the Email.php file incorrectly as I did not fully understand "Step 5" of the fix.

Where exactly do you add that code in the Email.php file to get it to work? If someone could please tell me what line do I need to find and insert the step 5 code after to make this work it would be great.

Also .... when you say the 'project root' in setp 2 ... do you mean www or the 'mysite' subdirectory?

Avatar
JamesinCO

Community Member, 14 Posts

21 September 2008 at 4:09am

Edited: 21/09/2008 10:21am

OK ... I am way over my head ... but did get this to work.

I inserted the Step 5 Code in Email.php right after :

// TO DO: Clean this code up, make it more OO.
// For now, we've just put a clean interface around this dirty code :)

I chose that position becasue it was right before the function htmlEmail which is the "else" part of the step 4 code mod. It may not be optimal, but it is working.

I also placed the folder for PhPMailer as phpmailer in both the www and the mysite directories ... again not efficient to have this in both places, but it is working now.

To be honest, for a product that is advertised as "When developing a SilverStripe website you'll be staying up all night because you're having fun, not because you're stuck or moving at a slow pace. " I am seriously disappointed by the large amount of custom modification I have had to make to get SS features to actually function on my server.

This leaves me VERY concerned about upgrades when they come as that will probably necessitate re-working these custom mods.

All I haveto say is THANK GOD these forums are here ... as I've spent hours researching, reading, and implementing fixes documented here to this "easy" and "fun" product.

Avatar
jacobsjensen

Community Member, 20 Posts

19 November 2008 at 6:55am

Hi guys,

I just did this to get my forms to send mails, and it worked lige a charm!
SS v. 2.2.3
Both forms and newsletters ship of nicely!!

THANKS!!!