| 1 | package Bio::Graphics::Browser2::SendMail; | ||
| 2 | |||
| 3 | # $Id: UserDB.pm 23607 2010-07-30 17:34:25Z cnvandev $ | ||
| 4 | use strict; | ||
| 5 | use Bio::Graphics::Browser2; | ||
| 6 | use CGI qw(:standard); | ||
| 7 | use DBI; | ||
| 8 | use Digest::SHA qw(sha1_hex sha1); | ||
| 9 | use JSON; | ||
| 10 | use Text::ParseWords 'quotewords'; | ||
| 11 | use Digest::MD5 qw(md5_hex); | ||
| 12 | use Carp qw(confess cluck croak); | ||
| 13 | |||
| 14 | # Do Sendmail - Handles outgoing email using either Net::SMTP or Net::SMTP::SSL as required. | ||
| 15 | # The format of the smtp argument is: | ||
| 16 | # | ||
| 17 | # smtp.server.com:port:encryption:username:password | ||
| 18 | # | ||
| 19 | # This has up to five fields. Only the first field is required. | ||
| 20 | # The port is assumed to be 25 unless ssl encryption is specified, in which case it defaults to 465. | ||
| 21 | # The protocol is either "plain" or "ssl", "plain" assumed. | ||
| 22 | # The username and password may be required by the SMTP server to send outgoing mail. | ||
| 23 | sub do_sendmail { | ||
| 24 | my $self = shift; | ||
| 25 | my $args = shift; | ||
| 26 | my $globals = shift; | ||
| 27 | |||
| 28 | eval { | ||
| 29 | $globals->smtp or die "No SMTP server found in globals"; | ||
| 30 | |||
| 31 | my ($server, $port, $protocol, $username, $password) = split ':', $globals->smtp; | ||
| 32 | $protocol ||= 'plain'; | ||
| 33 | $port ||= $protocol eq 'plain' ? 25 : 465; | ||
| 34 | $protocol =~ /plain|ssl/ or die 'encryption must be either "plain" or "ssl"'; | ||
| 35 | |||
| 36 | # At least some SMTP servers will refuse to accept mail | ||
| 37 | # unless From matches the authentication username. | ||
| 38 | my $smtp_from = $username ? $username : $args->{from}; | ||
| 39 | |||
| 40 | my $smtp_sender; | ||
| 41 | if ($protocol eq 'plain') { | ||
| 42 | eval "require Net::SMTP" unless Net::SMTP->can('new'); | ||
| 43 | $smtp_sender = 'Net::SMTP'; | ||
| 44 | } else { | ||
| 45 | eval "require Net::SMTP::SSL" unless Net::SMTP::SSL->can('new'); | ||
| 46 | $smtp_sender = 'Net::SMTP::SSL'; | ||
| 47 | } | ||
| 48 | |||
| 49 | my $smtp_obj = $smtp_sender->new( | ||
| 50 | $server, | ||
| 51 | Port => $port, | ||
| 52 | Debug => 1, | ||
| 53 | ) | ||
| 54 | or die "Could not connect to outgoing mail server $server"; | ||
| 55 | |||
| 56 | if ($username) { | ||
| 57 | $smtp_obj->auth($username, $password) | ||
| 58 | or die "Could not authenticate with outgoing mail server $server: ",$smtp_obj->message | ||
| 59 | } | ||
| 60 | |||
| 61 | $smtp_obj->mail("$smtp_from\n") or die $smtp_obj->message; | ||
| 62 | $smtp_obj->to("$args->{to}\n") or die $smtp_obj->message; | ||
| 63 | $smtp_obj->data() or die $smtp_obj->message; | ||
| 64 | $smtp_obj->datasend("From: \"$args->{from_title}\" <$args->{from}>\n") | ||
| 65 | or die $smtp_obj->message; | ||
| 66 | $smtp_obj->datasend("To: $args->{to}\n") or die $smtp_obj->message; | ||
| 67 | $smtp_obj->datasend("Reply-to: $args->{from}\n") or die $smtp_obj->message; | ||
| 68 | $smtp_obj->datasend("Subject: $args->{subject}\n") or die $smtp_obj->message; | ||
| 69 | $smtp_obj->datasend("Content-type: text/html; charset=ISO-8859-1\n") | ||
| 70 | or die $smtp_obj->message | ||
| 71 | if $args->{HTML}; | ||
| 72 | $smtp_obj->datasend("\n") or die $smtp_obj->message; | ||
| 73 | $smtp_obj->datasend($args->{msg}) or die $smtp_obj->message; | ||
| 74 | $smtp_obj->datasend("\n") or die $smtp_obj->message; | ||
| 75 | $smtp_obj->dataend() or die $smtp_obj->message; | ||
| 76 | $smtp_obj->quit(); | ||
| 77 | }; | ||
| 78 | warn $@ if $@; | ||
| 79 | return (0, $@) if $@; | ||
| 80 | return (1,''); | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; | ||
| 84 | |||
| 85 | __END__ |