469,315 Members | 1,795 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

How to embed a hyperlink behind a string

Hi,

I'm new with perl. I'm writting a perl script to read a text file to email the contract managers to warn them there are some contracts with High risks waiting for them to view.
The text file includes "Email_address", "Manager_Name","url_link",etc.
The result format is a list of contract numbers in the email body. e.g.

CONTRACT NO
020221021
970040006

I'd like to know how to code in perl to give a hyperlink to each of the "Contract No" listed above so that the user can just click on it and take them directly to view the actual contract image.

Currently I can get it work with email body looks like below:

CONTRACT NO
020221021 http://filenet/idmws/DocView.asp?id=74656190&Library=DefaultIMS:UNION_M P:FileNet

970040006 http://filenet/idmws/DocView.asp?id=66304113&Library=DefaultIMS:UNION_M P:FileNet

Could someone show me how to embed the http link behind the "Contract No" instead of being explicitly displayed, so that I can just click on the "Contract No" hyperlink and view the actual contract image?

Any help would be greatly appreciated...!!!


Gina
Nov 6 '07 #1
19 4940
eWish
971 Expert 512MB
Welcome to TSDN ginahong!!

In order for it be sent as html you have to have the proper headers. What method / module are you using to send the email? Also, please post what you have tried so we can help.

--Kevin
Nov 6 '07 #2
Hi Kevin,

Thank you for your prompt reply! Below is the source code that I have tried:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use ExtUtils::Installed;
  3. use strict;
  4.  
  5. my $inst    = ExtUtils::Installed->new();
  6. my @modules = $inst->modules();
  7. my $version = $inst->version(@modules);
  8.  
  9. my $email_from="FileNET SysAdmin <Stip-FileNet-Support\@uboc.com>";
  10. my $mailcc=' ';
  11. my $subject="High Risk Contracts";
  12. my $input_file;
  13. my $email_address;
  14. my $account_id;
  15. my $vendor_name;
  16. my $docid;
  17. my $message; 
  18. my $vrm_name;
  19. my $prev_vrm;
  20. my $prev_acctid;
  21. my $prev_vendor;
  22. my $prev_email;
  23. my $prev_docid;
  24. my $httplink;
  25.  
  26.  
  27.  
  28. #Function to page or mail.
  29. sub send_mail{
  30.   my ($email_from, $whotomail, $mailcc, $subject, $message) = @_;
  31.   open(SENDMAIL, "|/usr/lib/sendmail -oi -t")
  32.                         or die "Can't fork for sendmail: $!\n";
  33.  
  34.  
  35.  
  36. print SENDMAIL <<"EOF";
  37. From: $email_from
  38. To: $whotomail
  39. cc: $mailcc
  40. Subject: $subject
  41. $message
  42. EOF
  43. close(SENDMAIL)     or warn "sendmail didn't close nicely";
  44. return 0;
  45. }
  46.  
  47. sub main {
  48.   print join " ", @modules,$version; print "\n";
  49.   $input_file = "send-vrm-confirms-input.txt";
  50.   $prev_acctid = "";       
  51.   $prev_vendor = "";
  52.   $prev_docid = "";
  53.   open(FILE, "< $input_file") or "Can't open $input_file: $!\n";   
  54.   while( <FILE> ) {
  55.    ($email_address, $vrm_name,$httplink,$docid,$account_id,$vendor_name) = split('#', $_);   
  56.    if (($prev_acctid eq "") or ($vrm_name eq $prev_vrm))
  57.    {     
  58.      $prev_email = $email_address;          
  59.      $prev_vrm = $vrm_name;     
  60.      $prev_docid = $prev_docid.$account_id."   ".$httplink."\n";   
  61.      $prev_acctid = $prev_acctid.$account_id."               ".$vendor_name;                
  62.  
  63.    }   
  64.    elsif (($vrm_name ne $prev_vrm) and ($prev_acctid ne '%%')){            
  65.  
  66.    $message = "Dear ".$prev_vrm.": You have the following contracts with High Risks.\nPlease login to the website link below to view the contract(s).\n\nCONTRACT NO:\n".$prev_docid;                        
  67.     send_mail("$email_from", "$prev_email", "$mailcc", "$subject", "$message");    
  68.     $prev_acctid = "";       
  69.     $prev_vendor = "";
  70.     $prev_docid = "";    
  71.     $prev_email = $email_address;     
  72.     $prev_vrm = $vrm_name;    
  73.     $prev_docid = $prev_docid.$account_id."   ".$httplink."\n";   
  74.     $prev_acctid = $prev_acctid.$account_id."               ".$vendor_name;         
  75.  
  76.    }    
  77.   }; 
  78.  
  79.   if ($prev_acctid eq '%%')
  80.   {  
  81.   $message = "Dear ".$prev_vrm.": You have the following contracts with High Risks.\nPlease login to the website link below to view the contract(s).\n\nCONTRACT NO:\n".$prev_docid;                      
  82.   send_mail("$email_from", "$prev_email", "$mailcc", "$subject", "$message");  
  83.   }
  84.   close(FILE);
  85. }
  86.  
  87. # main program
  88. main ();
  89.  
Nov 6 '07 #3
eWish
971 Expert 512MB
Add:
Expand|Select|Wrap|Line Numbers
  1. "Content-type:text/html; charset=iso-8859-1\n";
After:
Expand|Select|Wrap|Line Numbers
  1. print SENDMAIL <<"EOF";
Then in the body of the email you can use standard HTML markup for the link.
Expand|Select|Wrap|Line Numbers
  1. <a href="http://someplace.com">Contract No</a>
I prefer modules for sending mail. It would be easier I think. Here is a sample using Mime::Lite.

Expand|Select|Wrap|Line Numbers
  1. my $message_type = 'text/html';
  2.  
  3.     my $msg = MIME::Lite->new(
  4.         From    => $from_email,
  5.         To        => $to_email,                
  6.         Subject => $subject,
  7.         Type    => $message_type,
  8.         Data    => $message
  9.     );
  10.  
  11.     $msg->send() || die "Couldn't send message:";
Nov 6 '07 #4
I added the two lines as you suggested (see below). However, when I run it, I got "No recipient addresses found in header".

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use ExtUtils::Installed;
  4. use strict;
  5.  
  6. my $inst    = ExtUtils::Installed->new();
  7. my @modules = $inst->modules();
  8. my $version = $inst->version(@modules);
  9.  
  10. my $email_from="FileNET SysAdmin <Stip-FileNet-Support\@uboc.com>";
  11. my $mailcc=' ';
  12. my $subject="High Risk Contracts";
  13. my $input_file;
  14. my $email_address;
  15. my $account_id;
  16. my $vendor_name;
  17. my $docid;
  18. my $message; 
  19. my $vrm_name;
  20. my $prev_vrm;
  21. my $prev_acctid;
  22. my $prev_vendor;
  23. my $prev_email;
  24. my $prev_docid;
  25. my $httplink;
  26.  
  27.  
  28.  
  29. #Function to page or mail.
  30. sub send_mail{
  31.   my ($email_from, $whotomail, $mailcc, $subject, $message) = @_;
  32.   open(SENDMAIL, "|/usr/lib/sendmail -oi -t")
  33.                         or die "Can't fork for sendmail: $!\n";
  34.  
  35.  
  36.  
  37. print SENDMAIL <<"EOF";
  38. "Content-type:text/html; charset=iso-8859-1\n"
  39. From: $email_from
  40. To: $whotomail
  41. cc: $mailcc
  42. Subject: $subject
  43.  
  44. $message
  45.  
  46. EOF
  47.  
  48.  
  49. close(SENDMAIL)     or warn "sendmail didn't close nicely";
  50. return 0;
  51. }
  52.  
  53. sub main {
  54.   print join " ", @modules,$version; print "\n";
  55.   $input_file = "send-vrm-confirms-input.txt";
  56.   $prev_acctid = "";       
  57.   $prev_vendor = "";
  58.   $prev_docid = "";
  59.   open(FILE, "< $input_file") or "Can't open $input_file: $!\n";   
  60.   while( <FILE> ) {
  61.    ($email_address, $vrm_name,$httplink,$docid,$account_id,$vendor_name) = split('#', $_);   
  62.    if (($prev_acctid eq "") or ($vrm_name eq $prev_vrm))
  63.    {     
  64.      $prev_email = $email_address;          
  65.      $prev_vrm = $vrm_name;     
  66.      #$prev_docid = $prev_docid.$account_id."   ".$httplink."\n";   
  67.      $prev_docid = $prev_docid.$account_id."   "."<a href=$httplink>$account_id</a>"."\n";     
  68.      $prev_acctid = $prev_acctid.$account_id."               ".$vendor_name;                
  69.  
  70.    }   
  71.    elsif (($vrm_name ne $prev_vrm) and ($prev_acctid ne '%%')){            
  72.  
  73.     $message = "Dear ".$prev_vrm.": You have the following contracts with High Risks.\nPlease login to the website link below to view the contract(s).\n\nCONTRACT NO:\n".$prev_docid;                        
  74.     send_mail("$email_from", "$prev_email", "$mailcc", "$subject", "$message");    
  75.     $prev_acctid = "";       
  76.     $prev_vendor = "";
  77.     $prev_docid = "";    
  78.     $prev_email = $email_address;     
  79.     $prev_vrm = $vrm_name;    
  80.     #$prev_docid = $prev_docid.$account_id."   ".$httplink."\n";   
  81.     $prev_docid = $prev_docid.$account_id."   "."<a href=$httplink>$account_id</a>"."\n";     
  82.     $prev_acctid = $prev_acctid.$account_id."               ".$vendor_name;         
  83.  
  84.    }    
  85.   }; 
  86.  
  87.   if ($prev_acctid eq '%%')
  88.   {  
  89.   $message = "Dear ".$prev_vrm.": You have the following contracts with High Risks.\nPlease login to the website link below to view the contract(s).\n\nCONTRACT NO:\n".$prev_docid;                      
  90.   send_mail("$email_from", "$prev_email", "$mailcc", "$subject", "$message");  
  91.   }
  92.   close(FILE);
  93. }
  94.  
  95. # main program
  96. main ();
  97.  
