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