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