Nov 6 '07 #5
eWish
971 Expert 512MB
You have posted code into the forum without using proper code tags. It is best practice here on TSDN to wrap all code posted into the forum in code tags.

Code tags start with [code] and end with [/code]. If you need an example other than this one, the please refer to the REPLY GUIDELINES (or POSTING GUIDELINES if you are starting a discussion) to the right of the Message window. You will find the examples and suggestions for posting there..

Please know that I have fixed your posts above to include the proper code tags. Please be sure and use code tags in all of your future posts here on TSDN.

- Moderator
Nov 6 '07 #6
eWish
971 Expert 512MB
Check and see what is in $whotomail. Basically, you are not telling it who to send to. Therfore, $whotomail must be empty. As a test print the values of the following before you send the email.
Expand|Select|Wrap|Line Numbers
  1. print 'From:' $email_from,"\n";
  2. print 'Send To:', $whotomail, "\n";
  3. print 'CC:', $mailcc,"\n";
  4. print 'Subject:',  $subject, "\n";
  5. print 'Message:', $message, "\n";
Then once you have it worked send the email and remove the print statements.

Also, in multilines like the message you can use the following to make it cleaner and easier to read.
Expand|Select|Wrap|Line Numbers
  1. $message = qq~
  2. Dear $prev_vrm: 
  3.  
  4. You have the following contracts with High Risks.
  5. Please login to the website link below to view the contract(s). 
  6. CONTRACT NO: $prev_docid 
  7. ~;
