boboson has asked for the wisdom of the Perl Monks concerning the following question:

I am going nuts soon! I am trying to send an email to a newly registered user of my webpage with an membership activation link. Here are some parts from my code.
#this is my sub for sending the email sub sendMail{ # application object my $self = shift; # get arguments my ($to, $from, $subject, $text) = @_; open (MAIL, "|/usr/lib/sendmail -t -oi") or die("Can not find sendmail: $!"); print MAIL <<_MAIL_; Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable To: $to From: $from Cc: Subject: $subject $text _MAIL_ close MAIL; }
I am calling the sendMail sub from another sub with the following data:
my $from = "noreply\@xxxxxx.com"; my $subject = "Membership"; my $link = "http://www.xxxxxx.com/xxxxxx/index.cgi?rm=activate&id=$act +ivate_code&email=$email"; my $text = "<html><head><title></title></head><body><a href=\"$link\"> +$link</a></body></html>"; $self->sendMail($email, $from, $subject, $text);
There can be some smaller syntax error in the above code because I took it out from the bigger picture.
I am using Data::GUID for my $activate_code for example: 956F8CE6-BA7D-11DA-A132-94F4820E4FA7 and the email to the user comes from a mySQL DB.
I've tried to encode the link with the following sub:
sub URLEncode { my $theURL = $_[0]; $theURL =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg; return $theURL; }
I dont know where to start with my questions?
First of all should I send the email as text/plain or text/html?
Can I somehow make sure this email can be read from both commercial email services and for example: outlook? Do I have to encode the link?
I pretty much trialed and errored every possible combination encoding/decoding to different email addresses. If someone could point me in the right direction, is there any modules I can use or ready made scripts for this?

Replies are listed 'Best First'.
Re: email activation link
by timos (Beadle) on Mar 23, 2006 at 16:21 UTC
    You could use Mail::Send to send your mails, so you wouldn't have to care about creating an RFC-compliant header and stuff. IMHO you should send mails in plaintext whenever possible because all MUA's can display them, which is not true for HTML.
    use Mail::Send; $msg = new Mail::Send; $msg->to('user@host'); $msg->subject('example subject'); $fh = $msg->open('sendmail'); print $fh "Body of message"; $fh->close;
    What you could do to make registration easier is: Allow the user to reply, keeping subject intact. So he hasn't go trough the hassle of fireing up his browser and visit any links.
      Thanks for the guidance. Now I understand a little bit more how the different modules work. One problem though. I am now using the MIME::Lite module to send my mail with sendmail.
      my $msg = MIME::Lite->new( From =>'johan@domain.com, To =>'user@hotmail.com', Subject =>'Registration', Data =>"Klick on the link.\n\n$link\n\n ); $msg->send;
      However, probably because of all spam being sent nowdays, I can't send the same email twice to a hotmail, gmail address. If I change the from field it's ok but otherwise it won't even hit the spambox. Does anyone now if this is within a timeframe or...just curious
      I agree with both of timos's points here. Allowing the user to register by simply replying with an email where
      the subject line is automatically filled in is a good practice. More generally, making subscribing and unsubscribing
      to email lists as simple as possible should be the goal of any list operator.

      basically, I make sure that the registered users emailadress is valid and belongs to the user.
      How would my application know that the user replied?
      Thanks for the guidance. Now I understand a little bit more how the different modules work. One problem though. I am now using the MIME::Lite module to send my mail with sendmail.
      my $msg = MIME::Lite->new( From =>'johan@domain.com, To =>'user@hotmail.com', Subject =>'Registration', Data =>"Klick on the link.\n\n$link\n\n ); $msg->send;
      However, probably because of all spam being sent nowdays, I can't send the same email twice to a hotmail, gmail address. If I change the from field it's ok but otherwise it won't even hit the spambox. Does anyone now if this is within a timeframe or...just curious
Re: email activation link
by eric256 (Parson) on Mar 23, 2006 at 23:21 UTC

    I'm not sure which of those was the question. lol. If its about the encoding then from your tries you should no that there is no way to be sure the user will get a link they can click. In practice I never, and you shouldn't either, click links in emails. Instead it is much safer to cut and paste the link into a browser. In that case your script just needs to email a link to the user with instructions for them to paste it back into the browser. Then you just make sure that said link goes to your script. You'll want some form of table to store users who are awaiting authentication, then when they hit the script it chekcs the code they entered and approves there account when their code comes in.

    Same could be done with the email solutions others have suggested, your script just needs to periodicaly check the email and parse the incoming messages.


    ___________
    Eric Hodges