Tuesday, March 09, 2010

Using Gmail SMTP for sending emails with Eprints 3

To successfully running an Eprints server, requirement of an email server is must. Since one need it to open/authenticate  the account and communicate with users.
Here I list the method I used to use a Gmail account for this purpose via SMTP.

Following is the "send_mail_via_smtp" section of $Eprints3PATH/perl_lib/EPrints/Email.pm file, which I modified to use with Gmail.

######################################################################
#=pod
#
#=item EPrints::Utils::send_mail_via_smtp( %properties )
#
#Send an email via STMP. Should not be called directly, but rather by
#EPrints::Utils::send_mail.
#
#=cut
######################################################################
### Modified extensively to use with Gamil SMTP ##### by Deepak Gupta

sub send_mail_via_smtp
{
    my( %p ) = @_;

    eval 'use Net::SMTP::TLS';

    my $repository = $p{session}->get_repository;

    my $smtphost = $repository->get_conf( 'smtp_server' );
    ##my $smtphost = 'smtp.gmail.com';

    if( !defined $smtphost )
    {
        $repository->log( "No STMP host has been defined. To fix this, find the full\naddress of your SMTP server (eg. smtp.example.com) and add it\nas the value of smtp_server in\nperl_lib/EPrints/SystemSettings.pm" );
        return( 0 );
    }


    my $smtp = new Net::SMTP::TLS (
           $smtphost,
        Hello   =>      'eprints-server.on-linux.org',
        Port    =>      587,
        User    =>      'eprints-server@gmail.com',
        Password=>      'gmail_password',
        );

    # my $smtp = Net::SMTP->new( $smtphost);

    if( !defined $smtp )
    {
        $repository->log( "Failed to create smtp connection to $smtphost" );
        return( 0 );
    }

    $smtp->mail( $p{from_email} );
    $smtp->to( $p{to_email} );
   
    # if( !$smtp->recipient( $p{to_email} ) )   
    # {
    #    $repository->log( "smtp server refused <$p{to_email}>" );
    #    $smtp->quit;
    #    return 0;
    # }

    my $message = build_email( %p );
    my $data = $message->as_string;
    # Send the message as bytes, to avoid Net::Cmd wide-character warnings
    utf8::encode($data);
    $smtp->data;
    $smtp->datasend( $data );
    $smtp->dataend;
    $smtp->quit;

    return 1;
}
#########################################

I also edited the following line in $Eprints3PATH/perl_lib/EPrint/SystemSettings.pm
 ....
 'smtp_server' => 'smtp.gmail.com',
 ....


I guess, any upgrade of  Eprints overwrite the above file. So save your working copy before any upgrade.


Restarted the Apache Server (using sudo /etc/init.d/apache2 restart) and Gmail with eprints server should be working.

1 Comments:

At Fri Jun 10, 12:31:00 AM PDT, Anonymous Anonymous said...

Hi,

I used your code but it is not working in my system. I'm still not able to send and receive mails through gmail. I have installed eprints on windows.Pls help.

Thanks.

 

Post a Comment

<< Home