The qq also interpolates - perlop Just like double quoting.
Nov 6 '07 #7
$whotomail = $email_address = $prev_email = "John.Young@uboc.com",which is hard coded for testing purpose. Before I added the code you suggested below. I could send email. but after adding that line, I started getting the error msg: "No recipient addresses found in header".

Add:
Expand|Select|Wrap|Line Numbers
  1. "Content-type:text/html; charset=iso-8859-1\n";
After:
Expand|Select|Wrap|Line Numbers
  1. print SENDMAIL <<"EOF";
Also, I still can not get the http link hidden (not explicitly displayed in the email) after adding the code you suggested below. All I want is the $account_id is a blue highlighted link (hot spot) so that user can click on it and is directed to view the actual page. Could you possibly add the lines you suggested in my original code, just to see the difference between yours and mine. Thank you!

Expand|Select|Wrap|Line Numbers
  1. $prev_docid = $prev_docid.$account_id."   "."<a href=$httplink>$account_id</a>"."\n";
Nov 7 '07 #8
KevinADC
4,059 Expert 2GB
This is bad syntax:

Expand|Select|Wrap|Line Numbers
  1. print SENDMAIL <<"EOF";
  2. "Content-type:text/html; charset=iso-8859-1\n"
  3. From: $email_from
  4. To: $whotomail
  5. cc: $mailcc
  6. Subject: $subject
  7.  
  8. $message
  9.  
  10. EOF
should be:


Expand|Select|Wrap|Line Numbers
  1. print SENDMAIL <<"EOF";
  2. Content-type:text/html; charset=iso-8859-1
  3.  
  4. From: $email_from
  5. To: $whotomail
  6. cc: $mailcc
  7. Subject: $subject
  8.  
  9. $message
  10.  
  11. EOF
although I am really not sure about that header:

Content-type:text/html; charset=iso-8859-1
Nov 7 '07 #9
Dear Kevin,

You are AWESOME...!!! Thank you so much for your help. This is definitely a great site to look for help... woo hoo....
Nov 7 '07 #10
One more thing, for some reason, my email body/message doesn't do any carriage return. I want the email body format as below:

CONTRACT NO VENDOR NAME
235424452 Cashing Inc
984534577 IBM

I did add "\n" when I want to go to the next line, but it didn't show in the email as expected. my code is as follows:

Expand|Select|Wrap|Line Numbers
  1. $prev_docid = $prev_docid."<a href=$httplink>$account_id</a>"."                ".$vendor_name."\n";     
  2.  
  3. $message = qq~
  4. Dear $prev_vrm:\n\n 
  5.    You have the following contracts with High Risks.\n
  6.     Please login to the website link below to view the contract(s).\n 
  7.     CONTRACT NO:\n\n$prev_docid 
  8.     ~;
  9.  
Nov 7 '07 #11
eWish
971 Expert 512MB
First, sorry about the quotes around the header in my previous post. I don't use here-doc much. If you are sending as HTML then you can use HTML markup. Here is a sample script that is for testing only.

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl 
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use CGI;
  7. use CGI::Carp qw/fatalsToBrowser/;
  8.  
  9. my $q = CGI->new;
  10.  
  11. print $q->header; 
  12. print $q->start_html(-title =>'Mysite.com'); 
  13.  
  14. my $mail_path         = '/usr/sbin/sendmail -i -t';
  15. my $email_to        = 'to@somesite.com';
  16. my $email_from        = 'from@somesite.com';
  17. my $email_subject    = 'Testing My Email';
  18.  
  19. my $email_message     = qq~ 
  20. Hello $email_to,<br><br>
  21.  
  22. Thank you for $email_subject service.<br>  
  23. <a href="http://www.somesite.com">Some Site</a>
  24. ~;
  25.  
  26.  
  27. &send_email($email_to, $email_from, $email_subject, $email_message);
  28.  
  29.  
  30. sub send_email{
  31.  
  32.     my ($to, $from, $subject, $message) = @_;
  33.  
  34.        open (my $MAIL, "|$mail_path") || print "Could Not Open Mail Program!";
  35.  
  36.            print $MAIL "Content-type:text/html; charset=iso-8859-1\n";
  37.            print $MAIL "To: $to\n";
  38.            print $MAIL "From: $from\n";
  39.            print $MAIL "Subject: $subject\n\n";
  40.            print $MAIL "$message\n\n";
  41.  
  42.    close ($MAIL);
  43.  
  44.    print 'Done.  Please check your email box';
  45.  
  46. }
  47.  
  48. print $q->end_html();
  49.  
  50. 1;
