how to set mail form on cmsimple-xh with PHPmailer ?

A place for general not CMSimple related discussions
Post Reply
zaidh@
Posts: 2
Joined: Sat Oct 28, 2017 4:48 pm

how to set mail form on cmsimple-xh with PHPmailer ?

Post by zaidh@ » Thu Jan 04, 2018 11:29 pm

Hello everybody, please
i have a situation where, The provider had disable the mail php function on their whole server for the security reason, and advise me to use PHPmailer like alternative to solve my problem
so, I need to know how can I make all default mailform on cmsimple on my website work using phpmailer to send mails correctly?
I need your Help

cmb
Posts: 14227
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: how to set mail form on cmsimple-xh with PHPmailer ?

Post by cmb » Fri Jan 05, 2018 1:23 pm

zaidh@ wrote:so, I need to know how can I make all default mailform on cmsimple on my website work using phpmailer to send mails correctly?
I have written a drop-in replacement for cmsimple/classes/Mail.php which uses PHPMailer 5.2 instead of the plain mail() function. To use it, replace the contents of cmsimple/classes/Mail.php with the following:

Code: Select all

<?php

/**
 * Emails.
 *
 * @category  CMSimple_XH
 * @package   XH
 * @author    Peter Harteg <peter@harteg.dk>
 * @author    The CMSimple_XH developers <devs@cmsimple-xh.org>
 * @copyright 1999-2009 Peter Harteg
 * @copyright 2009-2017 The CMSimple_XH developers <http://cmsimple-xh.org/?The_Team>
 * @license   http://www.gnu.org/licenses/gpl-3.0.en.html GNU GPLv3
 * @link      http://cmsimple-xh.org/
 */

namespace XH;

require_once __DIR__ . '/PHPMailerAutoload.php';

/**
 * Emails.
 *
 * @category CMSimple_XH
 * @package  XH
 * @author   The CMSimple_XH developers <devs@cmsimple-xh.org>
 * @license  http://www.gnu.org/licenses/gpl-3.0.en.html GNU GPLv3
 * @link     http://cmsimple-xh.org/
 * @since    1.7
 */
class Mail
{
    /**
     * @var \PHPMailer
     */
    private $mailer;

    /**
     * Initializes a new instance.
     *
     * @global array The configuration of the core.
     */
    public function __construct()
    {
        global $cf;

        $this->mailer = new \PHPMailer;
        $this->mailer->CharSet = 'UTF-8';
    }

    /**
     * Returns whether an email address is valid.
     *
     * For simplicity we are not aiming for full compliance with RFC 5322.
     * The local-part must be a dot-atom-text. The domain is checked with
     * gethostbyname() after applying idn_to_ascii(), if the latter is available.
     *
     * @param string $address An email address.
     *
     * @return bool
     */
    public function isValidAddress($address)
    {
        $atext = '[!#-\'*+\-\/-9=?A-Z^-~]';
        $dotAtomText = $atext . '(?:' . $atext . '|\.)*';
        $pattern = '/^(' . $dotAtomText . ')@([^@]+)$/u';
        if (!preg_match($pattern, $address, $matches)) {
            return false;
        }
        $domain = $matches[2];
        if (function_exists('idn_to_ascii')) {
            $domain = defined('INTL_IDNA_VARIANT_UTS46')
                ? idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46)
                : idn_to_ascii($domain);
        }
        if ($domain
            && (strlen($domain) > 255 || gethostbyname($domain) == $domain)
        ) {
            return false;
        }
        return true;
    }

    /**
     * Sets the To address.
     *
     * @param string $to A valid email address.
     *
     * @return void
     */
    public function setTo($to)
    {
        $this->mailer->addAddress($to);
    }

    /**
     * Returns the encoded subject.
     *
     * @return string
     */
    public function getSubject()
    {
        return $this->mailer->Subject;
    }

    /**
     * Sets the Subject.
     *
     * @param string $subject A subject.
     *
     * @return void
     */
    public function setSubject($subject)
    {
        $this->mailer->Subject = $subject;
    }

    /**
     * Sets the message.
     *
     * @param string $message A message.
     *
     * @return void
     */
    public function setMessage($message)
    {
        $this->mailer->Body = $message;
    }

    /**
     * Adds a header field.
     *
     * @param string $name  A header field name.
     * @param string $value A header field value.
     *
     * @return void
     */
    public function addHeader($name, $value)
    {
        if ($name === 'From') {
            $this->mailer->setFrom($value);
        } else {
            $this->mailer->addCustomHeader($name, $value);
        }
    }

    /**
     * Sends the email and return whether that succeeded.
     *
     * @return bool
     */
    public function send()
    {
        return $this->mailer->send();
    }
}
Then download the latest PHPMailer 5.2, that is currently 5.2.26. Extract the PHPMailer archive, and copy the five files starting with "class." and PHPMailerAutoload.php to cmsimple/classes/.

Basically, this should already be sufficient, but most likely you will have configure PHPMailer with appropriate SMTP settings. You can do this in function __construct() in cmsimple/classes/Mail.php, similar to what is shown in the Simple Example.
Christoph M. Becker – Plugins for CMSimple_XH

Post Reply