Pages: [1] 2   Go Down
Send this topic | Print
Author Topic: TIP: How send mails by GMail account (Patch for PHPMailer)  (Read 18892 times)
hopkins
Member
*
Offline Offline

Posts: 43


« on: September 08, 2006, 01:45:57 AM »

How promised, there is patch for PHPMailer.
I added one variable "SMTPSecurity" to PHPMailer class.
So if you are using PHPMailer and want use GMail account for sending, just set "SMTPSecurity" to "tls" or "ssl".

Example of using PHPMailer with this patch for GMail account:
Code:
<?php
  
/* Some code of your script */

  // Include PHPMailer class
  
require_once('PATH_TO_PHPMAILER/class.phpmailer.php');


  
// Start working with the class
  
$Mail = new PHPMailer();

  
// You should specify the DIR where PHPMailer is saved
  
$Mail->PluginDir = 'PATH_TO_PHPMAILER/';

  
// Optional: You may specify language (EN is default)
  
$Mail->SetLanguage("cz", 'PATH_TO_PHPMAILER/language/');

  
// We will use SMTP
  
$Mail->Mailer = "smtp";
  
// Specify SMTP server
  
$Mail->Host = "smtp.gmail.com";

  
/* There is the new future - the "SMTPSecurity" variable */
  
$Mail->SMTPSecurity   = "tls";  // I prefere this (ESMTP goes over TLS)
//  $Mail->SMTPSecurity   = "ssl";  // But this work too


  // Specify port
  
$Mail->Port = 465;

  
// We have to Authenticate
  
$Mail->SMTPAuth = true;
  
$Mail->Username = "USERNAME@gmail.com";
  
$Mail->Password = "PASSWORD";
  
  
// Optional: Specify character coding and encoding
  
$Mail->CharSet = "utf-8";
  
$Mail->Encoding = "quoted-printable";

  
// Some mail info
  
$Mail->FromName = "FROM_USER_NAME";
  
$Mail->AddReplyTo("REPLY_TO_ADDRESS");
  
$Mail->Subject = "Subject";
  
$Mail->Body = "Body of plain/text mail";

  
$Mail->AddAddress("TO_ADDRESS");

  
// And finaly - send the mail
  
if($Mail->Send())
echo "OK";
  else
echo "ERROR";


All list of avalible variables and methods is in PHPMailer documentation.
(Except the "SMTPSecurity" variable wink )

I hope it will help someone.

PS: If you are using your own error reporting handler, you may get SSL warning if communication closing (PHP developers says that it's bug in some servers (http://bugs.php.net/bug.php?id=23220)) but mail will be send.
« Last Edit: November 12, 2006, 12:06:18 AM by hopkins » Logged
hopkins
Member
*
Offline Offline

Posts: 43


« Reply #1 on: September 08, 2006, 02:07:04 AM »

If you are using some other mailer,
you should be able solve the GMail TLS requirement by specifying "tls://smtp.gmail.com" or "ssl://smtp.gmail.com" as host (SMTP server).
Logged
DP-NiSh
Member
*
Offline Offline

Posts: 17


« Reply #2 on: September 08, 2006, 02:56:50 AM »

Is this for phpBB too ? I mean it wud be nice if I cud use GMail smtp for my forums
Logged
hopkins
Member
*
Offline Offline

Posts: 43


« Reply #3 on: September 08, 2006, 04:07:38 AM »

I dont using it, but I download phpBB2 for you and look to source.

There is $board_config['smtp_host'] in smtp.php so probably you can specify "smtp_host" in config (or during instalation proces) - set it to "tls://smtp.gmail.com".

But I saw that there is fixed port (25).
You will have to change it to 465 (For GMail SMTP).

In the version I download (phpBB-2.0.21) it's folowing block from "includes/smtp.php" (Line: 108):
Code:
// Ok we have error checked as much as we can to this point let's get on
// it already.
if( !$socket = @fsockopen($board_config['smtp_host'], 25, $errno, $errstr, 20) )
{
message_die(GENERAL_ERROR, "Could not connect to smtp host : $errno : $errstr", "", __LINE__, __FILE__);
}

In !$socket = @fsockopen($board_config['smtp_host'], 25, $errno, $errstr, 20) ) change 25 to 465.

You should also find all "fgets" functions and modify them to "@fgets" (In my case there was only one).

