Mail-Webmail-Gmail

 view release on metacpan or  search on metacpan

lib/Mail/Webmail/Gmail.pm  view on Meta::CPAN

        @_,
    );

    if ( $args{ 'url' } ) {
        $args{ 'url' } =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack( "C", hex( $1 ) )/eg;
        return( $args{ 'url' } );
    } else {
        $self->{_error} = 1;
        $self->{_err_str} .= "Error: Must supply URL to unencode.";
        return;
    }
}

sub get_attachment {
    my ( $self ) = shift;
    my ( %args ) = (
        view   => 'att',
        disp   => 'attd',
        search => '',
        @_, );

    if ( defined( $args{ 'attid' } ) && defined( $args{ 'msgid' } ) ) {
        $args{ 'th' } = $args{ 'msgid' };
        delete( $args{ 'msgid' } );
    } elsif ( defined( $args{ 'attachment' } ) ) {
        if ( defined( $args{ 'attachment' }->{ 'id' } ) ) {
            $args{ 'attid' } = $args{ 'attachment' }->{ 'id' };
        } else {
            $self->{_error} = 1;
            $self->{_err_str} .= "Error: Not a valid attachment.1\n";
            return;
        }
        if ( defined( $args{ 'attachment' }->{ 'th' } ) ) {
            $args{ 'th' } = $args{ 'attachment' }->{ 'th' };
        } else {
            $self->{_error} = 1;
            $self->{_err_str} .= "Error: Not a valid attachment.2\n";
            return;
        }
        delete( $args{ 'attachment' } );        
    } else {
        $self->{_error} = 1;
        $self->{_err_str} .= "Error: Must supply attid and msgid or a reference to an attachment through 'attachment'.\n";
        return;
    }

    unless ( check_login( $self ) ) { return };
    
    my $res = get_page( $self, %args );

    if ( $res->is_success() ) {
        my $attachment = $res->content();
        return( \$attachment );
    } else {
        $self->{_error} = 1;
        $self->{_err_str} .= "Error: While requesting attachment: '$res->{_request}->{_uri}'.\n";
        return;
    }
}

