Home : Products : Gossamer Links : Discussions :

Products: Gossamer Links: Discussions: Re: [Andy] Links::send_email get_body with subject: Edit Log

Here is the list of edits for this post
Re: [Andy] Links::send_email get_body with subject
Hi Andy,

Found some time now. I will try both and see which one is faster but honestly I donīt care about fractions of seconds.
I had a lot of troubles with mails ending up in spam and so I am really curious about the headers and bodies that both variations create.
I appreciate your thoughts about pros and cons.

Maybe the false html if no file is available is an inspiration for you.

PDF seems to be some thing we needed both, I have textfiles I have to add from time to time so there are some additional lines.
Attachments with other endings I do not care about at the moment.

Code:
sub send_invoice {
my $results = shift;
my $mail;
my $mail_tpl;
$mail_tpl->{language} = 'de';
$mail_tpl->{txt_file} = 'eml_send_invoice_txt_' . $mail_tpl->{language} . '.eml';
$mail_tpl->{html_file} = 'eml_send_invoice_html_' . $mail_tpl->{language} . '.eml';
$mail_tpl->{txt} = Links::send_email($mail_tpl->{txt_file}, $results, { get_body_and_subject => 1 });
eval {
$mail_tpl->{html} = Links::send_email($mail_tpl->{html_file}, $results, { get_body_and_subject => 1 });
};
if ($@) {
$mail_tpl->{html}->{body} = min_text_to_html($mail_tpl->{txt}->{body});
}
$mail->{to} = 'default@recipient.domain';
$mail->{subject} = $mail_tpl->{txt}->{subject};
$mail->{txt_body} = $mail_tpl->{txt}->{body};
$mail->{html_body} = $mail_tpl->{html}->{body};
&send_mp_mail($mail);
}

Code:
sub send_mp_mail {
use MIME::Lite;
use MIME::Base64;
BEGIN { $MIME::Lite::VANILLA = 1; }
my $xmailer = "my x mailer";
use LWP::Simple;
use Unicode::UTF8simple;
use HTML::Entities;
use Encode;
my $uref = new Unicode::UTF8simple;
my $mail = shift;
my $suppress_archiv = shift || undef;
my $html_mail = $uref->toUTF8("iso-8859-1", $mail->{html_body});
my $txt_mail = $uref->toUTF8("iso-8859-1", $mail->{txt_body});
my $subject = "=?UTF-8?B?" . encode_base64(encode("utf8", decode_entities($mail->{subject})), "") . "?=";
my $from = $mail->{from} ? $mail->{from} : '"default company" <default@sender.domain>';
my $return_path = $mail->{return_path} ? $mail->{return_path} : 'default@sender.domain';
my $bcc = 'archiv@sender.domain' unless $suppress_archiv;
my $msg = MIME::Lite->new(
From => $from,
To => $mail->{to},
Cc => $mail->{cc},
Bcc => $bcc,
Type => 'multipart/mixed',
Subject => $subject,
"X-Mailer" => $xmailer,
"Return-Path" => $return_path,
);
my $part = MIME::Lite->new(
Type => 'multipart/alternative',
);
#add txt mail
my $att_text = MIME::Lite->new(
Type => 'text',
Data => $txt_mail,
Encoding => 'quoted-printable',
"X-Mailer" => $xmailer,
);
$att_text->attr('content-type' => 'text/plain; charset=UTF-8');
$part->attach($att_text);
#add html mail
my $att_html = MIME::Lite->new(
Type => 'text',
Data => $html_mail,
Encoding => 'quoted-printable',
"X-Mailer" => $xmailer,
);
$att_html->attr('content-type' => 'text/html; charset=UTF-8');
$part->attach($att_html);
$msg->attach($part);
if (defined @{$mail->{attachment_data}}) {
foreach (@{$mail->{attachment_data}}) {
$msg->attach(
Type => 'text/plain',
Data => $_->{data},
Disposition => 'attachment',
Filename => $_->{filename},
"X-Mailer" => $xmailer,
);
}
}
if (defined @{$mail->{attachment_urls}}) {
foreach (@{$mail->{attachment_urls}}) {
$msg->attach(
Type => 'application/pdf',
Data => get($_->{url}),
Disposition => 'attachment',
Filename => $_->{filename},
"X-Mailer" => $xmailer,
);
}
}
use Net::SMTP::SSL;
eval {
my $smtp;
$smtp = Net::SMTP::SSL->new('smtp.sender.domain', Port=>465, Debug=>0, Timeout=>20) or die "$_ Can't connect";
$smtp->auth('username', 'password') or die "Can't authenticate:" . $smtp->message();
$smtp->mail($from) or die "Error:" . $smtp->message();
$smtp->to($mail->{to}) or die "Error:" . $smtp->message();
if ($mail->{cc}) {
$smtp->cc($mail->{cc}) or die "Error:" . $smtp->message();
}
unless ($suppress_archiv) {
$smtp->bcc($bcc) or die "Error:" . $smtp->message();
}
$smtp->data() or die "Error:" . $smtp->message();
$smtp->datasend($msg->as_string) or die "Error:" . $smtp->message();
$smtp->dataend() or die "Error:" . $smtp->message();
$smtp->quit() or die "Error:" . $smtp->message();
};
if ($@) {
eval {
$msg->send;#fallback
};
}
}

Code:
sub min_text_to_html {
my $text = shift;
my $results;
for (split /^/, $text) {
if ($_ =~ /(.*)/) {
$_ = "$1<br>\n";
}
$results .= $_;
}
return $results;
}

Regards

Niko

Last edited by:

el noe: Jun 14, 2020, 11:54 AM

Edit Log: