Jump to:

23370 Posts in 18160 Topics by 2864 members

General Questions

SilverStripe Forums » General Questions » Change Silverstripe Mailer to use smtp with authentication with the pear package

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: 3050 Views
  • WalterW
    Avatar
    Community Member
    18 Posts

    Change Silverstripe Mailer to use smtp with authentication with the pear package Link to this post

    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

  • Cem
    Avatar
    Community Member
    31 Posts

    Re: Change Silverstripe Mailer to use smtp with authentication with the pear package Link to this post

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

  • WalterW
    Avatar
    Community Member
    18 Posts

    Re: Change Silverstripe Mailer to use smtp with authentication with the pear package Link to this post

    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)

    Attached Files
  • Cem
    Avatar
    Community Member
    31 Posts

    Re: Change Silverstripe Mailer to use smtp with authentication with the pear package Link to this post

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

  • Mo
    Avatar
    Community Member
    505 Posts

    Re: Change Silverstripe Mailer to use smtp with authentication with the pear package Link to this post

    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?

  • Mauro74
    Avatar
    Community Member
    30 Posts

    Re: Change Silverstripe Mailer to use smtp with authentication with the pear package Link to this post

    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.

  • xeraa
    Avatar
    Google Summer of Code Hacker
    58 Posts

    Re: Change Silverstripe Mailer to use smtp with authentication with the pear package Link to this post

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

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