Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » How to use PHP Mailer in SS

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

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

Page: 1
Go to End
Author Topic: 20330 Views
  • nomen
    Avatar
    Community Member
    52 Posts

    How to use PHP Mailer in SS Link to this post

    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.

  • JamesinCO
    Avatar
    Community Member
    14 Posts

    Re: How to use PHP Mailer in SS Link to this post

    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?

  • JamesinCO
    Avatar
    Community Member
    14 Posts

    Re: How to use PHP Mailer in SS Link to this post

    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.

  • jacobsjensen
    Avatar
    Community Member
    20 Posts

    Re: How to use PHP Mailer in SS Link to this post

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

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