title: Embedding images in HTML e-mails–replacing "src" attribute of <img>
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3820666/grabbing-the-href-attribute-of-an-a-element">Grabbing the href attribute of an A element</a>  </p>
  </blockquote>
  
  
  
  <p>I have a cms where clients can make their own HTML newsletters. These are send to selected receivers with Swift mailer. Included in these newsletters are images that are linked to online sources. I want to embed these images in the e-mail itself, a feature that Swift mailer supports.</p>
  
  <p>What I like to accomplish is that clients can create their own newsletter including linking to the images. When sending this linked images must be replaced by the code necessary to embed them in the e-mail. Therefor I need to replace the source of the images:</p>
  
  <pre><code>&lt;img src="http://source/to/file.gif" /&gt;
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>&lt;img src="{$message-&gt;embed(Swift_Image::fromPath('http://http://source/to/file.gif'))}" /&gt;
  </code></pre>
  
  <p>The HTML content, including the linked images, is stored in a variable $content. I tried to replace the src with:</p>
  
  <pre><code>$content = preg_replace(
      '/&lt;img.*src="(.*?)".*?&gt;/',
      '&lt;img src="' . $message-&gt;embed(Swift_Image::fromPath('\1"')) . '" /&gt;',
      $content);
  </code></pre>