Merge pull request #2890 from solgenomics/topic/check_login_always
[sgn.git] / lib / CXGN / Contact.pm
bloba658b0c8727b03e09cdb8aebb28008b52629b5f7
2 =head1 NAME
4 CXGN::Contact
6 =head1 DESCRIPTION
8 Allows scripts to send emails to the development team.
10 =head1 FUNCTIONS
12 =cut
14 package CXGN::Contact;
15 use strict;
17 use Mail::Sendmail;
18 use Email::Send::SMTP::Gmail;
20 use CXGN::Apache::Request;
21 use CXGN::Tools::Text;
23 use SGN::Context;
25 =head2 send_email
27 Usage:
28 Desc : Sends an email to the development team.
29 Args : - subject,
30 - message body,
31 - email address or conf key name that should contain an email
32 address to send to
33 - replyto address to include in the email
34 Ret : nothing meaningful
35 Side Effects: dies on error
36 Example:
38 CXGN::Contact::send_email($subject,$body,$mailto);#goes to wherever you say it should go
39 CXGN::Contact::send_email($subject,$body,'email');#goes to email found in conf object
40 CXGN::Contact::send_email($subject,$body,'bugs_email');#goes to bugs_email found in conf object
41 CXGN::Contact::send_email($subject,$body);#goes to this script's default of bugs_email
42 CXGN::Contact::send_email($subject,$body,$mailto,$replyto);#sends an email with a reply-to different from the sender (the sender is the apache user from the conf object, usually www-data)
44 =cut
46 sub send_email {
47 my ( $subject, $body, $mailto, $replyto ) = @_;
48 my $request_info = "";
49 my $vhost_conf = SGN::Context->new;
50 my @main_production_site_url = split "\:\/\/", $vhost_conf->get_conf('main_production_site_url');
51 my $hostname = $main_production_site_url[1];
52 chomp($hostname);
53 #my $dnsdomainname = `dnsdomainname`;
54 #chomp($dnsdomainname);
55 my $mailfrom = $vhost_conf->get_conf('www_user') . '@' . $hostname;
57 #if we are specifying a mailto as a vhost configuration variable (such as 'bugs_email'), then use that variable's value, and append the request info.
58 #mailto can also be specified normally (such as 'John Binns <zombieite@gmail.com>').
59 if ( $mailto and eval{ $vhost_conf->get_conf($mailto)} ) {
60 $mailto = $vhost_conf->get_conf($mailto);
61 ##$request_info .= CXGN::Apache::Request::as_verbose_string();
64 #if we have no one specified to mail to, send it to bugs, and append the request info.
65 unless ($mailto) {
66 $mailto = $vhost_conf->get_conf('bugs_email')
67 ; #for all emails that do not specify email address, send them to our bugs_email
68 ##$request_info .= CXGN::Apache::Request::as_verbose_string()
69 ; #append request info to all emails that do not specify email address
72 $subject ||= 'No subject specified';
73 $body ||= 'No message body specified';
74 $mailto ||= 'sgn-bugs@sgn.cornell.edu';
76 $body .= $request_info;
77 print STDERR "$subject\n\n$body";
78 if ( $vhost_conf->get_conf('production_server') ) {
79 if ( $vhost_conf->get_conf('disable_emails') ) {
80 print STDERR "CXGN::Contact: Configured as production server, but not configured to send emails; no email sent from $mailfrom to $mailto.\n";
82 else {
83 my $smtp_server = $vhost_conf->get_conf('smtp_server');
84 my $smtp_layer = $vhost_conf->get_conf('smtp_layer');
85 my $smtp_port = $vhost_conf->get_conf('smtp_port');
86 my $smtp_login = $vhost_conf->get_conf('smtp_login');
87 my $smtp_pass = $vhost_conf->get_conf('smtp_pass');
88 my $smtp_auth = $vhost_conf->get_conf('smtp_auth');
89 my $smtp_from = $vhost_conf->get_conf('smtp_from') || $mailfrom;
91 # If SMTP config values are found use external SMTP server
92 if ( $smtp_server and $smtp_login and $smtp_pass ) {
94 my ($mail,$error) = Email::Send::SMTP::Gmail->new(
95 -smtp => $smtp_server,
96 -layer => $smtp_layer,
97 -port => $smtp_port,
98 -login => $smtp_login,
99 -pass => $smtp_pass,
100 -auth => $smtp_auth
103 if ($mail == -1) {
104 print STDERR "CXGN::Contact: SMTP error: $error\n";
107 $mail->send(
108 -from => $smtp_from,
109 -to => $mailto,
110 -subject => $subject,
111 -body => $body
114 } else {
116 my %mail = (
117 To => $mailto,
118 From => $mailfrom,
119 Subject => $subject,
120 Body => $body,
122 $mail{'Reply-To'} = $replyto;
124 if ( sendmail(%mail) ) {
125 print STDERR "CXGN::Contact: Email notification sent from $mailfrom to $mailto.\n";
127 else {
128 print STDERR "CXGN::Contact: UNABLE TO SEND EMAIL NOTIFICATION\n";
134 else {
135 print STDERR "CXGN::Contact: Not configured as production server; no email sent from $mailfrom to $mailto.\n";
140 =head1 AUTHOR
142 john binns - John Binns <zombieite@gmail.com>
144 =cut
147 1;#do not remove