Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Inserting a hyperlink

by rio (Initiate)
on Nov 22, 2006 at 04:34 UTC ( #585436=perlquestion: print w/replies, xml ) Need Help??
rio has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Thanks for helping with my question yesterday regarding writing a perl code for sending a recurring email to my studens. Based on your suggestions I've comeup with the following. I would like to insert a hyper link. Any suggestions on how to do that?
#!/usr/bin/perl use MIME::Lite; use Net::SMTP; ### Adjust sender, recipient and your SMTP mailhost my $from_address = ''; my $to_address = ''; my $mail_host = ''; ### Adjust subject and body message my $subject = 'Flower Arrangement Class - check list'; my $message_body = "This is a list of things to bring\nFresh flowers f +rom Flowershop X\nToday's Arragement instructions from instruciton ho +me page\n"; ### Create the multipart container $msg = MIME::Lite->new ( From => $from_address, To => $to_address, Subject => $subject, Type =>'multipart/mixed' ) or die "Error creating multipart container: $!\n"; ### Add the text message part $msg->attach ( Type => 'TEXT', Data => $message_body ) or die "Error adding the text message part: $!\n"; ### Send the Message MIME::Lite->send('smtp', $mail_host, Timeout=>60); $msg->send;
I would like to add a hyperlink to "Fresh flowers from the flowershop X" and "Today's Arragement instructions from instruciton home page".
Any ideas on how to do that?

Edited (davorg): Removed very strange formatting

Replies are listed 'Best First'.
Re: Inserting a hyperlink
by dorward (Curate) on Nov 22, 2006 at 08:42 UTC

    You're writing a plain text (i.e. normal) email, so you can't explicitly add links.

    What you can do is insert a URL in the plain text, surrounded by whitespace, and preferably on a new line (for word wrap issues), and expect the vast majority of email clients to recognise it as a URL and turn it into a link for you.

    my $message_body = "This is a list of things to bring\n" . "Fresh flowers from Flowershop X\n" . "\n\n" . "Today's Arragement instructions from instruciton home page\n" . "\n\n";
      dorward Works a charm. Thanks. I'm wondering if there's a easy way to insert a hyperlink that can be clicked directly. Anyway. Thanks a bunch.

        You have a choice. Firstly, you can send a plain text attachment that contains the URL. As doward explained, most modern mail programs will recognise the the URL and turn it into a clickable link when the mail is displayed. In this case, your mail looks like this:

        my $message_body = <<END_OF_BODY; This is a list of things to bring * Fresh flowers from Flowershop X * Today's Arrangement instructions from instruction home page END_OF_BODY

        That's the only way it can work in plain text. You can just include the URL and rely on the mail program to make it clickable.

        Alternatively, you can create a HTML version of the mail which contains real <a> tags. In this case, your mail looks something like this:

        my $message_body = <<END_OF_BODY; <p>This is a list of things to bring</p> <ul> <li>Fresh flowers from <a href="">Flowe +rshop X</a></li> <li>Today's Arrangement instructions from <a href="http://www.example. +com/arrangement/">instruction home page</a></li> </ul> END_OF_BODY

        In this second example, you would need to set the content type of the attachment as 'text/html' so that the mail program that is displaying it knows how to treat it.

        As several people have mentioned in this thread, if you want HTML mail, then the best approach is to have two attachments, one of which is an HTML version and the other of which is the plain text version. In this case, you need to find out how your Mail-building module of choice (and I can only repeat once more my recommendation for the Perl Email Project) builds MIME messages with more than one attachment.


        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: Inserting a hyperlink
by Samy_rio (Vicar) on Nov 22, 2006 at 05:57 UTC

    Hi rio, give the hyperlink using HTML tags in the message part and mention the type as "text/html". See as below:

    my $message_body = "This is a list of things to bring\n<a href=\"http: +//\">Fresh flowers from Flowershop X</a>\n<a href=\" +\">Today's Arragement instructions from instrucito +n home page</a>\n"; ### Add the text message part $msg->attach (Type => 'text/html', Data => $message_body ) or die "Error adding the text message part: $!\n";

    Velusamy R.

    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

      Don't do that.

      HTML formatted email without a plain text alternative is a strong indicator or spammyness (or at least cluelessness). It is a very good way to get diverted to the spambucket.

      If you must do HTML formatted email, then Email::MIME::CreateHTML is a decent choice, and makes it easy to provide the plain text alternative.

Re: Inserting a hyperlink
by rio (Initiate) on Nov 22, 2006 at 08:29 UTC
    Thanks Sammy but it didn't work. It seems to be printing the text as it is.
      Meta comment: Perl monks has a threading facility. While you've phrased your response as if to Samy_rio, you seem to have marked it as a response to your own (thread starting) message. Its easier to follow discussions if threading actually represents the conversation. You should see a link beside each comment that you can use if you want to respond to that comments. Thanks.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://585436]
Approved by Samy_rio
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2017-09-24 10:43 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (274 votes). Check out past polls.