Email Attachment Question: SMTP

Discussion in 'Perl Misc' started by amerar@iwc.net, Jan 4, 2006.

  1. Guest

    Hi All,

    I have a Perl script that uses NET::SMTP to send out emails. I've
    attached a snippet of my code below.

    I want to also attach a file to this. I see lots of posts on using
    MIME::LITE, however, I am not understanding how it will work with
    NET::SMTP.

    Can anyone help, maybe provide some examples?

    Thanks in advance, and here is an example of my code:

    $smtp = Net::SMTP->new("nitelife");
    $smtp->mail($from);
    $smtp->to($to);
    $smtp->data();
    $smtp->datasend("From: $from1\n");
    $smtp->datasend("To: $to1\n");
    $smtp->datasend("Subject: $subject\n");
    $smtp->datasend("Mime-Version: 1.0\n");
    $smtp->datasend("Content-Type: text/html; charset=us-ascii\n");
    $smtp->datasend("Content-Disposition: inline\n");
    $smtp->datasend("Content-Transfer-Encoding: 7bit\n");
    $smtp->datasend("\n");
    $smtp->datasend("<br><br>\n\n");
    $smtp->datasend("Text goes here......\n");
    $smtp->datasend("Text goes here......\n");
    ..
    ..
    $smtp->dataend();
    $smtp->quit;

    Arthur
     
    , Jan 4, 2006
    #1
    1. Advertising

  2. Re: Email Attachment Question: SMTP

    wrote:
    > Hi All,
    >
    > I have a Perl script that uses NET::SMTP to send out emails. I've
    > attached a snippet of my code below.
    >
    > I want to also attach a file to this. I see lots of posts on using
    > MIME::LITE, however, I am not understanding how it will work with
    > NET::SMTP.


    What is it that you how to achive by explicily using Net::SMTP and
    MIME::Lite that you couldn't do just by using MIME::Lite's send()
    method to use Net::SMTP (as per the documentation)?
     
    Brian McCauley, Jan 4, 2006
    #2
    1. Advertising

  3. Tim Heaney Guest

    writes:
    >
    > I have a Perl script that uses NET::SMTP to send out emails. I've
    > attached a snippet of my code below.
    >
    > I want to also attach a file to this. I see lots of posts on using
    > MIME::LITE, however, I am not understanding how it will work with
    > NET::SMTP.


    Don't use Net::SMTP yourself directly; just let MIME::Lite use it.

    use MIME::Lite;

    # Create a new multipart message.
    $msg = MIME::Lite->new(To => $to1,
    Subject => $subject,
    Type =>'multipart/mixed',
    );

    # Attach the body.
    $msg->attach(Type => 'TEXT',
    Data => "Text goes here......",
    );

    # Attach a file.
    $msg->attach(Type => 'image/png',
    Path => 'granite.png',
    );

    # Tell MIME::Lite to use Net::SMTP when sending mail, instead of
    # its default method.
    MIME::Lite->send('smtp', "nitelife");

    # Now the behavior of the send method has been altered; it will
    # send with Net::SMTP. You don't have to do anything else.
    $msg->send;

    I hope this helps,

    Tim
     
    Tim Heaney, Jan 4, 2006
    #3
  4. Guest

    Re: Email Attachment Question: SMTP

    Brain,

    What I am unclear on is this.....how do I put multiple lines of text
    into the body or DATA section?

    My current program uses a FOR loop like this:

    foreach $line (@html_letter) {
    $smtp->datasend("$line\n");
    }

    Looking at the documentation from MIME::LITE, it shows this:

    # Attach the body.
    $msg->attach(Type => 'TEXT',
    Data => "Text goes here......",
    );

    How do I place my lines of text so that they format correctly, etc?
    The script has several places where it adds lines to the email message
    depending on certain conditions and such.........

    I hope I am being somewhat clear in my questions.

    Arthur
     
    , Jan 4, 2006
    #4
  5. Guest

    , Jan 4, 2006
    #5
  6. Re: Email Attachment Question: SMTP

    wrote:
    > Brain,
    >
    > What I am unclear on is this.....how do I put multiple lines of text
    > into the body or DATA section?
    >
    > My current program uses a FOR loop like this:
    >
    > foreach $line (@html_letter) {
    > $smtp->datasend("$line\n");
    > }
    >
    > Looking at the documentation from MIME::LITE, it shows this:
    >
    > # Attach the body.
    > $msg->attach(Type => 'TEXT',
    > Data => "Text goes here......",
    > );
    >
    > How do I place my lines of text so that they format correctly, etc?


    >From reading the docs (untested)...


    $msg->attach(Type => 'text/html',
    Data => join("\n",@html_letter),
    );

    If you can arrange that the data in @html_letter already has (or does
    not need) newlines added then...


    $msg->attach(Type => 'text/html',
    Data => \@html_letter,
    );
     
    Brian McCauley, Jan 4, 2006
    #6
  7. Paul Lalli Guest

    Re: Email Attachment Question: SMTP

    wrote:
    > May I also show you this example........I'm curious of your opinions:


    Who is "you"? Why are you showing us this example? Please quote some
    context when posting a reply to Usenet.

    > http://groups.google.com/group/comp...te attachment&rnum=43&hl=en#a067372c6df6d935


    That posting is more than 7 years old. MIME::Lite has involved a lot
    since then. Most specifically in this case, about a month after this
    posting you referenced, MIME::Lite was changed to automatically use
    Net::SMTP rather than relying on `sendmail`. See
    http://search.cpan.org/~yves/MIME-Lite-3.01/changes.pod

    Read the documentation for the module you want to use:
    http://search.cpan.org/~yves/MIME-Lite-3.01/lib/MIME/Lite.pm

    Paul Lalli
     
    Paul Lalli, Jan 4, 2006
    #7
  8. Guest

    Re: Email Attachment Question: SMTP

    wrote:
    > Hi All,
    >
    > I have a Perl script that uses NET::SMTP to send out emails. I've
    > attached a snippet of my code below.

    <snipped snippet>

    You are going to a lot of trouble there, my friend. Consider
    Mail::Sender instead:

    #!/usr/bin/perl
    use strict; use warnings;

    use Mail::Sender;
    (new Mail::Sender)->MailFile({
    'smtp' => 'mail.mySMTPserver.com',
    'from' => '',
    'to' => 'nobody@no_such_domain.com',
    'subject' => 'Something for you',
    'msg' => 'Here is a file for you.',
    'file' => '/path/to/some/file.mp3'
    }) || die "Cannot send message: $!\n";

    __END__

    --
    http://DavidFilmer.com
     
    , Jan 4, 2006
    #8
  9. Guest

    Re: Email Attachment Question: SMTP

    David,

    What would the MAIL::SENDER look like if I was sending an HTML message,
    with inline images, AND an attachment? Do I not have to show the type
    of attachment it is, like text/plain or image/gif?

    Arthur
     
    , Jan 4, 2006
    #9
  10. Dr.Ruud Guest

    Re: Email Attachment Question: SMTP

    schreef:

    > MAIL::SENDER


    YM: Mail::Sender

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jan 4, 2006
    #10
  11. Guest

    Re: Email Attachment Question: SMTP

    Also, a good thing about NET::SMTP is that I can use 2 different mail
    hosts : $smtp = Net::SMTP->new("$mailhost");

    Can I do that with MAIL::SENDER??
     
    , Jan 4, 2006
    #11
  12. Guest

    , Jan 4, 2006
    #12
  13. Re: Email Attachment Question: SMTP

    wrote:
    > Consider Mail::Sender instead:
    >
    > #!/usr/bin/perl
    > use strict; use warnings;
    >
    > use Mail::Sender;
    > (new Mail::Sender)->MailFile({
    > 'smtp' => 'mail.mySMTPserver.com',
    > 'from' => '',
    > 'to' => 'nobody@no_such_domain.com',
    > 'subject' => 'Something for you',
    > 'msg' => 'Here is a file for you.',
    > 'file' => '/path/to/some/file.mp3'
    > }) || die "Cannot send message: $!\n";


    I like Mail::Sender, too, but the above error check is not correct. Try
    this instead:

    ref ( new Mail::Sender->MailFile({
    'smtp' => 'mail.mySMTPserver.com',
    'from' => '',
    'to' => 'nobody@no_such_domain.com',
    'subject' => 'Something for you',
    'msg' => 'Here is a file for you.',
    'file' => '/path/to/some/file.mp3'
    }) ) || die "Cannot send message: $Mail::Sender::Error\n";

    Think it's described in the docs, btw. ;-)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 4, 2006
    #13
  14. Guest

    Re: Email Attachment Question: SMTP

    wrote:
    > Also, a good thing about NET::SMTP is that I can use 2 different mail hosts
    >
    > Can I do that with MAIL::SENDER??


    It isn't documented, but I tried this:

    my @smtp_servers = qw/mail.totally_bogus_server.com
    mail.myserver.com/;

    and then passed it (by reference) as a hash value:

    'smtp' => \@smtp_servers,

    and it skipped the bogus server and went to my real mailserver.

    So apparently it does work...

    --
    http://DavidFilmer.com
     
    , Jan 4, 2006
    #14
  15. Guest

    Re: Email Attachment Question: SMTP

    wrote:
    > It isn't documented, but I tried this:
    >
    > my @smtp_servers = qw/mail.bogus_server.com mail.myserver.com/;
    >
    > and then passed it (by reference) as a hash value:
    >
    > 'smtp' => \@smtp_servers,
    >
    > and it skipped the bogus server and went to my real mailserver.
    >
    > So apparently it does work...


    I'd like to 'retract' this post; I, ummm, didn't, ummm, actually save
    the script once I made the testing change that I described... D'OH!

    When I actually SAVED the change, it failed. So, AFAIK, Mail::Mailer
    won't allow you to specifiy more than one SMTP server for failover, as
    the Net module does.

    Sorry for the incorrect info...

    --
    http://DavidFilmer.com
     
    , Jan 5, 2006
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    1,092
    Roedy Green
    Jan 24, 2006
  2. Alex Hunsley
    Replies:
    4
    Views:
    3,489
    Tim Williams (gmail)
    Jun 29, 2004
  3. Eric E

    Attachment Size and SMTP EMail

    Eric E, Sep 23, 2008, in forum: Python
    Replies:
    1
    Views:
    349
    Eric E
    Sep 23, 2008
  4. ecureuil
    Replies:
    0
    Views:
    328
    ecureuil
    May 28, 2006
  5. ashish makani
    Replies:
    5
    Views:
    611
    Mark Lawrence
    Sep 20, 2012
Loading...

Share This Page