summaryrefslogtreecommitdiff
path: root/LedgerSMB/Mailer.pm
blob: 05a711bdc9fb88c910c841766e2a028b4cf310c3 (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 =~
  78. /(^\w+$)|\.(html|text|txt|sql)$/)
  79. ? "text"
  80. : "application";
  81. my $filename = $attachment;
  82. # strip path
  83. $filename =~ s/(.*\/|$self->{fileid})//g;
  84. $msg->attach(
  85. 'Type' => "$application/$self->{format}",
  86. 'Path' => $attachment,
  87. 'Filename' => $filename,
  88. 'Disposition' => 'attachment',
  89. );
  90. }
  91. }
  92. if (${LedgerSMB::Sysconfig::smtphost}) {
  93. $msg->send('smtp', ${LedgerSMB::Sysconfig::smtphost},
  94. Timeout => ${LedgerSMB::Sysconfig::smtptimeout}) ||
  95. return $!;
  96. } else {
  97. $msg->send('sendmail', ${LedgerSMB::Sysconfig::sendmail}) ||
  98. return $!;
  99. }
  100. return "";
  101. }
  102. 1;