Why are some anchor tags incorrect?

167 Views Asked by At

I am using Zend_Mail to send text emails via SMTP. In the test environment, I have noticed that when collecting my emails via a browser client, URL's have been converted to hyperlinks, but the boundary between the link and normal text has been drawn in the wrong place.

For example, in the email snippet below, you can see that I have sent a Registration email to the user. It includes parameters for 'id' and 'code'. You will see that in this case the browser client has decided that the 'id' is part of the hyperlink, but the 'code' is not.

please follow this link <a href="http://mydomain.com/user/validate/id/18">http://mydomain.com/user/validate/id/18</a>/code/1a0d917c1512c700f7237ae1f3727e47 If you did not...

Sometimes the 'boundary' is drawn in an arbitrary spot - for example, half way through the code field!

When in collect the email via client such as Outlook, the problem does not seem to occur.

Presumably this is some kind of incompatibility of character sets between my PHP environment and my browser client. Having said that, I'm using a stock standard PHP install, and I have not had problems receiving links from other sites in my browser client. (The browser client is provided as part of a Webmasters account.)

Any ideas?

UPDATE: Here's the code that generates the link. Tried all kinds of combinations including EOT, single quotes, double quotes... Same result...

$id         = $options['id'];
$code       = $options['validation'];
$link       = "http://mydomain.com/user/validate/id/" . $id . "/code/" . $code;

$body = 'Thanks for registering ... please follow this 
link ' . $link . ' If you did not request ....please ignore it.';

$this->setBodyText($body);
1

There are 1 best solutions below

0
On

With setBodyText() you set the text only part of the email: when you write in the plain text email a link you only write some characters with no particular meaning; the client that receive the mail than will understand if that is a link and how to render it. This process however is out of our control (it's clientside and depends on the client the user has).

The best solution in my opinion is to set also the HTML part of the Zend_Mail object calling the

$yourMailObject->setBodyHtml($htmlString);

Doing this you tell explicitly to the mail client how to render the link.

Obvious you have to work a little bit more, but since you want the client to render an HTML property as a A tag, the better way is to pass him the proper HTML.

To summarize, the workflow is as follow:

  1. determine the link and put it (let's say) in the $link variable
  2. prepare the mail body in plain text: $bodyText = "Something... click here: $link ...";
  3. prepare the mail body in HTML: $bodyHtml = "<body><p>Click the link below</p><p><a href='$link'>$link</a></p><p>Or copy and paste in your browser the url:<br/><pre>$link</pre></p></body>";
  4. call both the methods of the mail object: $yourMailObject->setBodyText($bodyText)->setBodyHtml($bodyHtml);

Then you cand send the mail as usual. You can optionally customize the html in the more complex way, adding inline css an so on. You can also format it to appear as a text only mail (with a monospace font) but since you passed the correct HTML you can be sure the client will click the proper link.