Attach a file with command line argument

Hello,
is there a way to attach a file by running mailspring with a command line argument ? I.e. something like

mailspring --attach “file.jpg”

or via some kind of d-bus interface?

If there is none, I’ll open a feature request: this feature would allow one to set easily setup a desktop service, like right-click on a file->share->attach to email?

I don’t know of any such option DBus interface. Digging through documentation and issue reports for the last couple of weeks, I’ve seen no mention of it. Probably worthy of a feature request, or maybe a plugin if you’re feeling adventurous!

This should already work using the following command:

mailspring file.jpg

Can you please check if this works for you @paulatz?

It does work actually, I just found out, but only if the argument is a path, i.e.:

mailspring file.jpeg # does NOT work
mailspring /home/paulatz/file.jpeg # works
mailspring ./file.jpg #also works

I can live with this, but I would have never found it on my own.

Thanks for checking. I will add this to the help method when mailspring is called from the terminal. I think this is the best place for this information.

See:

Thanks for the hint. Actually, as we are here. Is there also a way to send an email to a certain recipient from command line?
Again, this would allow to click on a mailto: field in a browser or other program and have it call a mailspring compose windows pre-filled.

In the meantime, you can pack in the linux package a file desktop like this that goes in /usr/share/kservices5/mailspring.desktop:

[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=KonqPopupMenu/Plugin
MimeType=application/octet-stream;
Actions=mailspringAttach
[Desktop Action mailspringAttach]
Name=Share with Mailspring
Icon=mail-attachment
Exec=/usr/bin/mailspring %F

And another one line this that goes in /usr/share/applications/mailspring-share.desktop

Name=Share with Mailspring
Exec=/usr/bin/mailspring %F
Icon=mail-attachment
Terminal=false
Type=Application
Encoding=UTF-8
StartupNotify=false
MimeType=application/octet-stream;
NoDisplay=true
NotShowIn=KDE

They should enable a “Share with Mailspring” action when right-clicking on a file (I have not tested the latter)

I’ve checked what is the strategy of other mail programs to manage command lines, and they are very different. I.e. thunderbird wants all in a single argument:

thunderbird -compose ‘to=dest@mail.com,attach=file://file1.jpeg,file://file2.png’

kmail takes several arguments:

kmail dest@mail.com --attach file.jpeg

but if there is only an argument and it contains no “@” it is considered an attachment (and if it contains an “@” it is considered a mail address, even if a file with that name exists)

Yes. You can run: mailspring mailto:johndoe@example.com. Unfortunately, it is currently not possible to combine the “mailto” parameter and an attachment. This will open two seperate e-mails.

I will check out the sharing option(s) in the desktop file.

I see, I think it is not very important to include my desktop file, as everything will work by using already built-in mechanism if mailspring can understand an argument like this to attach a file:

mailto:?attachment=file:///home/paulatto/files.pdf

I have a small script that manipulates the standard mailto: URL, converts it to a file name with full path if there is an attachment and then calls mailspring, and this works in every case without the need of extra desktop file.

thank you for your support

#!/bin/sh
# this is /usr/bin/mailspring
newarg=""
while [ $# -gt 0 ]; do
  if echo "$1" |grep -q "mailto:.*attachment=.*" ;then
     # If there is an address and an attachment, I throw away the address
     #echo "1: $1"
     arg=$(echo $1|sed -e "s/.*attachment=file:\/\///")
  elif echo "$1" |grep -q "mailto:.*@.*" ;then
     #echo "2: $1"
     arg="$1"
  else
     #echo "3: $1"
     arg=$(realpath $1)
  fi
  newarg="$newarg \"$arg\""
  shift
done

echo "calling /usr/share/mailspring/mailspring $newarg"
eval /usr/share/mailspring/mailspring $newarg

The PR updating the command line docs has been merged and will be included in the next release.

Unfortunately, there is currently no easy way to open an e-mail with a recipient and an attachment at the same time.