summaryrefslogtreecommitdiff
path: root/LedgerSMB/Mailer.pm
blob: 0e14c8f4ce682e01d9f73ff68ad4609953804117 (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. my $boundary = time;
  48. $boundary = "LSMB-$boundary";
  49. $domain =~ s/(.*?\@|>)//g;
  50. my $msgid = "$boundary\@$domain";
  51. $self->{contenttype} = "text/plain" unless $self->{contenttype};
  52. my $msgid = "$boundary\@$domain";
  53. my %h;
  54. for (qw(from to cc bcc)) {
  55. $self->{$_} =~ s/\&lt;/</g;
  56. $self->{$_} =~ s/\&gt;/>/g;
  57. $self->{$_} =~ s/(\/|\\|\$)//g;
  58. $h{$_} = $self->{$_};
  59. }
  60. $h{subject} =
  61. ( $self->{subject} =~ /([\x00-\x1F]|[\x7B-\xFFFF])/ )
  62. ? "Subject: =?$self->{charset}?B?"
  63. . MIME::Base64::encode( $self->{subject}, "" ) . "?="
  64. : "Subject: $self->{subject}";
  65. my $msg = MIME::Lite->new(
  66. 'From' => $self->{from},
  67. 'To' => $self->{to},
  68. 'Cc' => $self->{cc},
  69. 'Bcc' => $self->{bcc},
  70. 'Subject' => $self->{subject},
  71. 'Type' => 'TEXT',
  72. 'Data' => $self->{message},
  73. 'Message-ID' => $msg_id,
  74. );
  75. $msg->attr("Content-Type" => $self->{contenttype});
  76. $msg->add( 'Disposition-Notification-To' => $self->{from} )
  77. if $self->{notify};
  78. $msg->replace( 'X-Mailer' => "LedgerSMB $self->{version}" );
  79. if ( @{ $self->{attachments} } ) {
  80. foreach my $attachment ( @{ $self->{attachments} } ) {
  81. my $application =
  82. ( $attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/ )
  83. ? "text"
  84. : "application";
  85. my $filename = $attachment;
  86. # strip path
  87. $filename =~ s/(.*\/|$self->{fileid})//g;
  88. $msg->attach(
  89. 'Type' => "$application/$self->{format}",
  90. 'Path' => $attachment,
  91. 'Filename' => $filename,
  92. 'Disposition' => 'attachment',
  93. );
  94. }
  95. }
  96. if ( ${LedgerSMB::Sysconfig::smtphost} ) {
  97. $msg->send(
  98. 'smtp',
  99. ${LedgerSMB::Sysconfig::smtphost},
  100. Timeout => ${LedgerSMB::Sysconfig::smtptimeout}
  101. ) || return $!;
  102. }
  103. else {
  104. $msg->send( 'sendmail', ${LedgerSMB::Sysconfig::sendmail} )
  105. || return $!;
  106. }
  107. return "";
  108. }
  109. 1;