Build Your Own Stock Update Email

Build Your Own Stock Update Email

Receive simple stock price updates via emailon your own schedulewith a bit of Perl scripting.

If you have money in stocks, bonds, or mutual funds, you know how addictive it can be to check the current value of your investments. Some people enjoy the roller-coaster ride of watching their money ebb and flow throughout the day, while others take the long view and just want an occasional look at how their portfolio is progressing. Somewhere in the middle of these extremes lie daily stock updates. They can let you keep tabs on your portfolio on a regular basis without the ulcers caused by minute-to-minute checks.

Yahoo! Finance offers a daily update email that is easy to set up. Log in to Yahoo!, go to, and look for the Portfolios box at the top of the page. On the right side of the box, click the Customize Finance link, and then click Daily Update from the following page. From there, you can sign up for the email and set some preferences, including how often you want to receive the email: every market day or weekly.

The email itself is HTML-formatted like a web page and includes top finance stories from the day, any changes in your portfolios (if you have them set up), and announcements related to Yahoo! Finance. It's a good way to get a snapshot of the market from the day and looks like the email in Figure.

Yahoo! Finance Daily Update email

If you'd like to just get down to your stock prices, or have a bit more control over when the email is sent, you'll have to do a bit of scripting. Luckily, Yahoo! Finance gives you all of the information you need to make it happen; you just need to tie it together into a single email with a bit of Perl.

This example relies on the fact that you can look up any ticker symbol via a Yahoo! Finance URL. For example, all of the day's stock information about YHOO (the ticker symbol for Yahoo!, Inc.) is here: On each stock detail page like this one, you'll find a tiny Download Data link under the quote data. This gives you a text-only file with the relevant information for that stock in a format suitable for importing into a spreadsheet. The raw data itself is a series of comma-separated values that looks like this:


Though the intended destination for this data is a spreadsheet, you can use the same file to build your own email containing only the stock information you want to see.

The Code

This example has a few nonstandard Perl modules that you'll need to have installed before you can run it. The script uses LWP::Simple to fetch the stock data, Number::Format to make the stock prices look nice, and NET::SMTP to send the email.

You'll also need to replace a few values in the script with your own data. Set $smtp_server to your email server, $from_email to an address with permissions to send email on that server, and $to_email to the email address where you'd like to receive the stock update. And most importantly, set the list of stocks you'd like to track by adding their ticker symbols on this line:

	my @stocks = qw(insert ticker symbols separated by spaces);

So if you want to track Yahoo!, Amazon, eBay, and Microsoft as this example script does, you'll want this line to look like this:

	my @stocks = qw(YHOO AMZN EBAY MSFT);

Save the following code to a file called

	# A script to download Yahoo! Finance info about stocks
	# and send it via email
	# Usage:

	use LWP::Simple;
	use Number::Format;
	use Net::SMTP;
	use strict;

	# Set your stocks
	my @stocks = qw(YHOO AMZN EBAY MSFT);

	# Set output file
	my $file = "quick_stock_update.txt";
	open(FILE, ">$file")||die "Can't open $file";

	# Set email info
	my $subject = "Quick Stock Update";
	my $smtp_server = "insert your SMTP server";
	my $from_email = 'insert your from email';
	my $to_email = 'insert your to email';
	# Define some variables
	my $stock_symbol;
	my $last_trade_f;
	my $trade_date;
	my $change;

	# Define the file header
	format FILE_TOP=
			Quick Stock Update
	Symbol Price Date Change

	# Define the line-item details
	format FILE=
	@<<<<< @>>>> @|||||||||| @>>>>>
	$stock_symbol, $last_trade_f, $trade_date, $change

	# Loop through stocks
	foreach my $stock (@stocks) {
		my $stock_request = "".
		my $stock_data = get($stock_request);

		($stock_symbol, my $last_trade, $trade_date, my $trade_time,
			$change, my $open, my $high, my $low, my $volume)
				= split(/,/, $stock_data);

		my $x = new Number::Format(-int_curr_symbol => '');
		$last_trade_f = $x->format_number($last_trade,2,2);
		$trade_date =~ s/"//g; $trade_time =~ s/"//g;
		$stock_symbol =~ s/"//g;
		write FILE;
	# Close output file

	# Open output file for reading
	open(FILE, "$file")||die "Can't open $file";

	# Send the file in email
	my $smtp = Net::SMTP->new($smtp_server);
	$smtp->data( );
	$smtp->datasend("From: $from_email\n");	
	$smtp->datasend("To: $to_email\n");
	$smtp->datasend("Subject: $subject\n");
	$smtp->datasend('Content-Type: text/plain; charset="iso-8859-1"');
	while(<FILE>) {
	$smtp->dataend( );

	# Close output file

As you can see in the section labeled Loop through stocks, there's quite a bit more data in the Yahoo! Download Data file than this script makes use of for the email. The email sent by this script displays the ticker symbol, latest price, date of the last trade, and the change in price for the day. But you'll also find good tidbits in the data file, such as the opening price, high and low for the day, and the volume of the stock traded. This data is available for use, so if it's something you're interested it, you can tweak this script to show more.

Email is not a secure way to communicate, so be careful about what information you expose in your email messages. Think of email as a postcard that others might be able to read rather than a sealed envelope that only its intended recipient opens. An individual email can pass through several servers on the way to its destination, and there are plenty of opportunities for others to eavesdrop, so email is not the place for any sensitive financial information.

Running the Hack

To run this script once, you can just call it from the command line:


But the value of the script is in running it on a schedule you're comfortable with. Using Windows Scheduler, you can set it to run every weekday after the market closes, once a week, or once a quarter. If you really want to feel the bumps of the market, you could set this to run every hour.

The email itself is plain text, so you should see something like the message shown in Figure in your inbox.

Custom stock update email

While not nearly as aesthetically pleasing and packed with information as the standard Yahoo! Daily Update, this quick email has a certain minimalist charm. And this bit of scripting can get you exactly the information you want, when you want it.

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows