summaryrefslogtreecommitdiff
path: root/LedgerSMB/Mailer.pm
blob: 416f383f795e59c2d9c0b02563c4e9c2a9cd0e32 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. #
  5. # Copyright (C) 2006
  6. # This work contains copyrighted information from a number of sources all used
  7. # with permission.
  8. #
  9. # This file contains source code included with or based on SQL-Ledger which
  10. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  11. # under the GNU General Public License version 2 or, at your option, any later
  12. # version. For a full list including contact information of contributors,
  13. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  14. #
  15. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  16. # Copyright (C) 2002
  17. #
  18. # Author: DWS Systems Inc.
  19. # Web: http://www.sql-ledger.org
  20. #
  21. # Contributors:
  22. #
  23. # Original Author and copyright holder:
  24. # Dieter Simader dsmimader@sql-ledger.com
  25. #======================================================================
  26. #
  27. # This file has NOT undergone whitespace cleanup.
  28. #
  29. #======================================================================
  30. #
  31. # mailer package
  32. #
  33. #======================================================================
  34. package Mailer;
  35. sub new {
  36. my ($type) = @_;
  37. my $self = {};
  38. bless $self, $type;
  39. }
  40. sub send {
  41. my ($self, $out) = @_;
  42. my $boundary = time;
  43. $boundary = "LedgerSMB-$self->{version}-$boundary";
  44. my $domain = $self->{from};
  45. $domain =~ s/(.*?\@|>)//g;
  46. my $msgid = "$boundary\@$domain";
  47. $self->{charset} = "ISO-8859-1" unless $self->{charset};
  48. if ($out) {
  49. open(OUT, $out) or return "$out : $!";
  50. } else {
  51. open(OUT, ">-") or return "STDOUT : $!";
  52. }
  53. $self->{contenttype} = "text/plain" unless $self->{contenttype};
  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{cc} = "Cc: $h{cc}\n" if $self->{cc};
  62. $h{bcc} = "Bcc: $h{bcc}\n" if $self->{bcc};
  63. $h{notify} = "Disposition-Notification-To: $h{from}\n" if $self->{notify};
  64. $h{subject} = ($self->{subject} =~ /([\x00-\x1F]|[\x7B-\xFFFF])/) ? "Subject: =?$self->{charset}?B?".&encode_base64($self->{subject},"")."?=" : "Subject: $self->{subject}";
  65. print OUT qq|From: $h{from}
  66. To: $h{to}
  67. $h{cc}$h{bcc}$h{subject}
  68. Message-ID: <$msgid>
  69. $h{notify}X-Mailer: LedgerSMB $self->{version}
  70. MIME-Version: 1.0
  71. |;
  72. if (@{ $self->{attachments} }) {
  73. print OUT qq|Content-Type: multipart/mixed; boundary="$boundary"
  74. |;
  75. if ($self->{message} ne "") {
  76. print OUT qq|--${boundary}
  77. Content-Type: $self->{contenttype}; charset="$self->{charset}"
  78. $self->{message}
  79. |;
  80. }
  81. foreach my $attachment (@{ $self->{attachments} }) {
  82. my $application = ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/) ? "text" : "application";
  83. unless (open IN, $attachment) {
  84. close(OUT);
  85. return "$attachment : $!";
  86. }
  87. my $filename = $attachment;
  88. # strip path
  89. $filename =~ s/(.*\/|$self->{fileid})//g;
  90. print OUT qq|--${boundary}
  91. Content-Type: $application/$self->{format}; name="$filename"; charset="$self->{charset}"
  92. Content-Transfer-Encoding: BASE64
  93. Content-Disposition: attachment; filename="$filename"\n\n|;
  94. my $msg = "";
  95. while (<IN>) {;
  96. $msg .= $_;
  97. }
  98. print OUT &encode_base64($msg);
  99. close(IN);
  100. }
  101. print OUT qq|--${boundary}--\n|;
  102. } else {
  103. print OUT qq|Content-Type: $self->{contenttype}; charset="$self->{charset}"
  104. $self->{message}
  105. |;
  106. }
  107. close(OUT);
  108. return "";
  109. }
  110. sub encode_base64 ($;$) {
  111. # this code is from the MIME-Base64-2.12 package
  112. # Copyright (C) 2006
  113. # This work contains copyrighted information from a number of sources all used
  114. # with permission.
  115. #
  116. # This file contains source code included with or based on SQL-Ledger which
  117. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  118. # under the GNU General Public License version 2 or, at your option, any later
  119. # version. For a full list including contact information of contributors,
  120. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  121. #
  122. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  123. # Copyright (C) 2002
  124. #
  125. # Author: DWS Systems Inc.
  126. # Web: http://www.sql-ledger.org
  127. #
  128. # Contributors:
  129. #
  130. my $res = "";
  131. my $eol = $_[1];
  132. $eol = "\n" unless defined $eol;
  133. pos($_[0]) = 0; # ensure start at the beginning
  134. $res = join '', map( pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs));
  135. $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
  136. # fix padding at the end
  137. my $padding = (3 - length($_[0]) % 3) % 3;
  138. $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
  139. # break encoded string into lines of no more than 60 characters each
  140. if (length $eol) {
  141. $res =~ s/(.{1,60})/$1$eol/g;
  142. }
  143. return $res;
  144. }
  145. 1;