I'm using postfix and mail from mailutils to send email using my gmail address through command line in Ubuntu. It works fine when I provide both from and to address like below:

mail -s "Some random subject" -a "From: username@gmail.com" username1@gmail.com

But I need to configure to send email from default gmail address without specifying from address everytime. How can I do that?

Please help me!


You can specify the sender address mail uses by default in your ~/.mailrc file – create it if it doesn't exist yet – with the following line:

set from="username@gmail.com"

If this helps you, please also upvote this answer where I got it from and where you can get a configuration example.

| improve this answer | |
  • Thanks @dessert, but it is not working. If I send mail using that configuration it is using system username and hostname to send email not my gmail address. – harsha May 3 '18 at 6:35
  • @harsha Really odd, the manual explicitly states The user’s file, if it exists, is always processed. and The user’s configuration file is located in the user’s home directory and is named .mailrc. – dessert May 3 '18 at 7:03
  • @harsha Oh wait, but also Each line read from these files is processed like a usual mail command. – could you try a line -a "From: username@gmail.com" in your ~/.mailrc please? – dessert May 3 '18 at 7:07
  • tried adding that line. Got this error "mail: mu_wordsplit failed: missing closing quote". – harsha May 3 '18 at 7:18

A general way of adding options you want to be default to a command-line program is an alias, see: How do I create a permanent Bash alias?
In your case this would be a line

alias mail='mail -a "From: username@gmail.com"'

added to either your ~/.bashrc or ~/.bash_aliases file.

| improve this answer | |
  • I thought of this solution, but I have different use cases. I'm using other applications as well I should be able to send email using that application commands as well. – harsha May 3 '18 at 6:42
  • @harsha If you give an option a second time it overwrites the first one for many programs, I didn't try it with mail though. – dessert May 3 '18 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.