sub update_prefs {
    my ( $self ) = shift;
    my ( %args ) = (
        view         => 'tl',
        act          => 'prefs',
        search       => 'inbox',
        @_, );

    unless ( check_login( $self ) ) { return };

    $args{ 'at' } = $self->{_cookies}->{GMAIL_AT};

    my ( %pref_mappings ) = (
        bx_hs => 'keyboard_shortcuts',
        ix_nt => 'max_page_size',
        bx_sc => 'indicators',
        sx_dn => 'display_name',
        bx_ns => 'snippets',
        sx_rt => 'reply_to',
        sx_sg => 'signature', );

    my ( %pref_args ) = (
        view    => 'pr',
        pnl     => 'g',
        search  => '',
        start   => '',
        method  => '',
    );

    my $pref_res = get_page( $self, %pref_args );

    if ( $pref_res->is_success() ) {
        my %functions = %{ parse_page( $self, $pref_res ) };

        if ( $self->{_error} ) {
            return;
        }

        unless ( defined( $functions{ 'p' } ) ) {
            return;
        }

        ### Delete if equal to the string '' ###
        foreach ( 'signature', 'reply_to', 'display_name' ) {
            if ( defined( $args{ $_ } ) ) {
                if ( $args{ $_ } eq '' ) {
                    $args{ $_ } = '%0A%0D';
                }
            }
        }

        ### Load Prefs if not redefined ###
        foreach ( @{ $functions{ 'p' } } ) {
            my ( @setting ) = @{ extract_fields( $_ ) };
            foreach ( @setting ) {
                $_ = remove_quotes( $_ );
            }
            unless ( defined( $args{ $pref_mappings{ $setting[0] } } ) ) {
                $args{ 'p_' . $setting[0] } = $setting[1];
            } else {
                $args{ 'p_' . $setting[0] } = $args{ $pref_mappings{ $setting[0] } };
            }
            delete( $args{ $pref_mappings{ $setting[0] } } );
        }

lib/Mail/Webmail/Gmail.pm  view on Meta::CPAN


The standard call for starting a new Gmail session is simply

    my $gmail = Mail::Webmail::Gmail->new( username => 'username', password => 'password', );

This module does support the use of a proxy server

    my $gmail = Mail::Webmail::Gmail->new( username => 'username', password => 'password', 
                proxy_username => 'proxy_username',
                proxy_password => 'proxy_password',
                proxy_name => 'proxy_server' );

By default, this module only encrypts the logon process.  To encrypt the entire session, use
the argument encrypt_session

    my $gmail = Mail::Webmail::Gmail->new( username => 'username', password => 'password', encrypt_session => 1 );

After that, you are free to start making requests for data.

=head2 RETRIEVING LABELS

Returns an array of all user defined labels.

    my @labels = $gmail->get_labels();

=head2 EDITING LABELS

There are five actions that can currently be preformed on labels.  As a note, this module enforces Gmail's 
limits on label creation.  A label cannot be over 40 characters, and a label cannot contain the character '^'.  
On failure, error and error_msg are set.

    #creating new labels.
    $gmail->edit_labels( label => 'label_name', action => 'create' );

    #renaming existing labels.
    $gmail->edit_labels( label => 'label_name', action => 'rename', new_name => 'renamed_label' );

    #deleting labels.
    $gmail->edit_labels( label => 'label_name', action => 'delete' );

    #adding a label to a message.
    $gmail->edit_labels( label => 'label_name', action => 'add', msgid => $message_id );

    #removing a label from a message.
    $gmail->edit_labels( label => 'label_name', action => 'remove', msgid => $message_id );

=head2 UPDATING PREFERENCES

The following are the seven preferences and the allowed values that can currently be changed through Mail::Webmail::Gmail

    keyboard_shortcuts = ( 0, 1 )
    indicators         = ( 0, 1 )
    snippets           = ( 0, 1 )
    max_page_size      = ( 25, 50, 100 )
    display_name       = ( '', string value up to 96 Characters )
    reply_to           = ( '', string value up to 320 Characters )
    signature          = ( '', string value up to 1000 Characters )

Changing preferences can be accomplished by simply sending the preference(s) that you want to change, and the new value.

    $gmail->update_prefs( indicators => 0, reply_to => 'test@test.com' );

To delete display_name, reply_to, or signature simply send a blank string as in the following example.

    $gmail->update_prefs( signature => '' );

=head2 STARRING A MESSAGE

To star or unstar a message use these examples

    #star
    $gmail->edit_star( action => 'add', 'msgid' => $msgid );

    #unstar
    $gmail->edit_star( action => 'remove', 'msgid' => $msgid );

=head2 ARCHIVING

To archive or unarchive a message use these examples

    #archive
    $gmail->edit_archive( action => 'archive', 'msgid' => $msgid );

    #unarchive
    $gmail->edit_archive( action => 'unarchive', 'msgid' => $msgid );

=head2 RETRIEVING MESSAGE LISTS

By default, get_messages returns a reference to an AoH with the messages from the 'all'
folder.  To change this behavior you can either send a label

    my $messages = $gmail->get_messages( label => 'work' );

Or request a Gmail provided folder using one of the provided variables

    'INBOX'
    'STARRED'
    'SPAM'
    'TRASH'

    Ex.

    my $messages = $gmail->get_messages( label => $Mail::Webmail::Gmail::FOLDERS{ 'INBOX' } );

The Array of hashes is in the following format

    $indv_email{ 'id' }
    $indv_email{ 'new' }
    $indv_email{ 'starred' }
    $indv_email{ 'date_received' }
    $indv_email{ 'sender_email' }
    $indv_email{ 'subject' }
    $indv_email{ 'blurb' }
    @{ $indv_email{ 'labels' } }
    @{ $indv_email{ 'attachments' } }

=head2 SPACE REMAINING

Returns a scalar with the amount of MB remaining in you account.

    my $remaining = $gmail->size_usage();

If called in list context, returns an array as follows.
[ Used, Total, Percent Used ]
[ "0 MB", "1000 MB", "0%" ]

lib/Mail/Webmail/Gmail.pm  view on Meta::CPAN

                    print $gmail->error_msg();
                }
            }
        }
    }
    ###

    ### Prints out the vendor link from Ads attached to a message
    my $messages = $gmail->get_messages( label => $Mail::Webmail::Gmail::FOLDERS{ 'INBOX' } );

    foreach ( @{ $messages } ) {
        print "ID: " . $_->{ 'id' } . "\n";
        my %email = %{ $gmail->get_indv_email( msg => $_ ) };
        if ( $email{ $_->{ 'id' } }->{ 'ads' } ) {
            my $ads;
            foreach $ads ( @{ $email{ $_->{ 'id' } }->{ 'ads' } } ) {
                print " - AD LINK: $ads->{vendor_link}\n";
            }
        }
    }
    ###

    ### Shows different ways to look through your email
    my $messages = $gmail->get_messages();

    print "By folder\n";
    foreach ( keys %Mail::Webmail::Gmail::FOLDERS ) {
        print "KEY: $_\n";
        my $messages = $gmail->get_messages( label => $Mail::Webmail::Gmail::FOLDERS{ $_ } );
        print "\t$_:\n";
        if ( @{ $messages } ) {
            foreach ( @{ $messages } ) {
                print "\t\t$_->{ 'subject' }\n";
            }
        }
    }

    print "By label\n";
    foreach ( $gmail->get_labels() ) {
        $messages = $gmail->get_messages( label => $_ );
        print "\t$_:\n";
        if ( defined( $messages ) ) {
            if ( @{ $messages } ) {
                foreach ( @{ $messages } ) {
                    print "\t\t$_->{ 'subject' }\n";
                }
            }
        }
    }

    print "All (Note: the All folder skips trash)";
    $messages = $gmail->get_messages();
    if ( @{ $messages } ) {
        foreach ( @{ $messages } ) {
            print "\t\t$_->{ 'subject' }\n";
        }
    }
    ###

    ### Update preferences
        if ( $gmail->update_prefs( signature => 'Test Sig.', max_page_size => 100 ) ) {
            print "Preferences Updated.\n";
        } else {
            print "Unable to update preferences.\n";
        }
    ###

    ### Show all contact email addresses
    my ( @contacts ) = @{ $gmail->get_contacts() };
    foreach ( @contacts ) {
        print $_->{ 'email' } . "\n";
    }
    ###

    ### Print out space remaining in mailbox
    my $remaining = $gmail->size_usage();
    print "Remaining: '" . $remaining . "'\n";
    ###

=head1 AUTHOR INFORMATION

Copyright 2004-2005, Allen Holman.  All rights reserved.  

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Address bug reports and comments to: 
email: mincus \at cpan \. org
AIM: mincus c03
Website: http://code.mincus.com

When sending bug reports, please provide the version of Gmail.pm, the version of
Perl and the name and version of the operating system you are using. 

=head1 CREDITS

I'd like to thank the following people who gave me a little direction in getting 
this module started (whether they know it or not)

=over 4

=item Simon Drabble (Mail::Webmail::Yahoo)

=item Erik F. Kastner (WWW::Scraper::Gmail)

=item Abiel J. (C# Gmail API - http://www.migraineheartache.com/)

=item Daniel Stutz (http://www.use-strict.net)

=back



( run in 1.274 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )