Skip to content

Commit

Permalink
Options to send email in the background.
Browse files Browse the repository at this point in the history
Use fork to send email and launch the watcher in the background, in required.
  • Loading branch information
venkytv committed Apr 19, 2015
1 parent 1d40300 commit d3af1be
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Simple script to manage emails sent using msmtp while offline. It is modeled af

Requires ruby. (This was developed on version 2.2.0, but the script *should* run on anything greater than 1.9.3. Has not been tested, though.)

#### Installation
#### Installation and Usage

(This script is primarily designed for use with `mutt`, but does not have any hard dependencies on it. Usage instuctions below apply only to `mutt`, though.)

Expand All @@ -21,6 +21,29 @@ Requires ruby. (This was developed on version 2.2.0, but the script *should* ru
set sendmail_wait = -1
```

If you need the script to do the sending in the background (necessary for use with Emacs, as I understand), pass the `+fork` option:
```
/path/to/msmtp-offline +fork <any-msmtp-options>
```

Or if you prefer environment variables, set the `FORK_SEND` environment variable:
```
FORK_SEND=1 /path/to/msmtp-offline <any-msmtp-options>
```

If you want to disable to watcher which periodically tests network accessibility, pass the `+nowatcher` option:
```
/path/to/msmtp-offline +nowatcher <any-msmtp-options>
```

Or set the `NO_WATCHER` environment variable.

If the watcher is disabled, msmtp-offline will only attempt to flush any queued
messages the next time you send an email. Of course, you can always use
`msmtp-queue` to flush queued mails manually.

__IMPORTANT__: msmtp-offline options (starting with '+') need to be specified *before* any `msmtp` options.

#### Known Issues

* Uses `flock`, which has known issues with NFS-mounted directories. If your home directory is NFS-mounted, your milage may vary.
47 changes: 41 additions & 6 deletions msmtp-offline
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ QDIR = ENV['QDIR'] || File.join(ENV['HOME'], '.msmtp-offline.queue')
LOGFILE = ENV['LOGFILE'] || File.join(ENV['HOME'], 'log', 'msmtp-offline.log')
QFLUSH_INTERVAL = 30

$NO_WATCHER = ENV['NO_WATCHER'] || false
$FORK_SEND = ENV['FORK_SEND'] || false

$logger = Logger.new(LOGFILE)
$logger.level = (ENV['DEBUG'] ? Logger::DEBUG : Logger::INFO)
$logger.progname = 'flusher'
Expand Down Expand Up @@ -226,6 +229,28 @@ def launch_watcher
end
end

def launch_sender(id)
# Attempt to flush the mail right away
sendmail(id)

# Attempt to flush all queued mail
list_queue.each {|i| sendmail(i)}
end

def launch(action, proc)
if $FORK_SEND
if not Process.respond_to?(:fork)
$logger.warn { "Fork not supported. #{action} in-process." }
proc.call
else
$logger.info { "#{action} in the background." }
fork { proc.call }
end
else
proc.call
end
end

#-------------- MAIN ---------------

if (ARGV[0] == '+q')
Expand Down Expand Up @@ -264,6 +289,18 @@ if (ARGV[0] == '+q')
else
# Sendmail-compatible mode

# Consume msmtp-offline parameters (with a '+' prefix)
while (ARGV.first.start_with?('+'))
arg = ARGV.shift
if (arg == '+fork')
$FORK_SEND = true
elsif (arg == '+nowatcher')
$NO_WATCHER = true
else
$logger.warn { "Ignoring unknown argument: #{arg}" }
end
end

Dir.mkdir(QDIR) unless Dir.exists? QDIR

t = Time.new
Expand All @@ -290,13 +327,11 @@ else

(host, port) = smtp_host_port(ARGV)
if accessible?(host, port)
# Attempt to flush the mail right away
sendmail(qfilebase)

# Attempt to flush all queued mail
list_queue.each {|id| sendmail(id)}
launch('Sending email', Proc.new { launch_sender(qfilebase) })
else
$logger.info { "Server inaccessible (#{host}:#{port}). Queueing message: #{qfilebase}" }
launch_watcher
unless $NO_WATCHER
launch('Starting watcher', Proc.new { launch_watcher })
end
end
end

0 comments on commit d3af1be

Please sign in to comment.