PageRenderTime 21ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/CXGN/Contact.pm

https://github.com/solgenomics/sgn
Perl | 171 lines | 119 code | 37 blank | 15 comment | 12 complexity | c105a0403b5a2053175db6821b8c42df MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. =head1 NAME
  2. CXGN::Contact
  3. =head1 DESCRIPTION
  4. Allows scripts to send emails to the development team.
  5. =head1 FUNCTIONS
  6. =cut
  7. package CXGN::Contact;
  8. use strict;
  9. use Mail::Sendmail;
  10. use Email::Send::SMTP::Gmail;
  11. use Data::Dumper;
  12. use CXGN::Apache::Request;
  13. use CXGN::Tools::Text;
  14. use SGN::Context;
  15. =head2 send_email
  16. Usage:
  17. Desc : Sends an email to the development team.
  18. Args : - subject,
  19. - message body,
  20. - email address or conf key name that should contain an email
  21. address to send to
  22. - replyto address to include in the email
  23. Ret : nothing meaningful
  24. Side Effects: dies on error
  25. Example:
  26. CXGN::Contact::send_email($subject,$body,$mailto);#goes to wherever you say it should go
  27. CXGN::Contact::send_email($subject,$body,'email');#goes to email found in conf object
  28. CXGN::Contact::send_email($subject,$body,'bugs_email');#goes to bugs_email found in conf object
  29. CXGN::Contact::send_email($subject,$body);#goes to this script's default of bugs_email
  30. 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)
  31. =cut
  32. sub send_email {
  33. my ( $subject, $body, $mailto, $replyto ) = @_;
  34. my $request_info = "";
  35. my $vhost_conf = SGN::Context->new;
  36. my @main_production_site_url = split "\:\/\/", $vhost_conf->get_conf('main_production_site_url');
  37. my $hostname = $main_production_site_url[1];
  38. chomp($hostname);
  39. #my $dnsdomainname = `dnsdomainname`;
  40. #chomp($dnsdomainname);
  41. my $mailfrom = $vhost_conf->get_conf('www_user') . '@' . $hostname;
  42. #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.
  43. #mailto can also be specified normally (such as 'John Binns <zombieite@gmail.com>').
  44. if ( $mailto and eval{ $vhost_conf->get_conf($mailto)} ) {
  45. $mailto = $vhost_conf->get_conf($mailto);
  46. ##$request_info .= CXGN::Apache::Request::as_verbose_string();
  47. }
  48. #if we have no one specified to mail to, send it to bugs, and append the request info.
  49. unless ($mailto) {
  50. $mailto = $vhost_conf->get_conf('bugs_email')
  51. ; #for all emails that do not specify email address, send them to our bugs_email
  52. ##$request_info .= CXGN::Apache::Request::as_verbose_string()
  53. ; #append request info to all emails that do not specify email address
  54. }
  55. $subject ||= 'No subject specified';
  56. $body ||= 'No message body specified';
  57. $mailto ||= 'sgn-bugs@sgn.cornell.edu';
  58. $body .= $request_info;
  59. print STDERR "$subject\n\n$body";
  60. #if ( $vhost_conf->get_conf('production_server') ) {
  61. if ( $vhost_conf->get_conf('disable_emails') ) {
  62. print STDERR "CXGN::Contact: Configured as production server, but not configured to send emails; no email sent from $mailfrom to $mailto.\n";
  63. }
  64. else {
  65. my $smtp_server = $vhost_conf->get_conf('smtp_server');
  66. my $smtp_layer = $vhost_conf->get_conf('smtp_layer');
  67. my $smtp_port = $vhost_conf->get_conf('smtp_port');
  68. my $smtp_login = $vhost_conf->get_conf('smtp_login');
  69. my $smtp_pass = $vhost_conf->get_conf('smtp_pass');
  70. my $smtp_auth = $vhost_conf->get_conf('smtp_auth');
  71. my $smtp_from = $vhost_conf->get_conf('smtp_from') || $mailfrom;
  72. # If SMTP config values are found use external SMTP server
  73. if ( $smtp_server and $smtp_login and $smtp_pass ) {
  74. my ($mail,$error) = Email::Send::SMTP::Gmail->new(
  75. -smtp => $smtp_server,
  76. -layer => $smtp_layer,
  77. -port => $smtp_port,
  78. -login => $smtp_login,
  79. -pass => $smtp_pass,
  80. -auth => $smtp_auth
  81. );
  82. if ($mail == -1) {
  83. print STDERR "CXGN::Contact: SMTP error: $error\n";
  84. };
  85. $mail->send(
  86. -from => $smtp_from,
  87. -to => $mailto,
  88. -subject => $subject,
  89. -body => $body
  90. );
  91. } elsif ( $smtp_server ) {
  92. my ($mail,$error) = Email::Send::SMTP::Gmail->new(
  93. -smtp => $smtp_server,
  94. -layer => $smtp_layer,
  95. -port => $smtp_port,
  96. -auth => 'none'
  97. );
  98. if ($mail == -1) {
  99. print STDERR "CXGN::Contact: SMTP error: $error\n";
  100. };
  101. $mail->send(
  102. -from => $smtp_from,
  103. -to => $mailto,
  104. -subject => $subject,
  105. -body => $body
  106. );
  107. } else {
  108. my %mail = (
  109. To => $mailto,
  110. From => $mailfrom,
  111. Subject => $subject,
  112. Body => $body,
  113. );
  114. $mail{'Reply-To'} = $replyto;
  115. print STDERR "MAIL = ".Dumper(\%mail);
  116. if ( sendmail(%mail) ) {
  117. print STDERR "CXGN::Contact: Email notification sent from $mailfrom to $mailto.\n";
  118. }
  119. else {
  120. print STDERR "CXGN::Contact: UNABLE TO SEND EMAIL NOTIFICATION\n";
  121. }
  122. }
  123. }
  124. # }
  125. #else {
  126. # print STDERR "CXGN::Contact: Not configured as production server; no email sent from $mailfrom to $mailto.\n";
  127. #}
  128. }
  129. =head1 AUTHOR
  130. john binns - John Binns <zombieite@gmail.com>
  131. =cut
  132. ###
  133. 1;#do not remove
  134. ###