Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Trying to send a nicely formatted email -> GMail

by chexmix (Hermit)
on Apr 25, 2018 at 15:21 UTC ( #1213536=perlquestion: print w/replies, xml ) Need Help??

chexmix has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm working on a project where I'm going to need to send a decently-formatted table of things via email -- GMail, unfortunately, which I've found needs special lines for encoding the thing to "quoted-printable" to not destroy the nice formatting I've set up in the script.

The REAL script will get its data from a database, but I've put the following dummy script together to play with the modules I assume are necessary to get the email sent and look nice.

... and it's not working. I get blank emails with subject lines like "ARRAY(0x2730298)". I have futzed and futzed with the script-let below ... maybe a monk smarter than me (most of you) can tell me what I'm doing wrong.

#!/usr/bin/env perl use IO::All; use Email::MIME; use Email::Sender::Simple qw(sendmail); unlink 'mailtext.txt'; ################################################## # just an experiment with perlform to try and make # a nice output for the dashboard # ################################################ # set some vars so we can have stuff to print # what are we likely gonna wanna print? my $tweet_id = 0; my $tweet_cat = ''; # this is tweet category my $tweet_obj = ''; # this is tweet object: obsid? bibcode? my $tweet_obj_2 = ''; # secondary id - for instance, seq_nbr my $tweet_date = ''; my $tweet_weight = 0; open (MAILTEXT, '>>', 'mailtext.txt'); print(MAILTEXT "Content-Type: text/plain; charset=UTF-8\Content-Transf +er-Encoding: quoted-printable\n"); format TWIT = @<<<<<< @<<<<< @<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<< @<< +<<<<<<<< ^<<<<<<<<< $tweet_id, $tweet_cat, $tweet_obj, $tweet_obj_2, $tweet_date, $tweet_w +eight ---------------------------------------------------------------------- +------------------------ . format TWIT_TOP = ====================================================================== +======================== twit_id twit_cat twit_obj twit_obj_2 dat +e_active twit_weight ====================================================================== +======================== . select(MAILTEXT); $~ = TWIT; $^ = TWIT_TOP; my @id = (50, 51, 52, 53, 54); my @cat = ("BIB", "BIB", "DAT", "PLE", "UNP"); my @obj = ("2014HEAD...1411609Z", "2016SPIE.9905E..45G","20201", "2 +018ApJ...855..100S", "19288"); my @obj_2 = (254, 29, 'X-ray Clusters', 5.1, 502938); my @date = ("02/02/18","03/23/18","03/22/18","03/13/18","03/05/18"); my @weight = (2, 5, 4, 3, 1); my $i = 0; foreach (@id) { $tweet_id = $_; $tweet_cat = $cat[$i]; $tweet_obj = $obj[$i]; $tweet_obj_2 = $obj_2[$i]; $tweet_date = $date[$i]; $tweet_weight = $weight[$i++]; write; } my @parts = ( Email::MIME->create( attributes => { filename => "mailtext.txt", content_type => "text/plain", encoding => "quoted-printable", name => "CDA Twitter Dashboard", }, body => io( "mailtext.txt")->utf8->all, ), ); my $email = Email::MIME->create( header_str => [ From => [ "cdatwitter\@grumble.edu" ], To => [ "gbecker\@grumble.edu" ], Subject => [ "Yes this is for Twitter" ], ], parts => [ @parts ], ); sendmail($email);

I'm sure the script sucks in many ways, but it's the emailing I'm concerned about here. Any nudges would be most appreciated.

Thanks,

Glenn

Replies are listed 'Best First'.
Re: Trying to send a nicely formatted email -> GMail
by hippo (Bishop) on Apr 25, 2018 at 15:50 UTC
    my $email = Email::MIME->create( header_str => [ From => [ "cdatwitter\@grumble.edu" ], To => [ "gbecker\@grumble.edu" ], Subject => [ "Yes this is for Twitter" ], ], parts => [ @parts ], );

    You are using arrayrefs for everything - that isn't how the synopsis shows you to use the header_str field. Try:

    my $email = Email::MIME->create( header_str => [ From => 'cdatwitter@grumble.edu', To => 'gbecker@grumble.edu', Subject => 'Yes this is for Twitter', ], parts => [ @parts ], );

      Just a minor note here :)

      The To       => 'gbecker@grumble.edu', should be To       => [ 'gbecker@grumble.edu' ], as it can be multiple recipients.

      For example from Email::MIME::SYNOPSIS: To => [ 'user1@host.com', 'Name <user2@host.com>' ],.

      BR / Thanos

      Seeking for Perl wisdom...on the process of learning...not there...yet!

        Minor counter-note: the synopsis shows both arrayref and scalar:

        header_str => [ From => 'casey@geeknest.com', To => [ 'user1@host.com', 'Name <user2@host.com>' ], Cc => Email::Address::XS->new("Display Name \N{U+1F600}", 'use +r@example.com'), ], ... header_str => [ From => 'my@address', To => 'your@address', ],
        so either are presumably allowed. The important thing is that the From and the Subject are never shown as arrayref (and it doesn't make sense for more than one sender or more than one subject line)

      Progress, thanks. I'm still getting a blank email, but the subject line is correct.

      Not sure what I need to do to get that text file inserted, but will keep trying.

        Add close MAILTEXT; at line 63

        poj
Re: Trying to send a nicely formatted email -> GMail
by thanos1983 (Parson) on Apr 25, 2018 at 15:39 UTC

    Hello chexmix,

    Have you tried to use Email::Send::Gmail or Email::Send::SMTP::Gmail. They seem to be using a free Web-based email service provided by Google. I assume that they will provide better format that your designed. Give it a try and let us know if it works as expected.

    Also you can take a look on an older similar question Sending mails via gmail.

    Looking forward to your reply. BR / Thanos

    Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1213536]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2022-08-12 02:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?