CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Converting relative URLs in emails to be absolute
Pages: [1]

Author Topic: Converting relative URLs in emails to be absolute  (Read 1508 times)

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
Converting relative URLs in emails to be absolute
August 26, 2010, 01:21:09 am
Generally, I want HTML links and images to use relative URLs (in CiviEvent info pages, for instance) but for emails, these URLs need to be absolute so the recipient can access the resource.

My plan is replace relative URLs with absolute by doing ...

Code: [Select]
$htmlMessage = str_replace(' href="/', ' href="http://www.example.com/', $htmlMessage);
$htmlMessage = str_replace(' src="/', ' src="http://www.example.com/', $htmlMessage);

...in the send() function in CRM/Utils/Mail.php (for my purposes I only need to consider URLs in the form '/sites/default/files/happy.jpg')

My questions are...
  • Has someone already done this?
  • Are there gotcha's I should consider?
  • Is there a better way?
  • Is there a wider demand for this? (If so, I could contribute a patch)

Thanks,
Ken
« Last Edit: August 26, 2010, 01:24:07 am by ken »

emilyf

  • Ask me questions
  • ****
  • Posts: 696
  • Karma: 54
  • CiviCRM version: 2.x - 4.x
  • CMS version: Drupal 5, 6, 7
Re: Converting relative URLs in emails to be absolute
August 26, 2010, 05:06:33 am
I am always interested in this functionality. I think in the past I have been achieving this by editing the text editor config, as I believe there are options in there for using absolutes. But it would be nice if it was an option in civi  :)

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Converting relative URLs in emails to be absolute
August 26, 2010, 07:13:54 am

CiviMail and other emails should always use absolute urls. most of the other pages should use relative urls

in what cases are relative urls used in civimail/email?

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
Re: Converting relative URLs in emails to be absolute
August 26, 2010, 03:20:59 pm
If I create an activity "Send an email" and insert an image using a relative URL, the email is delivered with a relative URL and the image is unreadable. If I send a CiviMail containing an image with a relative URL, the result is the same.

I get these result on 3.1.5 under Joomla and 3.2.2 under Drupal. I don't think the demo will allow me to send emails, so I don't think I can reproduce it there.

Here is the patch I applied to fix this...

Code: [Select]
--- /data/Download/CiviCRM/civicrm-3.2.2-drupal/CRM/Utils/Mail.php 2010-08-26 21:54:44.125758811 +1000
+++ /data/Work/IT/CiviCRM/Local/3.2.2/php/CRM/Utils/Mail.php 2010-08-26 21:56:28.023490707 +1000
@@ -111,6 +111,9 @@
         }
 
         if ( $htmlMessage ) {
+            $config = CRM_Core_Config::singleton( );
+            $htmlMessage = str_replace(' href="/', ' href="'.$config->userFrameworkBaseURL, $htmlMessage);
+            $htmlMessage = str_replace(' src="/', ' src="'.$config->userFrameworkBaseURL, $htmlMessage);
             $msg->setHTMLBody($htmlMessage);
         }
 

It's pretty crude: it doesn't fix the text message (is there a better place to put this?); it may not work on multi-site configurations; and it probably breaks valid messages. It simply looks for SRCs in IMG elements and HREFs in A elements which start with a '/' and prepends the site URL.
« Last Edit: August 26, 2010, 07:57:49 pm by ken »

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Converting relative URLs in emails to be absolute
August 26, 2010, 04:14:15 pm

sorry, i made a mistake and assumed it was civicrm urls

that code might be better in the mail alter hook rather than hacking core

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
Re: Converting relative URLs in emails to be absolute
September 21, 2010, 05:12:45 am
Lobo,

Thanks for your suggestion about using a hook. I've implemented the hook, and posted the code on another thread at http://forum.civicrm.org/index.php/topic,12091.msg67544.html#msg67544

Ken

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Converting relative URLs in emails to be absolute

This forum was archived on 2017-11-26.