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 CiviMail (Moderator: Piotr Szotkowski) »
  • EMail images using relative url instead of absolute
Pages: [1]

Author Topic: EMail images using relative url instead of absolute  (Read 1115 times)

tso2085

  • I post occasionally
  • **
  • Posts: 75
  • Karma: 0
  • CiviCRM version: 4.7.15
  • CMS version: Joomla 3.6.5
  • MySQL version: 5.5.37
  • PHP version: 5.5.30
EMail images using relative url instead of absolute
November 15, 2012, 06:45:18 am
When I create emails to send to members, the images that I insert do not use the absolute url, but rather the relative url. This is fine for articles and items within the website, but not for emails.  So when a user receives the email, the image links are obviously broken.

Should this not be a feature of CiviMail to convert the relative urls to absolute?  This is a feature of other mail programs such as AcyMail and others.

I used one editor, JCE, that allowed you to set that parameter, but I want to use JCKEditor, which does not have that option.  However, it seems this should not be a requirement of the editor, but of the mail program (CiviMail).

Thanks,
Craig

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: EMail images using relative url instead of absolute
November 15, 2012, 07:18:30 am
That's a good idea indeed.

Not sure if it's trivial to detect what's a relative link (and where you need to add the link) or not

href="bla.html" href="/folder/bla.html" are relative, but

href="http://example.org" or href="mailto:" or href="//example.org/" are "absolute/no need to convert

Would be great you can identify what are the rules existing elsewhere

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

tso2085

  • I post occasionally
  • **
  • Posts: 75
  • Karma: 0
  • CiviCRM version: 4.7.15
  • CMS version: Joomla 3.6.5
  • MySQL version: 5.5.37
  • PHP version: 5.5.30
Re: EMail images using relative url instead of absolute
November 15, 2012, 09:07:34 pm
Hello Xavier,

Here is a coding example that I found while researching the issue.   Would this be a candidate for an extension, or just used with a Mail hook (assuming there is one), or part of the core CiviMail?   I do not know the CiviCRM framework very well yet, but am learning more every day it seems out of necessity.

$txt is the relative url to convert.

Code: [Select]
function absolute_url($txt, $base_url){
  $needles = array('href="', 'src="', 'background="');
  $new_txt = '';
  if(substr($base_url,-1) != '/') $base_url .= '/';
  $new_base_url = $base_url;
  $base_url_parts = parse_url($base_url);

  foreach($needles as $needle){
    while($pos = strpos($txt, $needle)){
      $pos += strlen($needle);
      if(substr($txt,$pos,7) != 'http://' && substr($txt,$pos,8) != 'https://' && substr($txt,$pos,6) != 'ftp://' && substr($txt,$pos,9) != 'mailto://'){
        if(substr($txt,$pos,1) == '/') $new_base_url = $base_url_parts['scheme'].'://'.$base_url_parts['host'];
        $new_txt .= substr($txt,0,$pos).$new_base_url;
      } else {
        $new_txt .= substr($txt,0,$pos);
      }
      $txt = substr($txt,$pos);
    }
    $txt = $new_txt.$txt;
    $new_txt = '';
  }
  return $txt;
} 

Thank you for your assistance!

Craig

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviMail (Moderator: Piotr Szotkowski) »
  • EMail images using relative url instead of absolute

This forum was archived on 2017-11-26.