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.

General Questions /

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

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

Change Silverstripe Mailer to use smtp with authentication with the pear package


Go to End


7 Posts   7831 Views

Avatar
WalterW

Community Member, 18 Posts

22 July 2009 at 1:51am

Edited: 23/02/2010 2:39am

I tried a lot to use silverstripe with a smtp server with authentication.

First I followed the proposals given in the forum "How to use PhpMailer with Silverstripe".
This was working, but: The PhpMailer class does not generate correct mails for use with outlook clients, especially with html formatted emails. As I did not see any solution for phpmailer, I found the Mail package in the Pear packages ( http://pear.php.net/package/Mail )

After making some simple changes to "Mailer.php" in Saphhire/Email mails are generated now correctly also for outlook clients.

The change for Mailer.php was:
Insert at the end of Mailer.php

/**
 * Send a email via Pear mail package
 */
function sendPearMail($to, $subject, $body, $headers /* , "-f$bounceAddress") */ ) {
	require_once "PEAR.php";
	require_once "Mail.php";

	$headers["Subject"] = $subject;
	$headers["To"] = $to;

	$from = $headers["From"] ;
	$host = "smtp.myserver.de";
	$username = "myusername";
	$password = "mypassword";

	$smtp = Mail::factory('smtp',
	  array ('host' => $host,
		'auth' => true,
		'username' => $username,
		'password' => $password));

	$mail = $smtp->send($to, $headers, $body);
	if (PEAR::isError($mail)) {
	  // echo("<p>" . $mail->getMessage() . "</p>");
	  return FALSE;
	 } else {
	  // echo("<p>Message successfully sent!</p>");
	  return TRUE;
	 }
}

and change all lines where

 mail(...) 
is called
if(!($result = @mail($to, $subject, $fullBody, $headers, "-f$bounceAddress"))) {
		$result = mail($to, $subject, $fullBody, $headers);
	}

to

$result=sendPearMail($to, $subject, $fullBody, $headers);

and last: comment out these lines:

		$headers = processHeaders($headers);

That should do it!

Greetings,

Walter

Avatar
Cem

Community Member, 31 Posts

22 February 2010 at 8:14am

Hi Walter,
Are you putting the package( http://pear.php.net/package/Mail ) to the root of SS first?
And then applying the changes to Mailer.php?
It would also be nice if you could attach your Mailer.php to this post as to prevent mistakes.
Sorry for being a noob...

Avatar
WalterW

Community Member, 18 Posts

23 February 2010 at 1:14am

My provider installed the pear package for us, so I had not to change the Silverstripe source directory.
The corresponding output of phpinfo is:
Configure Command

'./configure' '--prefix=/usr/' '--datadir=/usr/share/php/' '--bindir=/usr/bin/' '--libdir=/usr/share/' '--with-exec-dir=/usr/lib/php/bin/' '--with-config-file-path=/etc' '--with-config-file-scan-dir=/etc/php5-config' '--enable-discard-path' '--enable-force-cgi-redirect' '--enable-memory-limit' '--enable-sigchild' '--enable-track-vars' '--enable-trans-sid' '--disable-mysql' '--disable-pdo' '--disable-dom' '--enable-bcmath' '--enable-calendar' '--enable-ctype' '--enable-dbase' '--enable-exif' '--enable-filepro' '--enable-ftp' '--enable-magic-quotes' '--enable-mbstr-enc-trans' '--enable-mbstring' '--enable-mbregex' '--enable-shmop' '--enable-sysvsem' '--enable-sysvshm' '--enable-wddx' '--with-gettext' '--with-mcrypt' '--with-iconv' '--with-mcal' '--with-mcrypt' '--with-zlib' '--with-bz2' '--with-gmp' '--with-openssl=/usr' '--with-pear' '--enable-zip' '--with-recode' '--enable-suhosin' '--with-apxs2=/usr/sbin/apxs'

I append the changed Mailer.php (now it's version 2.3.6)

Avatar
Cem

Community Member, 31 Posts

24 February 2010 at 2:15am

I will give it a try and let you know.
Thank you for the attachement.
Cheers

Avatar
Mo

Community Member, 541 Posts

24 February 2010 at 3:13am

I really wish there was a way to do this without hacking the core files. Maybe if there was a sendMail() method that follows the current behaviour, but can be easily overwritten by using Object::add_extension()?

Any thoughts?

Avatar
Mauro74

Community Member, 30 Posts

22 September 2011 at 12:00am

Hi Walter, how do I do to integrate/install the pear package in SS, my hosting provider doesn't want install it for me (!)
Thanks in advance. :)

Avatar
xeraa

Community Member, 58 Posts

22 September 2011 at 10:02pm

Don't mess around with core - there's a simple module for this feature: https://github.com/xeraa/silverstripe-smtp