Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Perl - sending multipart/alternative Email

  • 21-04-2006 04:30PM
    #1
    Registered Users, Registered Users 2 Posts: 2,934 ✭✭✭


    Hi
    I'm trying to send emails in plain text and html

    #!/usr/bin/perl

    $tmp="/tmp/mailtmp";
    $reciever="me\@computers.com";
    $from="me\@computers.com";
    $sendmail="/usr/sbin/sendmail";
    $boundary = "====" . time() . "====";


    open(EMAIL,">$tmp") || die "Could not open $tmp: $!\n";
    print EMAIL "From: $from\n";
    print EMAIL "To: $reciever\n";
    print EMAIL "Subject: Widget information\n";
    print EMAIL "MIME-Version: 1.0\n";
    print EMAIL "content-type: multipart/alternative\;";
    print EMAIL " boundary=$boundary";



    print EMAIL qq~

    $boundary = '--'.$boundary;
    $boundary
    Content-Type: text/plain; charset="iso-8859-1"

    Text email.

    $boundary
    Content-Type: text/html; charset="iso-8859-1"

    <HTML>
    <BODY>
    <p>
    HTML email.
    </p>
    </BODY>
    </HTML>

    $boundary--
    ~;
    close(EMAIL);

    system "cat $tmp | $sendmail $reciever";


    The problem is that $boundary in the header does not get set properly it does get set in the body- it is set by the server...

    any idea on what my problem is...

    Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 6,679 ✭✭✭daymobrew


    Randal Schwartz, coauthor of Programming Perl and Learning Perl, has a web site with the magazine articles he has written over the years.
    Using Google to search the articles I found a few that might be useful:
    Using MIME modules and Sending MIME Email
    Both of these articles use the standard MIME modules.


  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    I have code to send emails in text format but not in html format. I would love to know how to use PERL to send HTML mail:confused:


  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    By the way, I don't think there is any need for the \ in front of the @ sign as your not printing to the screen or anything.


  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    egan007 wrote:
    Hi
    I'm trying to send emails in plain text and html

    #!/usr/bin/perl

    $tmp="/tmp/mailtmp";
    $reciever="me\@computers.com";
    $from="me\@computers.com";
    $sendmail="/usr/sbin/sendmail";
    $boundary = "====" . time() . "====";


    open(EMAIL,">$tmp") || die "Could not open $tmp: $!\n";
    print EMAIL "From: $from\n";
    print EMAIL "To: $reciever\n";
    print EMAIL "Subject: Widget information\n";
    print EMAIL "MIME-Version: 1.0\n";
    print EMAIL "content-type: multipart/alternative\;";
    print EMAIL " boundary=$boundary";



    print EMAIL qq~

    $boundary = '--'.$boundary;
    $boundary
    Content-Type: text/plain; charset="iso-8859-1"

    Text email.

    $boundary
    Content-Type: text/html; charset="iso-8859-1"

    <HTML>
    <BODY>
    <p>
    HTML email.
    </p>
    </BODY>
    </HTML>

    $boundary--
    ~;
    close(EMAIL);

    system "cat $tmp | $sendmail $reciever";


    The problem is that $boundary in the header does not get set properly it does get set in the body- it is set by the server...

    any idea on what my problem is...

    Thanks

    There is similar code at http://alma.ch/perl/Mail-Sendmail-FAQ.html#HTML .

    Let me know if you get it sorted. Thanks.


  • Closed Accounts Posts: 304 ✭✭Zaltais


    Guys, guys, guys - CPAN is your friend.

    MIME::Lite is what you're looking for...
    #!/usr/bin/perl
    
    use warnings;
    use strict;
    
    use MIME::Lite;
    
    my $from_address = 'me@example.com';
    my $to_address = 'you@example.com';
    
    ### Adjust subject and body message
    my $subject = 'A multipart/alternative MIME message';
    
    ### Create the multipart container
    my $msg = MIME::Lite->new (
      From => $from_address,
      To => $to_address,
      Subject => $subject,
      Type =>'multipart/alternative'
    ) or die "Error creating multipart container: $!\n";
    
    ### Add the text message part
    $msg->attach (
      Type => 'TEXT',
      Data => q{
    Text part
    }) or die "Error adding the text message part: $!\n";
    
    $msg->attach (
      Type => 'text/html',
      Data => qq{
    <html>
      <body>
        <h1>test</h1>
        <p>HTML part</p>
      </body>
    </html>
    }) or die "Error adding the HTML message part: $!\n";
    
    $msg->send() or die $!;
    


  • Advertisement
  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    Zaltais wrote:
    Guys, guys, guys - CPAN is your friend.

    MIME::Lite is what you're looking for...
    #!/usr/bin/perl
    
    use warnings;
    use strict;
    
    use MIME::Lite;
    
    my $from_address = 'me@example.com';
    my $to_address = 'you@example.com';
    
    ### Adjust subject and body message
    my $subject = 'A multipart/alternative MIME message';
    
    ### Create the multipart container
    my $msg = MIME::Lite->new (
      From => $from_address,
      To => $to_address,
      Subject => $subject,
      Type =>'multipart/alternative'
    ) or die "Error creating multipart container: $!\n";
    
    ### Add the text message part
    $msg->attach (
      Type => 'TEXT',
      Data => q{
    Text part
    }) or die "Error adding the text message part: $!\n";
    
    $msg->attach (
      Type => 'text/html',
      Data => qq{
    <html>
      <body>
        <h1>test</h1>
        <p>HTML part</p>
      </body>
    </html>
    }) or die "Error adding the HTML message part: $!\n";
    
    $msg->send() or die $!;
    

    Perfect, 100%. Great stuff. Your a class act Zaltais:)


Advertisement