summaryrefslogtreecommitdiff
path: root/LedgerSMB/Mailer.pm
blob: 32d16ab3472ab63c9be1371f258d74fc68160892 (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 Encode;
  37. use MIME::Lite;
  38. use MIME::Base64;
  39. use LedgerSMB::Sysconfig;
  40. sub new {
  41. my ($type) = @_;
  42. my $self = {};
  43. bless $self, $type;
  44. }
  45. sub send {
  46. my ($self) = @_;
  47. my $domain = $self->{from};
  48. my $boundary = time;
  49. $boundary = "LSMB-$boundary";
  50. $domain =~ s/(.*?\@|>)//g;
  51. my $msgid = "$boundary\@$domain";
  52. $self->{contenttype} = "text/plain" unless $self->{contenttype};
  53. my $msgid = "$boundary\@$domain";
  54. my %h;
  55. for (qw(from to cc bcc)) {
  56. $self->{$_} =~ s/\&lt;/</g;
  57. $self->{$_} =~ s/\&gt;/>/g;
  58. $self->{$_} =~ s/(\/|\\|\$)//g;
  59. $h{$_} = $self->{$_};
  60. }
  61. $h{subject} = "Subject: ".Encode::encode('MIME-Header', $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. 'Message-ID' => $msg_id,
  71. );
  72. $msg->attr("Content-Type" => $self->{contenttype});
  73. $msg->add( 'Disposition-Notification-To' => $self->{from} )
  74. if $self->{notify};
  75. $msg->replace( 'X-Mailer' => "LedgerSMB $self->{version}" );
  76. if ( @{ $self->{attachments} } ) {
  77. foreach my $attachment ( @{ $self->{attachments} } ) {
  78. my $application =
  79. ( $attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/ )
  80. ? "text"
  81. : "application";
  82. my $filename = $attachment;
  83. # strip path
  84. $filename =~ s/(.*\/|$self->{fileid})//g;
  85. $msg->attach(
  86. 'Type' => "$application/$self->{format}",
  87. 'Path' => $attachment,
  88. 'Filename' => $filename,
  89. 'Disposition' => 'attachment',
  90. );
  91. }
  92. }
  93. if ( ${LedgerSMB::Sysconfig::smtphost} ) {
  94. $msg->send(
  95. 'smtp',
  96. ${LedgerSMB::Sysconfig::smtphost},
  97. Timeout => ${LedgerSMB::Sysconfig::smtptimeout}
  98. ) || return $!;
  99. }
  100. else {
  101. $msg->send( 'sendmail', ${LedgerSMB::Sysconfig::sendmail} )
  102. || return $!;
  103. }
  104. return "";
  105. }
  106. 1;