0

I have a newsletter mailer that sends emails automaticly, however ive found a bug in it. I looked at the newsletter and noticed that colors were messed up and after debugging, i found that it was the link replace function that is causing this, somehow the str_replace is replacing correctly, but it also changes the hashtag of the color like this with a link, and is in places where does not exist a href link:

style="font-size: 8pt;  color: https://etcetc.com"

It's an odd situation, as it is replacing all links correctly, but however seems like the str_replace replaces all color:; values with the links aswell, and it was not supposed.

Ive tried putting old link and new link into an array to see if there were any of thoose colors in there, however in the array the links appear correctly with old one and new one to replace.

Ive tried instead of replacing in the foreach, passing the old link and new link to a new array and parsing from there but same outcome.

 // Trocar links para contabilitizar | Change links to parser
 $html = $corpo; // This contains the HTML of the newsletter
 //Create a new DOM document
 $dom = new DOMDocument;

 //Parse the HTML. The @ is used to suppress any parsing errors
 //that will be thrown if the $html string isn't valid XHTML.
 @$dom->loadHTML($html);

 //Get all links. You could also use any other tag name here,
 //like 'img' or 'table', to extract other tags.
 $links = $dom->getElementsByTagName('a');

 //Iterate over the extracted links and display their URLs
 foreach($links as $link) {

     $old_link = $link->getAttribute('href');
     $new_link = $this->url."/scripts/link.php?link=".$link->getAttribute('href')."&campaign_id=".$cid;
     $html = str_replace($old_link, $new_link, $html);

 }

 $corpo = $html;
 print_r($corpo);

Example of the newsletter HTML, original : https://codepen.io/gx19/pen/XwdaMN I removed image links for security reasons

Here is what this function is doing to the html: https://codepen.io/gx19/pen/byproX

FIX: (A huge thanks to @LucaRainone ) I had to check if the old url was actually an URL otherwise it would replace color hashtag's.

//Iterate over the extracted links and display their URLs
            foreach ($links as $link){

                $old_link = $link->getAttribute('href');
                $new_link = $this->url."/scripts/link.php?link=".$link->getAttribute('href')."&campaign_id=".$cid;

                if (strpos($old_link, 'https://') !== false OR strpos($old_link, 'http://') !== false) {
                  $html = str_replace($old_link,$new_link,$html);
                }

            }
G19
  • 3
  • 5
  • What kind of color is `color: https://etcetc.com`? – Markus Zeller May 10 '19 at 11:40
  • @MarkusZeller That is what the foreach function is doing, replaces the color hashtag with links aswell randomly... – G19 May 10 '19 at 11:41
  • Ah, I see. Can you post the original HTML (or a link to that) to replay? – Markus Zeller May 10 '19 at 11:43
  • 2
    I haven't written PHP in a dog's age, but the first thing I'd do is stop suppressing the errors. – The Head Rush May 10 '19 at 11:43
  • @MarkusZeller Ive added 2 code pen to the post one before the function, other after , as you can see it randomly changes the color styles. – G19 May 10 '19 at 11:55
  • @TheHeadRush Im only suppresing the loadHtml, as it's not for a div unclosed that it should not mail the newsletter.. However with mine even unspupressed no errors show – G19 May 10 '19 at 11:57
  • 1
    I suppose that sometimes you have `link` So the url catched is `#`. Then you replace all `#` with url proxy (catching also the color styles in rgb format). You have to ensure that the url catched is a real url (starts with "http" or with "/") before replace them. Otherwise is better to change approach using a preg_replace and a regexp like this https://stackoverflow.com/questions/43814906/regex-to-match-anchor-tag-and-its-href – Luca Rainone May 10 '19 at 12:11
  • have you tried "preg_replace". see this https://stackoverflow.com/questions/13579641/php-str-replace-hex-color – Nit May 10 '19 at 12:15
  • @LucaRainone You nailed it !!! Im convinced now , ill try that now , but im sure that ill fix it !! Ill comment back once tested – G19 May 10 '19 at 13:06

0 Answers0