1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PERL SMTP e-mail help

Discussion in 'Perl' started by gdsoccer11, Jul 2, 2008.

  1. gdsoccer11

    gdsoccer11 New Member

    Joined:
    Jul 2, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hey all,

    I've written a working smtp email script using the NET::SMTP module the only problem is that whenever I use a \n in the message body it fails to place it in the email. Here is the script:

    Code:
    use Data::Dumper;
    use Net::SMTP;
    use strict;
    
    my $to = 'to';
    my $from = 'your name';
    my $subject = "subject";
    my $greeting = "An error has occured";
    my $message = "Error Stats: blah blah blah /n
                              more blah blah";
    my $smtp = Net::SMTP->new('xxx.xxx.x.xxx');    
    $smtp->mail('me@myaddr.com');               
    my $status = $smtp->recipient($to);
    $smtp->data();
    $smtp->datasend("From: $from\n");
    $smtp->datasend("To: $to\n");
    $smtp->datasend("Subject: $subject\n");
    $smtp->datasend("Content-Type: text/html; charset=\"us-ascii\"\n");
    $smtp->datasend("\n");
    $smtp->datasend("$greeting\n\n");
    $smtp->datasend("\n");
    $smtp->datasend("$message");
    $smtp->datasend("\n");
    $smtp->dataend();
    $smtp->quit;
    now like I said that succesfull sends the e-mail if everything is filled in but none of my new lines (and I have like 6 of them) are showing up in the message body.

    Any thoughts?
     
  2. naveen

    naveen New Member

    Joined:
    Jun 2, 2005
    Messages:
    39
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Calcutta, India
    Home Page:
    I see that you've given content type to be text-html, in which case, the newline is defined by the <br> tag and not \n. For \n to be converted to a new line, you need the content type of the email to be set to text-plain.

    Try it out and see if it makes any difference. The rest of your script looks fine.

    HTH
    Naveen
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    or try this!

    Code:
    $message =~ s/\n/<br\/>/g;
    
    But Naveen's suggestion is the best!
     
  4. ballroot

    ballroot New Member

    Joined:
    Oct 14, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Engineer
    Location:
    Northern Ireland
    Thanks, works for me too.
     

Share This Page