Nov 7 '07 #12
eWish
971 Expert 512MB
Kevin

I don't use here-doc's much. I did not think about the quotes. Also, I have used this header in the past without an problems. But since then I have moved to modules. I better go read up on my PHP some more now. :)

--Kevin
Nov 7 '07 #13
I used <br> for newline and it works.

What about syntex for tab or make spaces between two fields in HTML? how do I apply to perl script to print the result shown below? e.g.

Contract_No <10 spaces> Vendor_Name
Nov 7 '07 #14
eWish
971 Expert 512MB
HTML equal to a non-braking space is &nbsp;

Expand|Select|Wrap|Line Numbers
  1. my $space = '&nbsp;' x 10;
  2. print 'Hello', $space, 'There';
You can also use the <pre> </pre> tags which will preserve the formatting as it is written.
Expand|Select|Wrap|Line Numbers
  1. <pre>Hello          There</pre>
Nov 7 '07 #15
Again, thank you so much Kevin for your prompt response and patience. You've got all my questions answered. and That's exactly what ALL I need. Thousands of thanks!!!
Nov 7 '07 #16
KevinADC
4,059 Expert 2GB
Kevin

I don't use here-doc's much. I did not think about the quotes. Also, I have used this header in the past without an problems. But since then I have moved to modules. I better go read up on my PHP some more now. :)

--Kevin

I never use here-docs. I use quote operators, they are just much more flexible than finicky here-docs. I think they are also more efficient. For emails I also pretty much use MIME::Lite. Once you understand the interface it's a joy to use.

I wish you would stop posting that obscene word. I may have to report you to Mary and suggest she whacks you on your pee-aych-pee.

;)
Nov 7 '07 #17
KevinADC
4,059 Expert 2GB
I used <br> for newline and it works.

What about syntex for tab or make spaces between two fields in HTML? how do I apply to perl script to print the result shown below? e.g.

Contract_No <10 spaces> Vendor_Name
Your questions are devolving into html questions. Read an html tutorial to learn how to use html encoding in documents. html is very simple stuff for the most part and the basic formmating tags can be learned in a few minutes.
Nov 7 '07 #18
For the fun part, I tried what you suggested, i.e. using MIMI::Lite. However, when I run it, it issued the following message:

"Can't locate MIME/Lite.pm in @INC (@INC contains: /usr/opt/perl5/lib/5.8.0/aix-t
hread-multi /usr/opt/perl5/lib/5.8.0 /usr/opt/perl5/lib/site_perl/5.8.0/aix-thre
ad-multi /usr/opt/perl5/lib/site_perl/5.8.0 /usr/opt/perl5/lib/site_perl .) at s
ample.pl line 4.
BEGIN failed--compilation aborted at sample.pl line 4."
Nov 7 '07 #19
eWish
971 Expert 512MB
That would mean that you do not have MIME::Lite installed on your server. perlfaq8 "How do I install a module from CPAN?"
Nov 7 '07 #20

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Thubaiti | last post: by
3 posts views Thread by Kumar | last post: by
2 posts views Thread by LmcE | last post: by
4 posts views Thread by wrytat | last post: by
4 posts views Thread by =?Utf-8?B?QW1pciBUb2hpZGk=?= | last post: by
20 posts views Thread by tshad | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.