A html anchor link gets the site URL prepended, breaking it
| Project: | Mime Mail |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
It's fairly common in newsletters to have an initial table of contents with anchor links to the articles below:
In This Newsletter ...
<a href="#1">What's New</a>
<a href="#2">Breaking News</a>
..etc
now the body of the email ...
<h2 id="1">What's New</h2><br>
... some text here ...
<h2 id="2">Breaking News</h2><br>
... some text here ...When I do this using SimpleNews / MimeMail, the links in the test email get my site $URL (http://lifetwo.com/production) prepended to them so that the first anchor link, for example, is no longer "#1" but "http://lifetwo.com/production/#1." Of course, that's not what we want -- we want the user to click on the #1 anchor and jump down to that section of the email, not get linked back to the site. It's also a nonexistent URL.
Although I'm composing and sending the test newsletter in html mode, I can see that the same problem exists for the plaintext links.
On the actual node page for the newsletter, the link is fully formed and behaves correctly -- clicking on it jumps you down the page to the appropriate anchor.
If this is an issue w/ MimeMail or some other module, please let me know and I'll cross post there. I thought SimpleNews was the most likely suspect since the change log indicated that work was done with internal links a while back.
thanks!

#1
Did you ever figure out why this was happening? I'm having to deal with the same thing.
#2
After digging around, I found that the following code in mimemail.inc is the culprit
//expand all local links$pattern = '/(<a[^>]+href=")([^"]*)/emi';
$body = preg_replace($pattern, '"\\1"._mimemail_url("\2")', $body);
So, I get the idea. A newsletter might be sprinkled with links pointing back to your site and this would make the links work properly. I think its safe to assume though that if the anchor href is "#text", chances are it's not pointing back to the site.
Any ideas how to enhance the regex pattern to not catch "#text"?
#3
alright, I have a fix.
In mimemail.inc, look for the function "_mimemail_url($url)".
I added the following code prior to the return statement
if (empty($path) && !empty($fragment)) {return '#' . $fragment;
}
}
Basically, I assume that a url with just the fragment is a jump link within the email. The reason I check that $path is empty is so that urls like "services#mytarget" are properly converted.
#4
That looks like a good fix.
Yes, most embedded links in an email should be expected to be fully-justified.
But, purely anchor tags can probably be exempted.
Good idea.
#5
It's such a simple fix.
#6
jbjaaz, I can agree with you that it is a simple patch. However it is not good practice to set your own patches to RTBC when you are not the module (co-)maintainer. But you have overlooked the most important fact. That this patch is for mimemail and it is currently in simplenews issue queue. Therefore I transfer it.
I recommend that this bug get fixed. The patch looks Ok to me from reviewing the code. Not tested.
#7
Thanks for catching this. Saved me some work.
#8
Tested this patch and verified that it resolves the issue mentioned above. Patch applies successfully and behavior is as expected - this should be clear to commit.
#9
Uploading an updates patch, this patch takes into account the "Code Cleanup" patch from: http://drupal.org/node/220432
This update requires that patch prior to being applied.
#10
Committed to HEAD, will roll a D5 release soon. Thanks!
#11
Automatically closed -- issue fixed for two weeks with no activity.