change script to send html email

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pelusa
    New Member
    • Nov 2007
    • 9

    change script to send html email

    hello all,

    i need help modifying a small perl script that sends an email after a form is submitted.
    i need to be able to receive the email in html format, but my knwoledge of perl is very poor.

    please any help at this moment would be fantastic.

    thank you.

    here is my script:
    Code:
    l#!/usr/bin/perl -w
     
    use Net::SMTP;
    use CGI;
     
    my $cgi = new CGI;
     
    sub send_mail 
     
    {
    	my $to = $_[0];
    	my $subject = $_[1];
    	my $body = $_[2];
     	my $from = $_[3];
     
    	my $smtp;
     
    if (not $smtp = Net::SMTP->new('mail.server.com', Port => 25, Debug => 1)) 
    {
    	die "Could not connect to server\n";
    }
     
    $smtp->mail($from . "\n");
    
    my @recepients = split(/,/, $to);
    
    foreach my $recp (@recepients) 
    {
    	$smtp->to($recp . "\n");
    }
    
    $smtp->data();
    $smtp->datasend("From: " . $from . "\n");
    $smtp->datasend("To: " . $to . "\n");
    $smtp->datasend("Subject: " . $subject . "\n");
    $smtp->datasend("\n");
    
    $smtp->datasend($body . "\n");
    $smtp->dataend();
    $smtp->quit;
    }
    # Send away!
    &send_mail($cgi->param('to'), $cgi->param('subject'), $cgi->param('body'), $cgi->param('from'));
    
    print $cgi->header;
    print '<html><body>Your e-mail has been sent</body></html>';
    Last edited by numberwhun; Jun 17 '08, 09:05 PM. Reason: Please use code tags!
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    You can use MIME::Lite to do this
    See this sample code
    [code=perl]
    use strict;
    use MIME::Lite;

    # SendTo email id
    my $to_id = 'yourid@mail.co m';

    # create a new MIME Lite based email
    my $msg = MIME::Lite->new
    (
    Subject => "HTML Mail",
    From => 'yyy@zzz.com',
    To => $to_id,
    Type => 'text/html',
    Data => '<H1>Hello</H1><br>This is sample html mail.<br>'
    );

    $msg->send();

    [/code]

    Raghu

    Comment

    Working...