Than it shoul be fine (I don't see any reson why not, but I cant test it).
Logged
DP-NiSh
Member
*
Offline Offline

Posts: 17


« Reply #4 on: September 08, 2006, 06:04:25 AM »

Oh thanks, I'll try it out Smiley  (Darn I cud have figured it out myself, lazy me  rolleyes )
Logged
kjaonline
Hyper-Active Member
***
Offline Offline

Posts: 253


« Reply #5 on: September 08, 2006, 11:12:56 AM »

Does anyone here know hoe to do it on e107?
e107.org
please help me..
I don't know a thing about php..
only html...
Will self study php this coming sembreak..
I still have class and am a Nursing student..
Thanks ^_^
Logged

.::xkiller213::.
Hyper-Active Member
***
Offline Offline

Posts: 325


Viva La 110mb~!


WWW
« Reply #6 on: September 08, 2006, 01:39:27 PM »

Nice... Now I'll try it on Xoops 2.0.15... LOL!

Code:

   /////////////////////////////////////////////////

    // SMTP VARIABLES

    /////////////////////////////////////////////////



    /**

     *  Sets the SMTP hosts.  All hosts must be separated by a

     *  semicolon.  You can also specify a different port

     *  for each host by using this format: [hostname:port]

     *  (e.g. "smtp1.example.com:25;smtp2.example.com").

     *  Hosts will be tried in order.

     *  @var string

     */

    var $Host        = "smtp.gmail.com";



    /**
     * There is the new future - the "SMTPSecurity" variable
     */
    $Mail->SMTPSecurity   = "tls";
 // I prefere this (ESMTP goes over TLS)
 // $Mail->SMTPSecurity   = "ssl";
 // But this work too

    /**

     *  Sets the default SMTP server port.

     *  @var int

     */

    var $Port        = 465;



    /**

     *  Sets the SMTP HELO of the message (Default is $Hostname).

     *  @var string

     */
    var $Helo        = "";



    /**

     *  Sets SMTP authentication. Utilizes the Username and Password variables.

     *  @var bool

     */

    var $SMTPAuth     = true;


    /**

     *  Sets SMTP username.

     *  @var string

     */

    var $Username     = "xkiller213@gmail.com";



    /**

     *  Sets SMTP password.

     *  @var string

     */

    var $Password     = "********";



    /**

     *  Sets the SMTP server timeout in seconds. This function will not

     *  work with the win32 version.

     *  @var int

     */

    var $Timeout      = 10;



    /**

     *  Sets SMTP class debugging on or off.

     *  @var bool

     */

    var $SMTPDebug    = false;



    /**

     * Prevents the SMTP connection from being closed after each mail

     * sending.  If this is set to true then to close the connection

     * requires an explicit call to SmtpClose().

     * @var bool

     */

    var $SMTPKeepAlive = false;



Have what I've edited in class.phpmailer.php correct?

I can't find the
Code:
// And finaly - send the mail
  if($Mail->Send())
   echo "OK";
  else
   echo "ERROR";


It is ok?

Anyway here's the  file:
http://eschorale.110mb.com/help/

There are 2 files in there... Do I need to edit the class.smtp.php one?

Plz advice

Rgds
Gab
Logged

hopkins
Member
*
Offline Offline

Posts: 43


« Reply #7 on: September 08, 2006, 06:52:10 PM »

Oh man, you don't uderstand me.
Overwrite the "class.phpmailer.php" and "class.smtp.php" by the 2 files from ZIP (1st post).

The code what was as Example was example of using (working with) PHPMailer - it is (should be) in separate file - but it's for those who want to write their own scripts.

If you are using some system - you are using "Xoops 2.0.15" (But sorry, I dont know and I'm not using it) - don't add any "code" to class.phpmailer.php, only set (modify - how you did) the variables in class.phpmailer.php, if there isn't any system config file with those variables (If is - in time of calling PHPMailer, the system overwrite youre default setings in class.phpmailer.php - so you will have to set them in the system config).
The code:
Code:
   /**
     * There is the new future - the "SMTPSecurity" variable
     */
    $Mail->SMTPSecurity   = "tls";
 // I prefere this (ESMTP goes over TLS)
 // $Mail->SMTPSecurity   = "ssl";
 // But this work too

shouldn't go to class.phpmailer.php.

But in my (from ZIP) file class.phpmailer.php you can find new variable "$SMTPSecurity" set it to "tls" or "ssl" if you wish to use GMail SMTP server and dont't want to search and modify the mailing functions in your system (than you will use something like $Mail->SMTPSecurity   = "tls"; from example).
- It's easyes way for you but not the cleanes wink  - You will have to count with this modification you did, if you will someday changing the SMTP.


PS:
1) I wrote "I added one variable "SMTPSecurity" to PHPMailer class.", but there are ofcourse modifications in class files, whitch working with this variable - so you realy have to overwrite the original files.
2) Noone can see/download the source code of your PHP file (if you don't print it to screen) - PHP file is processed by server (we see only blank page in this case).
Logged
.::xkiller213::.
Hyper-Active Member
***
Offline Offline

Posts: 325


Viva La 110mb~!


WWW
« Reply #8 on: September 08, 2006, 07:24:32 PM »

Ok Thx.. Anyway to see the files you could right-click them, choose save as, then open with wadeva u want.. Anyway thx for telling me...
Logged

hopkins
Member
*
Offline Offline

Posts: 43


« Reply #9 on: September 08, 2006, 07:56:15 PM »

Quote from: xkiller213
Anyway to see the files you could right-click them, choose save as, then open with wadeva u want..

So try it  Smiley
And you will get something like this (class.phpmailer.php):
Quote
<br />
<b>Parse error</b>:  syntax error, unexpected T_VARIABLE, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in <b>/home/vhosts/eschorale.110mb.com/public_html/help/class.phpmailer.php</b> on line <b>310</b><br />

Becose server processed the script and send you the result (which you save).

It's usefull for counting downloads (You log download and then send to browser/user content he originaly want.)
Logged
hiimdustin
Member
*
Offline Offline

Posts: 23


WWW
« Reply #10 on: September 09, 2006, 01:43:35 AM »

hey hopkins, this is a noob question... hehehe do i have to download anything for your code to work, or does 110mb already have the PHPMailer lib?

Thanks!!

 rolleyes  rolleyes  rolleyes
Logged

hopkins
Member
*
Offline Offline

Posts: 43


« Reply #11 on: September 09, 2006, 06:26:38 AM »

Because you have to overwrite the original files, you have to download your own version of PHPMailer (copy it to server and overwrite the two class files) - lik is in README file, but noone read Smiley , so there is link: http://sourceforge.net/projects/phpmailer/
Logged
Clookster
Cookie-eating freak #257
Advanced Authority Member
*****
Online Online

Posts: 1485


I <3 snowboarding & surfing


WWW
« Reply #12 on: September 09, 2006, 06:47:32 AM »

i did lol
but didn't try it yet as everything will be deleted
thanks already
Logged

(\ /)
(O.o)
(> <)
This is Bunny.
Copy Bunny into your signature
to help him on his way to
world domination

hiimdustin
Member
*
Offline Offline

Posts: 23


WWW
« Reply #13 on: September 10, 2006, 12:20:15 AM »

Quote from: hopkins
Because you have to overwrite the original files, you have to download your own version of PHPMailer (copy it to server and overwrite the two class files) - lik is in README file, but noone read Smiley , so there is link: http://sourceforge.net/projects/phpmailer/


hey, correct me if i'm wrong... but should i install PHPMailer to my root in order to use the script? is that right?
Logged

hopkins
Member
*
Offline Offline

Posts: 43


« Reply #14 on: September 10, 2006, 02:47:17 AM »

Yes copy phpmailer directory to root or some special directory where you are storing class and function files - for example: /kernel/, /libs/, ... - it doen't matter.
You will specify where phpmailer is stored in folowing 2 lines (1. - include phpmailer class, 2. - set phpmailer directory):
Code:
...
require_once('YOR_PATH_TO_PHPMAILER/class.phpmailer.php');
...
$Mail->PluginDir   = 'YOR_PATH_TO_PHPMAILER/';
...

- From 1st post ("Example of using")

PS: And don't forget to overwrite the class files by patched ones wink
Logged
hiimdustin
Member
*
Offline Offline

Posts: 23


WWW
« Reply #15 on: September 10, 2006, 02:56:37 AM »

ahhhhhhh... thanks mr. hopkins! you're really a big help!hehe

 Cheesy  Cheesy  Cheesy
Logged

xazor
Member
*
Offline Offline

Posts: 46



WWW
« Reply #16 on: September 21, 2006, 04:57:28 AM »

Can u use this for phpnuke? is it possible? how? same way? thanks.
Logged

cyberdevil
People call me a
Hyper-Active Member
***
Offline Offline

Posts: 150

Permanently Banned


WWW
« Reply #17 on: September 29, 2006, 03:00:44 AM »

Yea man,need it badly for PHP nuke..Help will be appreciated..!
Logged

habesha
Member
*
Offline Offline

Posts: 2


hi
« Reply #18 on: September 30, 2006, 06:05:31 AM »

i saw ur post how can Change my email sending method from PHP to SMTP. can u help me plzzzzz

10q
Logged
.::xkiller213::.
Hyper-Active Member
***
Offline Offline

Posts: 325


Viva La 110mb~!


WWW
« Reply #19 on: September 30, 2006, 11:48:42 PM »

Hmmm... I still cannot get it to work... Btw I found out that the files would be written by this file: xoopsmultimailer.php
Code:
if (!defined("XOOPS_ROOT_PATH")) {
    die("XOOPS root path not defined");
}
/**
 * @package class
 * @subpackage mail
 *
 * @filesource
 *
 * @author Jochen Büînagel <jb@buennagel.com>
 * @copyright copyright (c) 2000-2003 The XOOPS Project (http://www.xoops.org)
 *
 * @version $Revision: 507 $ - $Date: 2006-05-27 01:39:35 +0200 (Sat, 27 May 2006) $
 */

/**
 * load the base class
 */
require_once(XOOPS_ROOT_PATH.'/class/mail/phpmailer/class.phpmailer.php');

/**
 * Mailer Class.
 *
 * At the moment, this does nothing but send email through PHP's "mail()" function,
 * but it has the abiltiy to do much more.
 *
 * If you have problems sending mail with "mail()", you can edit the member variables
 * to suit your setting. Later this will be possible through the admin panel.
 *
 * @todo Make a page in the admin panel for setting mailer preferences.
 *
 * @package class
 * @subpackage mail
 *
 * @author Jochen Buennagel <job@buennagel.com>
 * @copyright (c) 2000-2003 The Xoops Project - www.xoops.org
 * @version $Revision: 507 $ - changed by $Author$ on $Date: 2006-05-27 01:39:35 +0200 (Sat, 27 May 2006) $
 */
class XoopsMultiMailer extends PHPMailer {

/**
* "from" address
* @var string
* @access private
*/
var $From = "";

/**
* "from" name
* @var string
* @access private
*/
var $FromName = "";

// can be "smtp", "sendmail", or "mail"
/**
* Method to be used when sending the mail.
*
* This can be:
* <li>mail (standard PHP function "mail()") (default)
* <li>smtp (send through any SMTP server, SMTPAuth is supported.
* You must set {@link $Host}, for SMTPAuth also {@link $SMTPAuth},
* {@link $Username}, and {@link $Password}.)
* <li>sendmail (manually set the path to your sendmail program
* to something different than "mail()" uses in {@link $Sendmail})
*
* @var string
* @access private
*/
var $Mailer = "mail";

/**
* set if $Mailer is "sendmail"
*
* Only used if {@link $Mailer} is set to "sendmail".
* Contains the full path to your sendmail program or replacement.
* @var string
* @access private
*/
var $Sendmail = "/usr/sbin/sendmail";

/**
* SMTP Host.
*
* Only used if {@link $Mailer} is set to "smtp"
* @var string
* @access private
*/
var $Host = "";

/**
* Does your SMTP host require SMTPAuth authentication?
* @var boolean
* @access private
*/
var $SMTPAuth = TRUE;

/**
* Username for authentication with your SMTP host.
*
* Only used if {@link $Mailer} is "smtp" and {@link $SMTPAuth} is TRUE
* @var string
* @access private
*/
var $Username = "";

/**
* Password for SMTPAuth.
*
* Only used if {@link $Mailer} is "smtp" and {@link $SMTPAuth} is TRUE
* @var string
* @access private
*/
var $Password = "";

/**
* Constuctor
*
* @access public
* @return void
*
* @global $xoopsConfig
*/
function XoopsMultiMailer(){
global $xoopsConfig;

$config_handler =& xoops_gethandler('config');
$xoopsMailerConfig =& $config_handler->getConfigsByCat(XOOPS_CONF_MAILER);
$this->From = $xoopsMailerConfig['from'];
if ($this->From == '') {
   $this->From = $xoopsConfig['adminmail'];
}

if ($xoopsMailerConfig["mailmethod"] == "smtpauth") {
   $this->Mailer = "smtp";
$this->SMTPAuth = TRUE;
$this->Host = implode(';',$xoopsMailerConfig['smtphost']);
$this->Username = $xoopsMailerConfig['smtpuser'];
$this->Password = $xoopsMailerConfig['smtppass'];
} else {
$this->Mailer = $xoopsMailerConfig['mailmethod'];
$this->SMTPAuth = FALSE;
$this->Sendmail = $xoopsMailerConfig['sendmailpath'];
$this->Host = implode(';',$xoopsMailerConfig['smtphost']);
}
}

/**
     * Formats an address correctly. This overrides the default addr_format method which does not seem to encode $FromName correctly
     * @access private
     * @return string
     */
    function AddrFormat($addr) {
        if(empty($addr[1]))
            $formatted = $addr[0];
        else
            $formatted = sprintf('%s <%s>', '=?'.$this->CharSet.'?B?'.base64_encode($addr[1]).'?=', $addr[0]);

        return $formatted;
    }
}

?>


Do I need to change anything here? Btw I had already patched my installation with the ones you provided... I know that I had to select SMTPAuth...

Anyway I got this error:
Quote
Errors
Language string failed to load: connect_host


Logged

Pages: [1] 2   Go Up
Send this topic | Print
Jump to: