[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 386
  • Last Modified:

how to send webpage as email in cgi?

hi,
is there any way through which i can send a webpage as email through a mail template using cgi mail script?
i want send mails automatically to the users through a script?
0
jiniasp
Asked:
jiniasp
1 Solution
 
petekmitCommented:
Not sure I understand the question as asked, but I can try to help.

I'm going to guess that, by what you wrote, you want to:

Have a script that, when a web user visits, emails them or someone else an email which has HTML code in it.  


I will answer in terms of PERL, the language I am most familiar with for completing this task.

There are multiple ways of doing this, and the one best for you depends on the configuration of the web server.  If your server is a linux box, and you are able to access it, you can use the mail program on the box to send your message.  You can code it in perl as such:

#!/usr/bin/perl

use CGI qw(param)
##Etc.  whatever you use to make your life easier

my $emailAddress = param("email");
##Get the user's email address from a form

my $email = "  <html>...</html>  ";

system "echo \"${email}\" | mail <${emailAddress}>";

print <<RESPONSE
Content-type: text/html


<html>
....</html>
RESPONSE
;

##Where above page is simply the page they see after running the script

-------------------------------

I hope this helps.  If not, if you can clarify what you're trying to get, perhaps I can help further.
0
 
TintinCommented:
What sort of webpage do you want to send?  A webpage on your website, or one that you dynamically generate?
0
 
revantineCommented:
This is how I invoice my customer using html in email. HTML Template is a VERY easy module to use, and will let you ue an editor to create a template and then almost drop it as-is. There is a very good section in the O'Reilly CGI Programming with Perl book, but I am sure there are easy to use stuff on the web too. I included a note inline if you are using static html or want a different way (and I tagged the Template part).

If you have a question I will try to answer it.

use Net::SMTP;
use HTML::Template;

my $template="/scripts/htmltemplate.tmpl";
# Who did the email come from
my $from = 'who@where.com';
# site for HELO
my $site = 'server.net';
# The mail server
my $smtp_host = 'mail.server.net';
# Blind copy me
my $bcc = 'revantine@server.com';


   $smtp = Net::SMTP->new($smtp_host, Hello => $site);
   # start email
   $smtp->mail($from);
   $smtp->to($to);
   $smtp->bcc($bcc);
   # to ( ADDRESS [, ADDRESS [...]] )
   # cc ( ADDRESS [, ADDRESS [...]] )
   # bcc ( ADDRESS [, ADDRESS [...]] )
   $smtp->data();

   $smtp->datasend("From: $from\n");
   $smtp->datasend("To: $to\n");
# build a subject string
   $SUBJECT="invoice ";
   $smtp->datasend("Subject: ".$SUBJECT."\n");
   $smtp->datasend("MIME-Version: 1.0\n");
   $smtp->datasend("Content-Type: text/html; charset=us-ascii\n");
   $smtp->datasend("\n");

# start HTML::Template part
      $tmpl->param( User_msg => $User_msg );

   $tmpl=new HTML::Template( filename => $template );

   $tmpl->param( invdata => [\%formhash] );
   $smtp->datasend($tmpl->output);
# end HTML Template part
# you could just:
$smtp->datasend("<html><body>You can put html here</body></html>");

   # dataend
   $smtp->dataend();    # NOT datasend!
   $smtp->quit;




#####################
# /scripts/htmltemplate.tmpl
<html>
<body>
 Some tamplate text with a variable: <TMPL_VAR NAME="User_msg">
</body>
</html>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
revantineCommented:
Oops. There is a $to variable missing. Just add it at the top where all the assignment is going on. I use the routine it in a lookup against a database query, so the $to is part of a loop.

my $to='you@yourdomain.com';
0
 
jiniaspAuthor Commented:
i am using a free tripod server , below is the module which i can use to send mail.
it also contains the mail template & cgi example which i use to send mail. it works well for text mail. what changes should be done in the mail template so that i can send html page which includes graphics & background? or how can i send a web page on my web site.
***********************
package TripodMail;

################
# TripodMail.pm #
#############################################################################
# TripodMail is a module that allows you to send out email messages with    
# your scripts.  In order to use it you'll have to have a mail template in  
# your cgi-bin directory.  The mail template will need to look something    
# like this:

# To: $email
# From: FredFlintstone@hotmail.com
# Subject: YabbaDabbaDoo!
#
# Hello $name,
# Congratulations! You're user number $number of this mail script!

# You can add other email headers (Reply-To:, Errors-To:, etc), but To:
# and From: are manditory.  You can customize your email by adding variables
# wherever you would like to fill something in on the fly.
# The sendMail method requires 2 parameters- the location of the mail
# template file, and a reference to a hash of variables.
# The keys of the varaible hash should correlate with the variables in the
# mail template.

# Example of use:
# require TripodMail;
# $MAIL = new TripodMail;
# $mail_template = "./flintstones_mail.txt";
# %variables = ('email'  => 'Wilma@gurlmail.com',
#               'name'   => 'Wilma',
#             'number' => '2');
# $MAIL->sendMail($mail_template, \%variables);

###########################################################################

sub new {
    my $class = shift;
    my $self = {};
    bless $self, $class;
    return $self;
}

sub sendMail {
    # example: $MAIL->sendMail($template_file, \%hash_o_variables)
    # requires: 1) name of a file that is a template for the mail
    #           2) a reference to hash of variables to fill out the template
    # does: writes a mail file to member's directory to be mailed later
    #       by sendmail
    # returns: 1 on success, 0 on failure
   
    my ($self, $template_file, $hash_ref) = @_;
    my ($message, $key, $time, $file);

    # error checking
    if ((! $template_file)||(! $hash_ref)){
      print STDERR "usage: sendMail(template_file, hash_reference)\n";
      return 0;
    }
    if (! -s $template_file){
      print STDERR "file does not exist or has a 0 size!\n";
      return 0;
    }
   
    # read in template
    open (MESSAGE, "<$template_file") or
      die "can't open template file $template for reading\n";
   
    undef $/;
    $message = <MESSAGE>;
    close (MESSAGE);
    $/ = "\n";

    # variable substitution
    foreach $key (keys (%{$hash_ref})){
      $message =~ s/\$$key/$hash_ref->{$key}/eg;
    }
   
    # check final template format
    if ($message !~ /to:.*\w+@\w+\.\w+/i){
      print STDERR "To: field missing or invalid recipient\n";
      return 0;
    }
    if ($message !~ /from:.*\w+@\w+\.\w+/i){
      print STDERR "From: field missing or invalid sender\n";
        return 0;
    }

    # write template to file
    $time = time();
    $file = 'mail.'.$time;

    open (FILE, ">$file") or
      die "can't open file: $file for writing\n";
    print FILE $message;
    close (FILE);

    return 1;
}

1;
****************
0
 
jiniaspAuthor Commented:
i tried to change the template, but only certain tags work like header,anchor tag..
but bgcolor or background... doesn't work.I'm new to cgi so please help.
Below is the changed mail template:
******
To: $email
From: FredFlintstone@hotmail.com
Subject: YabbaDabbaDoo!
Mime-Version: 1.0;
Content-Type: text/html; charset="ISO-8859-1";
Content-Transfer-Encoding: 7bit;

<html>
<body bgcolor="olive" BACKGROUND="bg1.jpg" text="white" >
Hello $name,
Congratulations! You're user number $number of this mail script!

<a href="http://tripod.com"><b>TRIPOD</b></a>
</body>
</html>
**********
Thanks everyone for ur help!
0
 
revantineCommented:
I will print out your listing and take a look at it. Most email clients strip the <head> and <body> tags, but I know you can place backgrounds so I will check in to it.

I generally use links back to a web server for images. Mostly for the smallest email size, and ease of creation.
0
 
revantineCommented:
Quoted from http://webdesign.about.com/od/intermediatetutorials/a/aa050800b.htm

"Tricks

    * You can use the <base href=""> tag in the <head> of your HTML email, but not all email clients support that tag
    * Background images (in the <body> tag) only work in some email clients
    * The bgcolor attribute works best if you put it in a table, and enclose your entire email in a single celled table
    * Most email clients that support HTML email, only support a limited version of HTML, it's best to avoid more complex things like frames in your email"

You can create a table and set the background image and color for the table.
0
 
jiniaspAuthor Commented:
thanks revantine ,that worked.
can i send attachment through mail template?
0
 
revantineCommented:
I have pondered that question myself. I am sure it is possible, but I have not invested the 10 minutes it would take to find the answer.

That said, I think it is a good time to find out :) I will hunt down an answer for us both and get back sometime today I imagine.
0
 
revantineCommented:
Hopefully I can make this as painless as possible. The answer is a combination of 3 or 4 pieces of information to cover the broadest range of possibilities while maintaining (hopefully) an easy to impliment method.

Bibliography (where I got the info):
http://forums.devshed.com/showthread.php?t=184255 *
http://perl.about.com/library/weekly/aa042302a.htm
http://cpan.uwinnipeg.ca/htdocs/MIME-Lite/MIME/Lite.html  **
and O'Reilly's Perl Cookbook 2nd Ed.

* indicates primary code source
** indicates best probable source for additional information

To encode, you use the MIME::Lite module. It includes some modification of the way the message is built. You can send to multiple addresses like this: $to_address='who@where.com, me@there.net' and it should still work with smtp correctly. I have not tested that statement, but MIME::Lite is built on Net::SMTP (from what I read) and the syntax appears consistent enough.

#! /usr/bin/perl

use MIME::Lite;
use Net::SMTP;

my $from_address = 'xxxxxxx';
my $to_address = 'xxxxxxx';
my $subject = 'MIME Net::SMTP test';
my $mime_type = 'TEXT';
my $message = "Test Perl email with attachment\n";

# Create the initial text of the message
# vvv   you can add these if you want   vvv
#  Cc => $cc_address,
#  Bcc => $bcc_address,
my $mime_msg = MIME::Lite->new(
  From => $from_address,
  To => $to_address,
  Subject => $subject,
  Type => $mime_type,
  Data => $message
  )
or die "Error creating MIME body: $!\n";

my $filename = 'girl.zip';
my $recommended_filename = 'girl.zip';

# Attach the test file
$mime_msg->attach(
  Type => 'application/zip',
  Path => $filename,
  Filename => $recommended_filename
  )
or die "Error attaching test file: $!\n";

my $message_body = $mime_msg->body_as_string();

# Set this variable to your smtp server name
my $ServerName = "xxxxxx";

# Create a new SMTP object
$smtp = Net::SMTP->new($ServerName) or
  die "Couldn't connect to server";

$smtp->auth( $from_address, 'xxxxxx' );
$smtp->mail( $from_address );
$smtp->to( $to_address );

$smtp->data();
$smtp->datasend($message_body);
$smtp->dataend();

# Close the connection
$smtp->quit();

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now