Problem with remote SMTP -- and a fix!

Discuss, research, share, find, and solve bugs

Problem with remote SMTP -- and a fix!

Postby simlan » 2:58pm, Mon 16 Feb, 2009

Hello!

I figured out a problem with using PHPMailer in combination with external SMTP servers (that is if you don't want to use the build-in PHP mail() function). This problem has already been reported but it's still not fixed.

See this Mantis entry: http://mantis.phplist.com/view.php?id=8590

The problem is located in "admin/class.phplistmailer.php", which derives from PHPMailer but show inappropriate usage of the parent.

The first problem is related to setting the language for PHPMailer, which results in stupid errors to be produced, like "Error: Language string failed to load".

Code: Select all
parent::SetLanguage('en', dirname(__FILE__) . 'phpmailer/language/');


This is missing a separator "/", and the correct line looks like:

Code: Select all
parent::SetLanguage('en', dirname(__FILE__) . '/phpmailer/language/');


Note that problems can also arise if you set PHPMailer to use your dedicated SMTP server but your Webhosting Service does not allow invocation of the fsockopen() PHP function. You can test this with a small script I wrote (store it outside of PHPList because .htaccess forbids execution). If your host does not allow fsockopen(), you'll get "Connection refused" or similar. PHPMailer depends on this function to work!

Code: Select all
$host = "your.mailserver.tld";
$port = 25;
$tval = 15;

$smtp_conn = fsockopen($host, $port, $errno, $errstr, $tval);

if (empty($smtp_conn))
{
  print("Error connecting: ". $errno .": ". $errstr);
} else {
  print("Connect seems good!\n");
  print_r($smtp_conn);
  fclose($smtp_conn);
}


Other issues are related to inappropriate usage of $this->Host, $ip and wrong usage of "if", especially if the host is already in IP format. I propose the following code for the constructor of the class, which is not well tested but works for me and seems to be more sensible.

Code: Select all
    function PHPlistMailer($messageid,$email) {
    #  parent::PHPMailer();
      parent::SetLanguage('en', dirname(__FILE__) . '/phpmailer/language/');
      $this->addCustomHeader("X-Mailer: phplist v".VERSION);
      $this->addCustomHeader("X-MessageID: $messageid");
      $this->addCustomHeader("X-ListMember: $email");
      $this->addCustomHeader("Precedence: bulk");
      $this->CharSet = getConfig("html_charset");

      /* Check if there is a global SMTP host defined. */
      if (defined('PHPMAILERHOST') && PHPMAILERHOST != ''
          && isset($GLOBALS['phpmailer_smtpuser'])
          && $GLOBALS['phpmailer_smtpuser'] != '')
      {
        //logEvent('Sending authenticated email via '.PHPMAILERHOST);
        //$this->addCustomHeader("X-Mailer-Method: Dedicated SMTP");
        $this->SMTPAuth = true;
        $this->Helo = getConfig("website");
        $this->Host = PHPMAILERHOST;
        $this->Username = $GLOBALS['phpmailer_smtpuser'];
        $this->Password = $GLOBALS['phpmailer_smtppassword'];
        $this->Mailer = "smtp";
      }
      else
      {
        //logEvent('Sending email using PHP mail()');
        $this->Mailer = "mail";
      }

      if ($GLOBALS["message_envelope"]) {
        $this->Sender = $GLOBALS["message_envelope"];
        $this->addCustomHeader("Errors-To: ".$GLOBALS["message_envelope"]);
      }
    }


Dear PHPList guys, please fix this issue.

Regards,
Simon

[EDITED by moderator to include link to Mantis report]
simlan
phpList newbie
 
Posts: 1
Joined: 2:08pm, Mon 16 Feb, 2009

Postby H2B2 » 5:16pm, Sat 28 Feb, 2009

Thanks! Have included your suggestions in http://mantis.phplist.com/view.php?id=8590
H2B2
Moderator
 
Posts: 7188
Joined: 1:51am, Wed 15 Mar, 2006

Re: Problem with remote SMTP -- and a fix!

Postby cipixul » 9:20am, Tue 01 Sep, 2009

Hey!

The if statement is wrong by a mile and I see our dear developers included the code without even looking at it.

I just spent quite a lot of time to figure that phplist 2.10.10 is faulty, please next time look at the code and you won't waste the time of other people :)

The code is wrong because it uses PHPMAILERHOST only if the admin set phpmailer_user, which is not always the case, and as we run several smtpd servers on same machine, we don't authenticate to our smtpd instances because they're local.

The correct code which works in our case and in all other cases I can think of should be this one:

Code: Select all
      if (defined('PHPMAILERHOST') && PHPMAILERHOST != '')
      {
        //logEvent('Sending authenticated email via '.PHPMAILERHOST);
        //$this->addCustomHeader("X-Mailer-Method: Dedicated SMTP");
        $this->SMTPAuth = true;
        $this->Helo = getConfig("website");
        $this->Host = PHPMAILERHOST;
        if ( isset($GLOBALS['phpmailer_smtpuser']) && $GLOBALS['phpmailer_smtpuser'] != ''
                && isset($GLOBALS['phpmailer_smtppassword']) && $GLOBALS['phpmailer_smtppassword']
        )
        {
                $this->Username = $GLOBALS['phpmailer_smtpuser'];
                $this->Password = $GLOBALS['phpmailer_smtppassword'];
        }
        $this->Mailer = "smtp";
      }

Cheers and keep up the good work!

[Moderator edit] Related mantis bug tracker report: http://mantis.phplist.com/view.php?id=15325
cipixul
phpList newbie
 
Posts: 1
Joined: 9:06am, Tue 01 Sep, 2009

Re: Problem with remote SMTP -- and a fix!

Postby GazOutEast » 5:04am, Fri 09 Apr, 2010

The code is wrong because it uses PHPMAILERHOST only if the admin set phpmailer_user, which is not always the case


when you say " phpmailer_user " - do you mean the config.php setting for " $phpmailer_smtpuser " ?

Or is there another user setting that I have missed entirely, and which may be the root cause of my non-functioning installs?

Thanks
Gaz
If it's fixed, I can break it
GazOutEast
PL Geek
 
Posts: 61
Joined: 4:47am, Thu 08 Apr, 2010

Re: Problem with remote SMTP -- and a fix!

Postby epheterson » 2:17am, Sat 26 Jun, 2010

cipixul, thanks for your code! I put this in /lists/admin/class.phplistmailer.php and my problem was cleared right up. I was sure my SMTP server had gone sour.

I was banging my head against the wall, you're my relief! Thanks!


-Eric
epheterson
phpList newbie
 
Posts: 3
Joined: 2:44pm, Mon 26 Oct, 2009

Re: Problem with remote SMTP -- and a fix!

Postby mrmckoy » 6:13pm, Sat 20 Nov, 2010

Where does this code go? What does it replace? Do i just add it anywhere in class.phplistmailer.php?

cipixul wrote:Hey!

The if statement is wrong by a mile and I see our dear developers included the code without even looking at it.

I just spent quite a lot of time to figure that phplist 2.10.10 is faulty, please next time look at the code and you won't waste the time of other people :)

The code is wrong because it uses PHPMAILERHOST only if the admin set phpmailer_user, which is not always the case, and as we run several smtpd servers on same machine, we don't authenticate to our smtpd instances because they're local.

The correct code which works in our case and in all other cases I can think of should be this one:

Code: Select all
      if (defined('PHPMAILERHOST') && PHPMAILERHOST != '')


      {
        //logEvent('Sending authenticated email via '.PHPMAILERHOST);
        //$this->addCustomHeader("X-Mailer-Method: Dedicated SMTP");
        $this->SMTPAuth = true;
        $this->Helo = getConfig("website");
        $this->Host = PHPMAILERHOST;
        if ( isset($GLOBALS['phpmailer_smtpuser']) && $GLOBALS['phpmailer_smtpuser'] != ''
                && isset($GLOBALS['phpmailer_smtppassword']) && $GLOBALS['phpmailer_smtppassword']
        )
        {
                $this->Username = $GLOBALS['phpmailer_smtpuser'];
                $this->Password = $GLOBALS['phpmailer_smtppassword'];
        }
        $this->Mailer = "smtp";
      }


Cheers and keep up the good work!
mrmckoy
PL Master
 
Posts: 169
Joined: 7:10am, Sat 24 Nov, 2007
Location: Atlanta, GA

Re: Problem with remote SMTP -- and a fix!

Postby Dragonrider » 8:14pm, Sat 20 Nov, 2010

mrmckoy wrote:Where does this code go? What does it replace? Do i just add it anywhere in class.phplistmailer.php?


Replace part of the existing [ function PHPlistMailer($messageid,$email) ] in class.phplistmailer.php, it's quite early on in the file.

Do a search for
Code: Select all
if (defined('PHPMAILERHOST') && PHPMAILERHOST != '')

and replace from that to
Code: Select all
$this->Mailer = "smtp";
 }


inclusive with the above block of code.
My sites:- http://wharfedalefestival.co.uk, http://ilkleygardeners.org.uk, http://emergencyaid.net, http://dragonrider.co.uk
Latest phpList version is now 3.0.12 (3 February 2015) and requires a minimum of MySQL 5.0 and PHP 5.3.x
Dragonrider
Moderator
 
Posts: 3460
Joined: 6:58am, Sun 02 Jul, 2006
Location: Ilkley, West Yorkshire, United Kingdom


Return to Bug Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron