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

HTML link nor working in email

Status
Not open for further replies.

maslett

Technical User
Mar 11, 2004
121
GB
Hi, I'm trying to send an email with a link in it. I searched the forum and found out I needed to put a content type line in after the subject. I have done this but the content type line is just printed in the mail. Here's the code:

Code:
sub setup_email
{
$msgtext =<<__STOP_OF_MESSAGE__;
Content-Type: text/html\n\n
$fields{'the_message'}

<a href="/cgi-bin/reply.pl?id=$fields{'new_id'}">Link</a>
__STOP_OF_MESSAGE__
}

Here's the email output:

Code:
Content-Type: text/html

Test email.

<a href="/cgi-bin/reply.pl?id=2305120052131447">Link</a>

Thanks for reading :)
 
I'd make a guess that the content type your sending is after one has already been sent. Please post the actual portion of your script that handles the e-mailing of your content so we can see where the problem may lie.

- Rieekan
 
Email headers are separated from the rest of the body by a contentless line (i.e. just a \r\n pair). Only the first such a line matters. Anything after that, no matter whether it's formatted like a header or not, is classed as body content.
 
Thanks for the input - I would have replied earlier but the tek-tips emails that inform me of a reply aren't being sent!?

Here's the code that sends the email - I didn't write it - I'm hacking a free script i got off the net and it's the most complicated piece of code for sending an email.

Code:
##################################################################
sub send_mail
{

&setup_letter;
$mailresult=&sendmail($fields{sender_email}, $fields{sender_email}, $fields{recip_email}, $SMTP_SERVER, $SUBJECT, $msgtext); 

}

##################################################################
sub setup_letter
{
$msgtext =<<__STOP_OF_MESSAGE__;
Content-Type: text/html\n\n
$fields{'the_message'}

<a href="/cgi-bin/reply.pl?id=$fields{'new_id'}">Link</a>
__STOP_OF_MESSAGE__
}
###################################################################
sub sendmail  {

# error codes below for those who bother to check result codes <gr>

# 1 success
# -1 $smtphost unknown
# -2 socket() failed
# -3 connect() failed
# -4 service not available
# -5 unspecified communication error
# -6 local user $to unknown on host $smtp
# -7 transmission of message failed
# -8 argument $to empty
#
#  Sample call:
#
# &sendmail($from, $reply, $to, $smtp, $subject, $message );
#
#  Note that there are several commands for cleaning up possible bad inputs - if you
#  are hard coding things from a library file, so of those are unnecesssary
#

    my ($fromaddr, $replyaddr, $to, $smtp, $subject, $message) = @_;

    $to =~ s/[ \t]+/, /g; # pack spaces and add comma
    $fromaddr =~ s/.*<([^\s]*?)>/$1/; # get from email address
    $replyaddr =~ s/.*<([^\s]*?)>/$1/; # get reply email address
    $replyaddr =~ s/^([^\s]+).*/$1/; # use first address
    $message =~ s/^\./\.\./gm; # handle . as first character
    $message =~ s/\r\n/\n/g; # handle line ending
    $message =~ s/\n/\r\n/g;
    $smtp =~ s/^\s+//g; # remove spaces around $smtp
    $smtp =~ s/\s+$//g;

    if (!$to)
    {
	return(-8);
    }

 if ($SMTP_SERVER ne "")
  {
    my($proto) = (getprotobyname('tcp'))[2];
    my($port) = (getservbyname('smtp', 'tcp'))[2];

    my($smtpaddr) = ($smtp =~
		     /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
	? pack('C4',$1,$2,$3,$4)
	    : (gethostbyname($smtp))[4];

    if (!defined($smtpaddr))
    {
	return(-1);
    }

    if (!socket(MAIL, AF_INET, SOCK_STREAM, $proto))
    {
	return(-2);
    }

    if (!connect(MAIL, pack('Sna4x8', AF_INET, $port, $smtpaddr)))
    {
	return(-3);
    }

    my($oldfh) = select(MAIL);
    $| = 1;
    select($oldfh);

    $_ = <MAIL>;
    if (/^[45]/)
    {
	close(MAIL);
	return(-4);
    }

    print MAIL "helo $SMTP_SERVER\r\n";
    $_ = <MAIL>;
    if (/^[45]/)
    {
	close(MAIL);
	return(-5);
    }

    print MAIL "mail from: <$fromaddr>\r\n";
    $_ = <MAIL>;
    if (/^[45]/)
    {
	close(MAIL);
	return(-5);
    }

    foreach (split(/, /, $to))
    {
	print MAIL "rcpt to: <$_>\r\n";
	$_ = <MAIL>;
	if (/^[45]/)
	{
	    close(MAIL);
	    return(-6);
	}
    }

    print MAIL "data\r\n";
    $_ = <MAIL>;
    if (/^[45]/)
    {
	close MAIL;
	return(-5);
    }

   }

  if ($SEND_MAIL ne "")
   {
     open (MAIL,"| $SEND_MAIL");
   }

    print MAIL "To: $to\n";
    print MAIL "From: $fromaddr\n";
    print MAIL "Reply-to: $replyaddr\n" if $replyaddr;
    print MAIL "X-Mailer: Perl Powered Socket Mailer\n";
    print MAIL "Subject: $subject\n\n";
    print MAIL "$message";
    print MAIL "\n.\n";

 if ($SMTP_SERVER ne "")
  {
    $_ = <MAIL>;
    if (/^[45]/)
    {
	close(MAIL);
	return(-7);
    }

    print MAIL "quit\r\n";
    $_ = <MAIL>;
  }

    close(MAIL);
    return(1);
}

Cheers.
 
Your issue is where you print the Subject. Placing two return characters is pushing you out of your Headers section. Change the \n\n to a single \n and you should be good to go.

- Rieekan
 
Cheers - Rieekan, I can't try it at work as we can't ftp stuff around - will try it later.
 
Right - sussed it - will write the fix here in case it's useful for someone else.

This code relates to the code above:
Code:
    print MAIL "To: $to\n";
    print MAIL "From: $fromaddr\n";
    print MAIL "Reply-to: $replyaddr\n" if $replyaddr;
    print MAIL "X-Mailer: Perl Powered Socket Mailer\n";

    print MAIL "Subject: $subject\n";
    print MAIL "Content-type: text/html\n";

    print MAIL "$message";
    print MAIL "\n.\n";

I moved the 'Content-type' line to the above code and removed the second \n from the subject line.

Cheers all
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top