package Gmail;
# -----------------------------------------------------------------------------
# Author: 
#   Kumcho Vulcho <kumcho@vulcho.com>
#
# Url:
#   http://www.vulcho.com/perl/Gmail/Gmail.pm
#
# Purpose:
#   Perl OOP interface for sending mail via gmail account;
# -----------------------------------------------------------------------------

use Net::SMTP::SSL;
use strict;




sub new
{
    my $class = shift;
    my $this  = {@_};

    $this->{_host} = $this->{Host} || 'smtp.gmail.com';
    $this->{_port} = $this->{Port} || 465;
    $this->{_user} = $this->{User};
    $this->{_pass} = $this->{Pass};
    
    $this->{_header} = {};
    $this->{_smtp} = Net::SMTP::SSL->new($this->{_host}, 
					 Port => $this->{_port});

    bless $this, $class;
    return $this;
}


sub header
{
    my $this = shift;
    my $ref  = {@_};

    if (scalar keys %{$ref}) {
	foreach (keys %{$ref}) {
	    $this->{_header}->{$_} = $ref->{$_};
	}
    }

    return $this->{_header};
}


sub mail
{
    my $this = shift;
    my($to, $body) = @_;
    
    $this->{_smtp}->mail($this->{_user});
    $this->{_smtp}->to($to);

    $this->{_smtp}->data();
    $this->{_smtp}->datasend('To: '.$to."\r\n");
    $this->{_smtp}->datasend("From: ".$this->{_user}."\r\n");

    if (scalar keys %{$this->header()}) {
	foreach (keys %{ $this->{_header} }) {
	    $this->{_smtp}->datasend("$_: ".$this->{_header}->{$_}."\r\n");
	}
    }
    $this->{_smtp}->datasend("\r\n");

    $this->{_smtp}->datasend($body);
    $this->{_smtp}->dataend();
}


sub port
{
    my $this = shift;
    $this->{_port}=$_[0] if $_[0];
    return $this->{_port};
}


sub domain
{
    my $this = shift;
    $this->{_host}=$_[0] if $_[0];
    return $this->{_host};
}