Difference between revisions of "Injecting incoming email"

From Dreamwidth Notes
Jump to: navigation, search
(Initial submit)
 
(Add example MIME-generating script)
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
This page describes how to make a Dreamwidth install think it is receiving incoming email without actually sending it email. This might be useful if you are testing features such as post-by-email in an environment that is not able to receive email - such as a [[Dreamhack]]. The paths given below will assume that you are using a Dreamhack. If you're not, you probably know enough to correct them :-)
+
[[Category:Dreamhack]]
 +
This page describes how to make a Dreamwidth install think it is receiving incoming email without actually sending it email. This might be useful if you are testing features such as post-by-email in an environment that is not able to receive email - such as a [[Dreamhack]].
  
First, prepare the email. The easiest way is to send an email to yourself from one account to another and save the resulting message, including all headers, and then edit what you need to edit. In some clients this will be via a "view source" or "view original" link, or similar. Note that sending mail from one gmail account to another does <em>not</em> provide all the headers that you need - the email needs to actually go out to the internet and back.
+
First, prepare the email. The easiest way is to send an email to yourself from one account to another and save the resulting message, including all headers, and then edit what you need to edit. In some clients this will be via a "view source" or "view original" link, or similar. Note that sending mail from one gmail account to another does <em>not</em> provide all the headers that you need - the email needs to actually go out to the internet and back. In particular, anything without <tt>Return-path:</tt> will be dropped as spam. For more complex emails, such as emails with image attachments, it might be easier to build them from parts using something such as [http://search.cpan.org/~rjbs/MIME-Lite-3.030/lib/MIME/Lite.pm MIME::Lite], then adding/modifying the necessary headers using your favorite text editor.
  
 
<strong>Make sure that your email does not have any blank lines anywhere until the body</strong>. This includes blank lines before the start of the headers. If you did accidentally leave a blank line at the top and not notice, you might spend two and a half hours wondering why nothing worked. Ahem.  
 
<strong>Make sure that your email does not have any blank lines anywhere until the body</strong>. This includes blank lines before the start of the headers. If you did accidentally leave a blank line at the top and not notice, you might spend two and a half hours wondering why nothing worked. Ahem.  
Line 7: Line 8:
 
Anyway, save your email somewhere accessible from the Dreamwidth installation. I'll assume from here that it's in your home directory and that it's called <tt>email.txt</tt>.
 
Anyway, save your email somewhere accessible from the Dreamwidth installation. I'll assume from here that it's in your home directory and that it's called <tt>email.txt</tt>.
  
Incoming email is processed by the [[worker]] <tt>incoming-email</tt>. Start this with
+
Incoming email is processed by the <tt>incoming-email</tt> worker. Start this with
  
<pre>~/dw/bin/worker/incoming-email</pre>
+
<pre>$LJHOME/bin/worker/incoming-email</pre>
  
 
on a spare terminal. This will run until stopped by <tt>Ctrl-C</tt>, without showing any output on the screen.
 
on a spare terminal. This will run until stopped by <tt>Ctrl-C</tt>, without showing any output on the screen.
Line 15: Line 16:
 
Then, using another terminal, you can inject your test message by
 
Then, using another terminal, you can inject your test message by
  
<pre>~/dw/bin/incoming-mail-inject.pl < ~/email.txt</pre>
+
<pre>$LJHOME/bin/incoming-mail-inject.pl < ~/email.txt</pre>
  
 
Hopefully, everything worked! If you are debugging something in the email-handling code, remember that you will need to restart the <tt>incoming-email</tt> worker after you change things to make sure that it is running the latest code.  
 
Hopefully, everything worked! If you are debugging something in the email-handling code, remember that you will need to restart the <tt>incoming-email</tt> worker after you change things to make sure that it is running the latest code.  
Line 22: Line 23:
 
* Make sure that posting by email is enabled at http://www.yourname.hack.dreamwidth.net/manage/settings/?cat=mobile, with a permitted sender and a PIN. Make sure that your <tt>From:</tt> header is the permitted sender, and that the PIN is featured in one of the allowed places. Unless you are testing what happens when it isn't ;-)
 
* Make sure that posting by email is enabled at http://www.yourname.hack.dreamwidth.net/manage/settings/?cat=mobile, with a permitted sender and a PIN. Make sure that your <tt>From:</tt> header is the permitted sender, and that the PIN is featured in one of the allowed places. Unless you are testing what happens when it isn't ;-)
 
* Make sure that your successive test emails do not have the same date. There's some code intended to do rate-limiting that will drop successive emails that have less than five minutes between their <tt>Date:</tt> headers. This may not matter if you are restarting the worker each time - I haven't checked.
 
* Make sure that your successive test emails do not have the same date. There's some code intended to do rate-limiting that will drop successive emails that have less than five minutes between their <tt>Date:</tt> headers. This may not matter if you are restarting the worker each time - I haven't checked.
 +
* If you want to make a post or comment by email, be aware that in the default configuration, free users are unable to do this. You will need to either use http://www.yourname.hack.dreamwidth.net/admin/pay/ to give yourself a different account type, or edit the <code>%CAP</code> hash in the <tt>$LJHOME/etc/config.pl</tt> file to set the <code>emailpost</code> capability to 1 for free users.
 +
* If you're wanting to inject email intended for the support board, you'll need to make sure that the address you use in the "To" (or "Cc") header is defined as the reply address for one of the support categories. You can do this by going to http://www.yourname.hack.dreamwidth.net/admin/supportcat/ (giving yourself the '''supportadmin:support''' [[privs|priv]] if necessary), editing the category, and entering a valid email address into the "Reply address" field.
 +
* For some maybe-useful diagnostics, try logging into your hack's System account and looking at http://www.yourname.hack.dreamwidth.net/admin/theschwartz
 +
 +
The following script may be useful in building an email file with attachments. Don't forget after using it to edit the file to make sure all headers, including Return-path:, are present and correct.
 +
 +
<code><pre>#!/usr/bin/perl
 +
 +
# Typical use: message_with_attachment attachment_file > MIME_message_file
 +
 +
use strict;
 +
use warnings;
 +
use MIME::Lite;
 +
use File::Type;
 +
 +
die "Wrong number of arguments.\n" if @ARGV != 1;
 +
die "'$ARGV[0]' can't be empty or only spaces.\n" if $ARGV[0] =~ /^\s*$/;
 +
die "'$ARGV[0]' must be in the current directory.\n" if $ARGV[0] =~ /\//;
 +
 +
my $ft = File::Type->new;
 +
my $type = $ft->checktype_filename( $ARGV[0] );
 +
die "Can't determine type of $ARGV[0].\n" unless defined $type;
 +
$type = 'image/png' if $type eq 'image/x-png';
 +
 +
my $msg = MIME::Lite->new( From => 'me@example.com', To => 'you@example.net', Subject => 'A subject line', Type => 'multipart/mixed' );
 +
$msg->attach( Type => 'TEXT', Data => 'A text body line' );
 +
$msg->attach( Type => $type, Path => $ARGV[0], Filename => $ARGV[0], Disposition => 'attachment' );
 +
$msg->print( \*STDOUT );</pre></code>

Latest revision as of 04:01, 17 January 2018

This page describes how to make a Dreamwidth install think it is receiving incoming email without actually sending it email. This might be useful if you are testing features such as post-by-email in an environment that is not able to receive email - such as a Dreamhack.

First, prepare the email. The easiest way is to send an email to yourself from one account to another and save the resulting message, including all headers, and then edit what you need to edit. In some clients this will be via a "view source" or "view original" link, or similar. Note that sending mail from one gmail account to another does not provide all the headers that you need - the email needs to actually go out to the internet and back. In particular, anything without Return-path: will be dropped as spam. For more complex emails, such as emails with image attachments, it might be easier to build them from parts using something such as MIME::Lite, then adding/modifying the necessary headers using your favorite text editor.

Make sure that your email does not have any blank lines anywhere until the body. This includes blank lines before the start of the headers. If you did accidentally leave a blank line at the top and not notice, you might spend two and a half hours wondering why nothing worked. Ahem.

Anyway, save your email somewhere accessible from the Dreamwidth installation. I'll assume from here that it's in your home directory and that it's called email.txt.

Incoming email is processed by the incoming-email worker. Start this with

$LJHOME/bin/worker/incoming-email

on a spare terminal. This will run until stopped by Ctrl-C, without showing any output on the screen.

Then, using another terminal, you can inject your test message by

$LJHOME/bin/incoming-mail-inject.pl < ~/email.txt

Hopefully, everything worked! If you are debugging something in the email-handling code, remember that you will need to restart the incoming-email worker after you change things to make sure that it is running the latest code.

Things to check if your emails are not doing anything useful:

  • Make sure that posting by email is enabled at http://www.yourname.hack.dreamwidth.net/manage/settings/?cat=mobile, with a permitted sender and a PIN. Make sure that your From: header is the permitted sender, and that the PIN is featured in one of the allowed places. Unless you are testing what happens when it isn't ;-)
  • Make sure that your successive test emails do not have the same date. There's some code intended to do rate-limiting that will drop successive emails that have less than five minutes between their Date: headers. This may not matter if you are restarting the worker each time - I haven't checked.
  • If you want to make a post or comment by email, be aware that in the default configuration, free users are unable to do this. You will need to either use http://www.yourname.hack.dreamwidth.net/admin/pay/ to give yourself a different account type, or edit the %CAP hash in the $LJHOME/etc/config.pl file to set the emailpost capability to 1 for free users.
  • If you're wanting to inject email intended for the support board, you'll need to make sure that the address you use in the "To" (or "Cc") header is defined as the reply address for one of the support categories. You can do this by going to http://www.yourname.hack.dreamwidth.net/admin/supportcat/ (giving yourself the supportadmin:support priv if necessary), editing the category, and entering a valid email address into the "Reply address" field.
  • For some maybe-useful diagnostics, try logging into your hack's System account and looking at http://www.yourname.hack.dreamwidth.net/admin/theschwartz

The following script may be useful in building an email file with attachments. Don't forget after using it to edit the file to make sure all headers, including Return-path:, are present and correct.

#!/usr/bin/perl

# Typical use: message_with_attachment attachment_file > MIME_message_file

use strict;
use warnings;
use MIME::Lite;
use File::Type;

die "Wrong number of arguments.\n" if @ARGV != 1;
die "'$ARGV[0]' can't be empty or only spaces.\n" if $ARGV[0] =~ /^\s*$/;
die "'$ARGV[0]' must be in the current directory.\n" if $ARGV[0] =~ /\//;

my $ft = File::Type->new;
my $type = $ft->checktype_filename( $ARGV[0] );
die "Can't determine type of $ARGV[0].\n" unless defined $type;
$type = 'image/png' if $type eq 'image/x-png';

my $msg = MIME::Lite->new( From => 'me@example.com', To => 'you@example.net', Subject => 'A subject line', Type => 'multipart/mixed' );
$msg->attach( Type => 'TEXT', Data => 'A text body line' );
$msg->attach( Type => $type, Path => $ARGV[0], Filename => $ARGV[0], Disposition => 'attachment' );
$msg->print( \*STDOUT );