Nat TaylorBlog, AI, Product Management & Tinkering

Customizing Website Subscription Emails

Published on .

Swappa.com allows users to subscribe to listings via email, but the messages only contain a link and omit useful information like the price.

I wanted to use the email notifications to generate better email notifications, and cooked up the following:

  1. Forward the email to a separate dedicated email account
  2. Apply a filter to the messages and pipe it to a script
  3. Parse useful information out of the email
  4. Generate and send a new email

This was a relatively easy project and as usual a useful learning exercise.

Note: At BlueHost.com, the email filter’s “pipe to a program” destination needs to be $home/script.php and chmod 644 script.php (assuming its in your home directory.)

My ugly script is below.  It’s fragile with no error checking or DomDocumenet node validation.

Here’s what the emails look like before and after.

#!/usr/bin/php -q
<?php
libxml_use_internal_errors(true);

while($f = fgets(STDIN)){
    if(preg_match('/^https:\/\/swappa.com\/listing\/.*?\/view/', $f)) {
    	
    	$ch = curl_init(trim($f));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 (compatible; NTWD/0.1; +https://nattaylor.com/)");
		$response = curl_exec($ch);
    	curl_close($ch);
		
		$response = mb_convert_encoding($response, 'HTML-ENTITIES', 'UTF-8');
		$doc = new DOMDocument();
		$doc->loadHTML( '<?xml version="1.0" encoding="UTF-8"?>'.$response );
	
		if( !isset( $doc->documentElement ) ) {
			return false;
		}

		$title = $doc->getElementsByTagName("title")->item(0)->textContent;
		$h1 = trim(str_replace("\n"," ",str_replace("\t","",$doc->getElementsByTagName("h1")->item(0)->textContent)));
		$h2 = trim($doc->getElementsByTagName("h2")->item(0)->textContent);

		$storage = trim($doc->getElementsByTagName("table")->item(0)->childNodes->item(1)->childNodes->item(6)->textContent);
		$color = trim($doc->getElementsByTagName("table")->item(0)->childNodes->item(1)->childNodes->item(8)->textContent);
		$condition = trim($doc->getElementsByTagName("h1")->item(0)->childNodes->item(1)->childNodes->item(1)->textContent);
		$price = trim($doc->getElementsByTagName("h1")->item(0)->childNodes->item(3)->textContent);

		
		foreach( $doc->getElementsByTagName("li") as $node ) {
			if( strpos( $node->textContent, "Device:" ) ) {
				$device = trim(str_replace("Device: ", "", $node->textContent));
			}
		}
		
		
		$emailto = 'nattaylor@gmail.com';
		$toname = 'Nat Taylor';
		$emailfrom = 'swappa@nattaylor.com';
		$fromname = 'Nat Taylor';
		$subject = "$".$price." for ".$storage.", ".$condition.", ".$color." ".$device;
		$messagebody = implode("\n", array($h1, $h2, $f));
		$headers = 
			'Return-Path: ' . $emailfrom . "\r\n" . 
			'From: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" . 
			'X-Priority: 3' . "\r\n" . 
			'X-Mailer: PHP ' . phpversion() .  "\r\n" . 
			'Reply-To: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" .
			'MIME-Version: 1.0' . "\r\n" . 
			'Content-Transfer-Encoding: 8bit' . "\r\n" . 
			'Content-Type: text/plain; charset=UTF-8' . "\r\n";
		$params = '-f ' . $emailfrom;
		$send = mail($emailto, $subject, $messagebody, $headers, $params);
    }
}
?>

Post Navigation

«
»