Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending HTML Email via Perl Script using sendmail

Status
Not open for further replies.

marybailey

Programmer
Mar 14, 2002
47
US
Does anyone know how - or know the site that will tell me how - to code, in PERL, the sending of an HTML email. Right now, I know how to do text email using sendmail - the old

print MAIL "some string";

coding - but if I put html code in the string portion of the MAIL line I just get an email with the html code in it. (I'm modifying FormMail from Matt's Script Archive if thats any help.)

I'm desperate!

Thanks,
Mary B.
 
You just need to print the "Content-type: text/html\n\n"; into the mail before the body of the mail and after the header info.
The following was clipped from one of our apps.

Code:
open (MAIL, "|$mailprog -f $sender_name $recipient")
       or showError("Can't open $mailprog!, $!");
print MAIL "From: $sender_name\n";
print MAIL "Reply-To: $sender_name\n";
print MAIL "Errors-to: $sender_name\n";
print MAIL "Subject: Some text here\n";
print MAIL "Content-Type: text/html charset=iso-8859-1\n";
print MAIL "Precedence: bulk\n\n";
# print MAIL "-----------------------------------------------------------\n";
print MAIL "$email_content\n";
print MAIL '.';
close (MAIL);
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Okay I put that in but same problem. What is in $email_content? Html?
Mary
 
Here is the code. Is the -t the problem?

open(MAIL,"|$mailprog -t");

print MAIL "To: $Config{'recipient'}\n";
print MAIL "From: $Config{'email'} ($Config{'realname'})\n";

# Check for Message Subject
if ($Config{'subject'}) { print MAIL "Subject: $Config{'subject'}\n\n" }
else { print MAIL "Subject: Submission\n\n" }
print MAIL "Content-Type: text/html charset=iso-8859-1\n";
print MAIL "MIME-Version: 1.0\n";

print MAIL &quot;<html>\n&quot;;
print MAIL &quot;<head>\n&quot;;
print MAIL &quot;</head>\n&quot;;
print MAIL &quot;<body>\n&quot;;
print MAIL &quot;hello world\n&quot;;
print MAIL &quot;</body>\n&quot;;
print MAIL &quot;</html>&quot;;


close (MAIL);
 
I think the '-t' should work ok.
I don't see anything immediately that looks wrong.
Can you describe the symptoms?
Is the mail getting sent?
If so, do you receive it?
If you do, does it have any content?
etc.... 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
The email is sent EXCEPT its contents is the html code - as if it were just plain text.
 
Is it possible this is a MIME issue? I am having the site hosted by Earthlink. Maybe they dont have MIME? I dont know.
 
Try commenting that line out and see what happens. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Someone told me to set the content type - that my code only prints it. I dont know how to do that. Do you?
This is maddening!
 
This works on my system.
Solaris with Perl 5.6.

[/code]
#!/usr/local/bin/perl
$mailprog = '/usr/lib/sendmail';
$recipient = 'user@mail.server.com';
$command = &quot;$mailprog $recipient&quot;;
open (MAIL, &quot;|$command&quot;) or
die &quot;Can't open $mailprog!\n&quot;;

print MAIL &quot;From: user\@mail.server.com\n&quot;;
print MAIL &quot;Reply-To: user\@mail.server.com\n&quot;;
print MAIL &quot;Subject: Demo HTML formatted mail with sendmail\n&quot;;
print MAIL &quot;Content-Type: text/html charset=iso-8859-1\n&quot;;
print MAIL &quot;Precedence: normal\n\n&quot;;
print MAIL &quot;----------------------------------------\n&quot;;
print MAIL &quot;<hr>&quot;;
print MAIL &quot;<p><strong>some text in bold</strong></p>&quot;;
print MAIL &quot;<p><i>some text in italics</i></p>&quot;;
print MAIL &quot;<p><font color='blue'>some text in bold</font></p>&quot;;
print MAIL &quot;.&quot;;
close MAIL;
[/code]

You could put a valid email address in this, make it work, and then carefully expand it as needed.

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Figured it out. By accident of course. For some reason, when I removed blank lines between my &quot;print MAIL string&quot; it worked as HTML. Dont ask me why! I only work here.
Thanks to the two who were helping me!
Jean
 
By the way, if you want to know what the -t in the sendmail command does, it tells sendmail that the &quot;to&quot; address is supplied in the email message itself instead of on the command line. It's a security feature. Normally you would call sendmail with the to address on the command line. But if you just take any string entered by someone and assume it's an email address and put it in a sendmail command it could be very dangerous. The person could put shell metacharacters in the so-called email address and do all kinds of malicious things. So the -t flag tells sendmail that there is no to address on the command line, it's in the email message itself, where shell metacharacters can't do any harm. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top