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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Net::SMTP HTML Mail Catch 22 dilema

Status
Not open for further replies.

JNameNotTaken

Programmer
Aug 29, 2006
24
0
0
IE
Hello

I'm able to send mail ok using Net::SMTP but I have an issue I'm hoping sombody can please help with.

Code:
Code:
use Net::SMTP;
my $mail_body = "my html message";
my $smtp = Net::SMTP->new('smpt.mydomain.com');
$smtp->mail( $email ); #from email address
$smtp->to( $RECIPIENT_EMAIL ); #to email address
$smtp->data();

# SEND THE HEADER.
$smtp->datasend("From: " . $email . "\n");
$smtp->datasend("To: " . $RECIPIENT_EMAIL . "\n");
$smtp->datasend("Subject: " . $subject . "\n");
$smtp->datasend("\n");
# END SEND THE HEADER

##NOTE THESE LINES
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("Content-Type: text/html; charset=us-ascii\n");
##############

$smtp->datasend("\n");
# Send the body.
$smtp->datasend( $mail_body );
$smtp->datasend("\n");
$smtp->dataend();
$smtp->quit;

It works fine but, the content of the mail is not received in html format.

So, placing the lines highlighted above before the header is sent (see code comment) results in the mail being received in html format but, the header is part of the mail body and the subject is not reconised, i.e. their is no subject in the mail.

What is a guy to do?
If I set the content-type to text/html before the header, the header is not reconised as so and becomes part of the mail.
If I set the Content-type to text/html after the header it has no effect. The mail is received as text.

Any help would be great.
Thanks Very Much
 
afaik it should be like this:

Code:
To: soandso@nowhere.net\n
From: myname@nowhere.net\n
Subject: hello world\n
MIME-Version: 1.0\n
Content-Type: text/html\n
\n
body of the message

Generally headers all stay together and a \n\n indicates that the header has ended and the remaining content is the body, and generally content-type belongs in the header. The rules might be a little different in SMTP but give that a try.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Cheers, kirsle you appear to be correct.

Additionally, I think the extra \n between the header and the body may have done the trick but I couldn't be certain.

In any case the following code works:

Code:
my $smtp = Net::SMTP->new('smpt.mydomain.com');

$smtp->mail( $email );
$smtp->to( $RECIPIENT_EMAIL );

$smtp->data();
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("Content-Type: text/html; charset=us-ascii\n");

# Send the header.
$smtp->datasend("From: " . $email . "\n");
$smtp->datasend("To: " . $RECIPIENT_EMAIL . "\n");
$smtp->datasend("Subject: " . $subject . "\n");
$smtp->datasend("\n");
$smtp->datasend("\n");

# Send the body.
$smtp->datasend( $mail_body );
$smtp->datasend("\n");
$smtp->dataend();
$smtp->quit;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top