<commit_before><?php
/**
 * Embeds a tracker gif in emails before sending to allow view tracking.
 *
 * @package silverstripe-newsletter-tracking
 */
class NewsletterEmailViewTrackingExtension extends Extension {

	public function onBeforeSend() {
		if (!$to = $this->owner->To()) {
			return;
		}

		$member = DataObject::get_one('Member', sprintf(
			'"Email" = \'%s\'', Convert::raw2sql($to)
		));

		if (!$member) {
			return;
		}

		$body = $this->owner->Body()->forTemplate();
		$pos  = strrpos($body, '</body>');

		$img = sprintf('<img src="%s" alt="" width="1" height="1">', Controller::join_links(
			Director::absoluteBaseURL(),
			'newsletter-view',
			$this->owner->Newsletter()->Token,
			"$member->NewsletterTrackingToken.gif"
		));

		if ($body) {
			$body = substr($body, 0, $pos) . $img . substr($body, $pos);
		} else {
			$body .= $img;
		}

		$this->owner->setBody(DBField::create('HTMLText', $body));
	}

}
<commit_msg>BUGFIX Fixed typo where $pos should have been used for checking where to insert image into body content<commit_after><?php
/**
 * Embeds a tracker gif in emails before sending to allow view tracking.
 *
 * @package silverstripe-newsletter-tracking
 */
class NewsletterEmailViewTrackingExtension extends Extension {

	public function onBeforeSend() {
		if (!$to = $this->owner->To()) {
			return;
		}

		$member = DataObject::get_one('Member', sprintf(
			'"Email" = \'%s\'', Convert::raw2sql($to)
		));

		if (!$member) {
			return;
		}

		$body = $this->owner->Body()->forTemplate();
		$pos  = strrpos($body, '</body>');

		$img = sprintf('<img src="%s" alt="" width="1" height="1">', Controller::join_links(
			Director::absoluteBaseURL(),
			'newsletter-view',
			$this->owner->Newsletter()->Token,
			"$member->NewsletterTrackingToken.gif"
		));

		if ($pos) {
			$body = substr($body, 0, $pos) . $img . substr($body, $pos);
		} else {
			$body .= $img;
		}

		$this->owner->setBody(DBField::create('HTMLText', $body));
	}

}

{'fraction_non_alphanumeric': 0.1993911719939117, 'fraction_numerical': 0.004058853373921867, 'mean_word_length': 4.00507614213198, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1560294', 'n_tokens_mistral': 750, 'n_tokens_neox': 671, 'n_words': 147}