Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

adding carriage return to gmail script

by flieckster (Scribe)
on Jun 13, 2016 at 14:43 UTC ( #1165479=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, i have a quick on that i can't seem to figure out. I installed a script to send out the contents of a folder by globing contents then sending an email via gmail. all works well expect for one part, i can't get a "\n" after each file name like i normally would.
#!/usr/bin/env perl use strict; use warnings; use File::Find; use File::Copy; use Email::Send; use Mail::Sender; use Email::Send::SMTP::Gmail; use POSIX qw(strftime); my $date = strftime("%m-%d-%y",localtime); my $time = strftime("%I:%M:%S",localtime); my $directory ="/Users/flieckb/Desktop/files/"; my $to ='brianflieck\@gmail.com'; my $from='bflieck\@industrialcolor.com'; my $subject = "test"; chdir( $directory ) or warn "Cant chdir to $directory $!"; my(@push_list) = glob "*"; my $filecount = @push_list; my ($mail,$error)=Email::Send::SMTP::Gmail->new( -smtp=>'smtp-relay.gm +ail.com', -login=>'youremailadd +resss, -pass=>'yourpassword' +, # -layer=> 'ssl', -port=> '25', -debug=> 1,); $mail->send(-to=>"$to", -from=>"$from", -subject=>"$subject", -body=>" +@push_list\n total files pushed: $filecount"); $mail->bye;
all works well, but when i get the email it looks like this.

1a.txt 1b.txt 1c.txt 1d.txt 1e.txt 1f.txt 1g.txt 1h.txt 1i.txt 1j.txt 1k.txt 1l.txt 1m.txt 1n.txt 1o.txt total files pushed: 15

what isn't the \n working in this case?

Replies are listed 'Best First'.
Re: adding carriage return to gmail script
by pryrt (Monsignor) on Jun 13, 2016 at 15:07 UTC

    Based on Email::Send::SMTP::Gmail, I infer that the module is sending in text/html rather than text/plain (that's probably a gmail default or something), so newlines alone won't be sufficient. Replace your send line with:

    $mail->send(-to=>"$to", -from=>"$from", -subject=>"$subject", -body=>j +oin "<br>\n", @push_list, "total files pushed: $filecount");

    I took @push_list out of the string, because it doesn't stringify the way you think it does: it doesn't automatically add \n after each element of the list just because you say "@push_list\n". I converted that to a join to make sure every element is separated by the newline. I also changed the joining from just "\n" to "<br>\n", so that there will be an HTML break between each entry, not just a newline (which HTML ignores). Finally, I included the total-count string as part of the join, to automate a line break between your list and the total at the end.

    TIMTOWTDI: instead of the the join, if you changed to local $" = "<br>\n";, then the stringification you used would do almost what you wanted... you'd just have to add a <br> before your final \n as well:

    local $" = "<br>\n"; $mail->send(-to=>"$to", -from=>"$from", -subject=>"$subject", -body=>" +@push_list<br>\ntotal files pushed: $filecount");

    edit: bumped the create button before finishing, so finished my example

    edit2: here's a link to $LIST_SEPARATOR for $".

      your first solution works adding the join, but adds the actual <br> into the email.
      1a.txt<br> 1b.txt<br> 1c.txt<br> 1d.txt<br> 1e.txt<br> 1f.txt<br> 1g.txt<br> 1h.txt<br> 1i.txt<br> 1j.txt<br> 1k.txt<br> 1l.txt<br> 1m.txt<br> 1n.txt<br> 1o.txt<br>
        Try adding content-type:
        $mail->send(-to .... -contenttype=>'text/html' );

                This is not an optical illusion, it just looks like one.

        Sorry. I've never used the module. When I didn't see the newline between the list of filenames and the "total files pushed", I assumed that HTML was swallowing the newline. When I skimmed the documentation, I saw the <BR> but didn't notice the -contenttype=>'text/html', so thought I had confirmation that it defaulted to HTML; oops.

        Now looking at the source of the OP, I now see that since the output was not wrapped in <c></c>, the post hid the newline that was actually there.

        To fix the example code, either use NetWallah's suggestion of -contenttype=>'text/html', or remove the unnecessary <br>'s from my example.

Re: adding carriage return to gmail script
by talexb (Chancellor) on Jun 13, 2016 at 15:01 UTC

    You are putting a "\n" after the array, so the script is doing what you asked. Perhaps instead you want to do something like join("\n",@push_list) . "\n total files .." instead.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

      No need for join. Use $" ($LIST_SEPARATOR).
      Bill

        Technically, that's correct. Realistically, that's a terrible solution -- putting newlines in between each of the elements in the list is a much clearer way to write this code. Keep in mind that your code should strive to be as clear as possible. That usually doesn't mean that it should be as clever as possible.

        Alex / talexb / Toronto

        Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: adding carriage return to gmail script
by Marshall (Canon) on Jun 13, 2016 at 14:59 UTC
    I don't use this mailer, but looking at your code, this line: my(@push_list) = glob "*"; , glob returns a list of file names without "\n".

    Consider:

    #!/usr/bin/perl use strict; use warnings; my (@push_list) = map{"$_\n"} glob "*"; print "@push_list\n";
    That would add a "\n" after each file name. Hope that helps, but I'm not sure. Run the code with and without the map.

    Update: see post++ by pryrt. I was puzzled by the fact that the single "\n" didn't appear to do anything, hence my uncertainty. The key appears to use <br> instead of "\n".

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2022-05-25 17:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (90 votes). Check out past polls.

    Notices?