summaryrefslogtreecommitdiff
path: root/LedgerSMB/Mailer.pm
blob: 22574827a1f716d0df9c96e2284c3b7fd126c120 (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. use MIME::Base64;
  125. return MIME::Base64::encode($_[1], $_[2]);
  126. }
  127. 1;