summaryrefslogtreecommitdiff
path: root/LedgerSMB/Mailer.pm
blob: db44cff03162ade851dabb7bef4b5a2801e13c31 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. #
  5. # See COPYRIGHT file for copyright information
  6. #======================================================================
  7. #
  8. # This file has NOT undergone whitespace cleanup.
  9. #
  10. #======================================================================
  11. #
  12. # mailer package
  13. #
  14. #======================================================================
  15. package Mailer;
  16. sub new {
  17. my ($type) = @_;
  18. my $self = {};
  19. bless $self, $type;
  20. }
  21. sub send {
  22. my ($self, $out) = @_;
  23. my $boundary = time;
  24. $boundary = "LedgerSMB-$self->{version}-$boundary";
  25. my $domain = $self->{from};
  26. $domain =~ s/(.*?\@|>)//g;
  27. my $msgid = "$boundary\@$domain";
  28. $self->{charset} = "ISO-8859-1" unless $self->{charset};
  29. if ($out) {
  30. open(OUT, $out) or return "$out : $!";
  31. } else {
  32. open(OUT, ">-") or return "STDOUT : $!";
  33. }
  34. $self->{contenttype} = "text/plain" unless $self->{contenttype};
  35. my %h;
  36. for (qw(from to cc bcc)) {
  37. $self->{$_} =~ s/\&lt;/</g;
  38. $self->{$_} =~ s/\&gt;/>/g;
  39. $self->{$_} =~ s/(\/|\\|\$)//g;
  40. $h{$_} = $self->{$_};
  41. }
  42. $h{cc} = "Cc: $h{cc}\n" if $self->{cc};
  43. $h{bcc} = "Bcc: $h{bcc}\n" if $self->{bcc};
  44. $h{notify} = "Disposition-Notification-To: $h{from}\n" if $self->{notify};
  45. $h{subject} = ($self->{subject} =~ /([\x00-\x1F]|[\x7B-\xFFFF])/) ? "Subject: =?$self->{charset}?B?".&encode_base64($self->{subject},"")."?=" : "Subject: $self->{subject}";
  46. print OUT qq|From: $h{from}
  47. To: $h{to}
  48. $h{cc}$h{bcc}$h{subject}
  49. Message-ID: <$msgid>
  50. $h{notify}X-Mailer: LedgerSMB $self->{version}
  51. MIME-Version: 1.0
  52. |;
  53. if (@{ $self->{attachments} }) {
  54. print OUT qq|Content-Type: multipart/mixed; boundary="$boundary"
  55. |;
  56. if ($self->{message} ne "") {
  57. print OUT qq|--${boundary}
  58. Content-Type: $self->{contenttype}; charset="$self->{charset}"
  59. $self->{message}
  60. |;
  61. }
  62. foreach my $attachment (@{ $self->{attachments} }) {
  63. my $application = ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/) ? "text" : "application";
  64. unless (open IN, $attachment) {
  65. close(OUT);
  66. return "$attachment : $!";
  67. }
  68. my $filename = $attachment;
  69. # strip path
  70. $filename =~ s/(.*\/|$self->{fileid})//g;
  71. print OUT qq|--${boundary}
  72. Content-Type: $application/$self->{format}; name="$filename"; charset="$self->{charset}"
  73. Content-Transfer-Encoding: BASE64
  74. Content-Disposition: attachment; filename="$filename"\n\n|;
  75. my $msg = "";
  76. while (<IN>) {;
  77. $msg .= $_;
  78. }
  79. print OUT &encode_base64($msg);
  80. close(IN);
  81. }
  82. print OUT qq|--${boundary}--\n|;
  83. } else {
  84. print OUT qq|Content-Type: $self->{contenttype}; charset="$self->{charset}"
  85. $self->{message}
  86. |;
  87. }
  88. close(OUT);
  89. return "";
  90. }
  91. sub encode_base64 ($;$) {
  92. # this code is from the MIME-Base64-2.12 package
  93. # Copyright 1995-1999,2001 Gisle Aas <gisle@ActiveState.com>
  94. my $res = "";
  95. my $eol = $_[1];
  96. $eol = "\n" unless defined $eol;
  97. pos($_[0]) = 0; # ensure start at the beginning
  98. $res = join '', map( pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs));
  99. $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
  100. # fix padding at the end
  101. my $padding = (3 - length($_[0]) % 3) % 3;
  102. $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
  103. # break encoded string into lines of no more than 60 characters each
  104. if (length $eol) {
  105. $res =~ s/(.{1,60})/$1$eol/g;
  106. }
  107. return $res;
  108. }
  109. 1;