summaryrefslogtreecommitdiff
path: root/LedgerSMB/Mailer.pm
blob: eb1d4a9b56689cbf135afb843c8755f83e5c553e (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://www.ledgersmb.org/
  5. #
  6. # Copyright (C) 2006
  7. # This work contains copyrighted information from a number of sources all used
  8. # with permission.
  9. #
  10. # This file contains source code included with or based on SQL-Ledger which
  11. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  12. # under the GNU General Public License version 2 or, at your option, any later
  13. # version. For a full list including contact information of contributors,
  14. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  15. #
  16. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  17. # Copyright (C) 2002
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors:
  23. #
  24. # Original Author and copyright holder:
  25. # Dieter Simader dsmimader@sql-ledger.com
  26. #======================================================================
  27. #
  28. # This file has undergone whitespace cleanup.
  29. #
  30. #======================================================================
  31. #
  32. # mailer package
  33. #
  34. #======================================================================
  35. package Mailer;
  36. use MIME::Lite;
  37. use MIME::Base64;
  38. use LedgerSMB::Sysconfig;
  39. sub new {
  40. my ($type) = @_;
  41. my $self = {};
  42. bless $self, $type;
  43. }
  44. sub send {
  45. my ($self) = @_;
  46. my $domain = $self->{from};
  47. $domain =~ s/(.*?\@|>)//g;
  48. my $msgid = "$boundary\@$domain";
  49. $self->{contenttype} = "text/plain" unless $self->{contenttype};
  50. my %h;
  51. for (qw(from to cc bcc)) {
  52. $self->{$_} =~ s/\&lt;/</g;
  53. $self->{$_} =~ s/\&gt;/>/g;
  54. $self->{$_} =~ s/(\/|\\|\$)//g;
  55. $h{$_} = $self->{$_};
  56. }
  57. $h{subject} =
  58. ( $self->{subject} =~ /([\x00-\x1F]|[\x7B-\xFFFF])/ )
  59. ? "Subject: =?$self->{charset}?B?"
  60. . MIME::Base64::encode( $self->{subject}, "" ) . "?="
  61. : "Subject: $self->{subject}";
  62. my $msg = MIME::Lite->new(
  63. 'From' => $self->{from},
  64. 'To' => $self->{to},
  65. 'Cc' => $self->{cc},
  66. 'Bcc' => $self->{bcc},
  67. 'Subject' => $self->{subject},
  68. 'Type' => 'TEXT',
  69. 'Data' => $self->{message},
  70. );
  71. $msg->add( 'Disposition-Notification-To' => $self->{from} )
  72. if $self->{notify};
  73. $msg->replace( 'X-Mailer' => "LedgerSMB $self->{version}" );
  74. if ( @{ $self->{attachments} } ) {
  75. foreach my $attachment ( @{ $self->{attachments} } ) {
  76. my $application =
  77. ( $attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/ )
  78. ? "text"
  79. : "application";
  80. my $filename = $attachment;
  81. # strip path
  82. $filename =~ s/(.*\/|$self->{fileid})//g;
  83. $msg->attach(
  84. 'Type' => "$application/$self->{format}",
  85. 'Path' => $attachment,
  86. 'Filename' => $filename,
  87. 'Disposition' => 'attachment',
  88. );
  89. }
  90. }
  91. if ( ${LedgerSMB::Sysconfig::smtphost} ) {
  92. $msg->send(
  93. 'smtp',
  94. ${LedgerSMB::Sysconfig::smtphost},
  95. Timeout => ${LedgerSMB::Sysconfig::smtptimeout}
  96. ) || return $!;
  97. }
  98. else {
  99. $msg->send( 'sendmail', ${LedgerSMB::Sysconfig::sendmail} )
  100. || return $!;
  101. }
  102. return "";
  103. }
  104. 1;