interaction on sommer
[sgn.git] / lib / CXGN / Contact.pm
blob363da9f429944ca783a195d299f4750c8b03815d
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;
19 use Data::Dumper;
21 use CXGN::Apache::Request;
22 use CXGN::Tools::Text;
24 use SGN::Context;
26 =head2 send_email
28 Usage:
29 Desc : Sends an email to the development team.
30 Args : - subject,
31 - message body,
32 - email address or conf key name that should contain an email
33 address to send to
34 - replyto address to include in the email
35 Ret : nothing meaningful
36 Side Effects: dies on error
37 Example:
39 CXGN::Contact::send_email($subject,$body,$mailto);#goes to wherever you say it should go
40 CXGN::Contact::send_email($subject,$body,'email');#goes to email found in conf object
41 CXGN::Contact::send_email($subject,$body,'bugs_email');#goes to bugs_email found in conf object
42 CXGN::Contact::send_email($subject,$body);#goes to this script's default of bugs_email
43 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)
45 =cut
47 sub send_email {
48 my ( $subject, $body, $mailto, $replyto ) = @_;
49 my $request_info = "";
50 my $vhost_conf = SGN::Context->new;
51 my @main_production_site_url = split "\:\/\/", $vhost_conf->get_conf('main_production_site_url');
52 my $hostname = $main_production_site_url[1];
53 chomp($hostname);
54 #my $dnsdomainname = `dnsdomainname`;
55 #chomp($dnsdomainname);
56 my $mailfrom = $vhost_conf->get_conf('www_user') . '@' . $hostname;
58 #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.
59 #mailto can also be specified normally (such as 'John Binns <zombieite@gmail.com>').
60 if ( $mailto and eval{ $vhost_conf->get_conf($mailto)} ) {
61 $mailto = $vhost_conf->get_conf($mailto);
62 ##$request_info .= CXGN::Apache::Request::as_verbose_string();
65 #if we have no one specified to mail to, send it to bugs, and append the request info.
66 unless ($mailto) {
67 $mailto = $vhost_conf->get_conf('bugs_email')
68 ; #for all emails that do not specify email address, send them to our bugs_email
69 ##$request_info .= CXGN::Apache::Request::as_verbose_string()
70 ; #append request info to all emails that do not specify email address
73 $subject ||= 'No subject specified';
74 $body ||= 'No message body specified';
75 $mailto ||= 'sgn-bugs@sgn.cornell.edu';
77 $body .= $request_info;
78 print STDERR "$subject\n\n$body";
79 #if ( $vhost_conf->get_conf('production_server') ) {
80 if ( $vhost_conf->get_conf('disable_emails') ) {
81 print STDERR "CXGN::Contact: Configured as production server, but not configured to send emails; no email sent from $mailfrom to $mailto.\n";
83 else {
84 my $smtp_server = $vhost_conf->get_conf('smtp_server');
85 my $smtp_layer = $vhost_conf->get_conf('smtp_layer');
86 my $smtp_port = $vhost_conf->get_conf('smtp_port');
87 my $smtp_login = $vhost_conf->get_conf('smtp_login');
88 my $smtp_pass = $vhost_conf->get_conf('smtp_pass');
89 my $smtp_auth = $vhost_conf->get_conf('smtp_auth');
90 my $smtp_from = $vhost_conf->get_conf('smtp_from') || $mailfrom;
92 # If SMTP config values are found use external SMTP server
93 if ( $smtp_server and $smtp_login and $smtp_pass ) {
95 my ($mail,$error) = Email::Send::SMTP::Gmail->new(
96 -smtp => $smtp_server,
97 -layer => $smtp_layer,
98 -port => $smtp_port,
99 -login => $smtp_login,
100 -pass => $smtp_pass,
101 -auth => $smtp_auth
104 if ($mail == -1) {
105 print STDERR "CXGN::Contact: SMTP error: $error\n";
108 $mail->send(
109 -from => $smtp_from,
110 -to => $mailto,
111 -subject => $subject,
112 -body => $body
115 } elsif ( $smtp_server ) {
117 my ($mail,$error) = Email::Send::SMTP::Gmail->new(
118 -smtp => $smtp_server,
119 -layer => $smtp_layer,
120 -port => $smtp_port,
121 -auth => 'none'
124 if ($mail == -1) {
125 print STDERR "CXGN::Contact: SMTP error: $error\n";
128 $mail->send(
129 -from => $smtp_from,
130 -to => $mailto,
131 -subject => $subject,
132 -body => $body
135 } else {
137 my %mail = (
138 To => $mailto,
139 From => $mailfrom,
140 Subject => $subject,
141 Body => $body,
143 $mail{'Reply-To'} = $replyto;
145 print STDERR "MAIL = ".Dumper(\%mail);
147 if ( sendmail(%mail) ) {
148 print STDERR "CXGN::Contact: Email notification sent from $mailfrom to $mailto.\n";
150 else {
151 print STDERR "CXGN::Contact: UNABLE TO SEND EMAIL NOTIFICATION\n";
157 #else {
158 # print STDERR "CXGN::Contact: Not configured as production server; no email sent from $mailfrom to $mailto.\n";
163 =head1 AUTHOR
165 john binns - John Binns <zombieite@gmail.com>
167 =cut
170 1;#do not remove