Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Perl - sending multipart/alternative Email

Options
  • 21-04-2006 4:30pm
    #1
    Registered Users 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 Posts: 6,508 ✭✭✭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