I believe you mail is queued. Just try mailq
and then sendmail -q
or sendmail -q -v
.
Possible is also, that email is being held because of unknown host. You need to have set localhost
and your hostname
in /etc/hosts
You normally don't need to use sendmail
from command line to send emails, because it's not very convenient. You should use mailx
or mutt
With mailx
echo you message | mail -s subject user@host
The description reads:
Being able to send emails from command-line from a server is quite
useful when you need to generate emails programatically from shell
scripts or web applications for example.
How the mail command works
For those who are curious about how exactly the mail command delivers the mails to the recipients, here is a little quick explanation.
The mail command invokes the standard sendmail binary (/usr/sbin/sendmail) which in turns connects to the local MTA to send the mail to its destination. The local MTA is a locally running smtp server that accepts mails on port 25.
mail command -> /usr/sbin/sendmail -> local MTA (smtp server) -> recipient MTA (and Inbox)
This means that an smtp server like Postfix should be running on the machine where you intend to use the mail command. If none is running you get the error message "send-mail: Cannot open mail:25".
Install Sendmail
Open terminal & type following command in terminal.
sudo apt-get install mailutils
sudo apt-get install sendmail
Configure Sendmail
After installing sendmail , you should configure sendmail. Its little hard. But don
t worry after that we can spoof email to anyone.
Type following command on terminal
sudo gedit /etc/mail/sendmail.mc
It will open sendmail.mc file.
For example your last two lines are as follows:
MAILER(`local')dnl
MAILER(`smtp')dnl
Put this code before those two lines.
MAILER_DEFINITIONS
define('SMART_HOST',`smtp.gmail.com')
Now close that file
Now we will generate configure file from .mc file so type following command in terminal.
sudo bash -c 'cd/etc/mail/ && m4 sendmail.mc >sendmai.cf'
Now everything is complete, try to send mail using terminal
Some examples from the link that I provided:
Use the mail command
Run the command below, to send an email to someone@example.com. The
s option specifies the subject of the mail followed by the recipient
email address.
$ mail -s "Hello World" someone@example.com
- Send mail to a local system user
To send mail to a local system user just use the username in place of the recipient address
$ mail -s "Hello World" username
- Specify the FROM name and address
The "-a" option allows to specify additional header information to attach with the message. It can be used to provide the "FROM" name and address. Here is a quick example
# echo "This is the message body" | mail -s "This is the subject" mail@example.com -aFrom:sender@example.com
The a option basically adds additional headers. To specify the from name, use the following syntax.
$ echo "This is the body" | mail -s "Subject" -aFrom:Harry\<harry@gmail.com\> someone@example.com
Note that we have to escape the less/great arrows since they have special meaning for the shell prompt. When you are issuing the command from within some script, you would omit that.
Sources:
What is mail?
mail command examples
Install and Configure mail
Best Answer
First of all you need to install and configure Postfix to Use Gmail SMTP on Ubuntu.
Install all necessary packages:
If you do not have postfix installed before, postfix configuration wizard will ask you some questions. Just select your server as Internet Site and for FQDN use something like mail.example.com
Then open your postfix config file:
and add following lines to it:
You might have noticed that we haven’t specified our Gmail username and password in above lines. They will go into a different file. Open/Create:
And add following line:
If you want to use your Google App’s domain, please replace @gmail.com with your @domain.com.
Fix permission and update postfix config to use sasl_passwd file:
Next, validate certificates to avoid running into error. Just run following command:
Finally, reload postfix config for changes to take effect:
Testing
Check if mails are sent via Gmail SMTP server
If you have configured everything correctly, following command should generate a test mail from your server to your mailbox.
To further verify, if mail sent from above command is actually sent via Gmail’s SMTP server, you can log into Gmail account USERNAME@gmail.com with PASSWORD and check "Sent Mail" folder in that Gmail account. By default, Gmail always keeps a copy of mail being sent through its web-interface as well as SMTP server. This logging is one strong reason that we often use Gmail when mail delivery is critical.
Troubleshooting
Error: "SASL authentication failed; server smtp.gmail.com"
You need to unlock the captcha by visiting this page https://www.google.com/accounts/DisplayUnlockCaptcha
You can run test again after unlocking captcha.
source
You need to use following syntax of
mail
andmutt
to send emails, note that if you want to send attachment file viamail
command it's not support or it's better I say I can not send my attached file viamail
command, instead you can usemutt
command line, it's very useful. and inmutt
command you have to type attachment arguments after the email address. I test it and works fine.you can install
mutt
via this command:Using
mail
Using
mutt
While
UserReport.txt
is your attachment file,MessageBody
is text/file of your body of email,TestSubject
is your email subject.-s
flag is used for "Subject" and-a
flag is used for "Attachment file"