9

My requirements are:

  • send an email from the terminal (for the sake of batch processing)
  • attach a pdf file to that email (the pdfs would be identical for all recipients, except for a watermark)
  • specify a reply-to address ([email protected])

I've tried the "simplest answer to sending one-line messages via gmail is to use ssmtp", and several variants, and keep getting:

laptop sSMTP[19226]: Authorization failed (535 5.7.8 https://support.google.com/mail/?p=BadCredentials d13sm3920147qkj.27 - gsmtp)

Google settings:

  • IMAP enabled
  • Allow less secure apps is ON

For each ssmtp.conf setup that I tried, I have done DisplayUnlockCaptcha just before:

$echo "Testing...1...2...3" | ssmtp [email protected]

Looking at the stated thread alone, there is no consensus as to how /etc/ssmtp/ssmtp.conf should be set up:

  • mailhub=smtp.gmail.com:587 vs 465
  • UseTLS=YES vs UseSTARTTLS=Yes (or both?)
  • hostname=localhost vs whatever was put there as the default (in my case, laptop)

Could someone make a suggestion to sort this out, and possibly paste in full a working conf file?

There is a claim in a thread from 2017, that:

You can not use external applications with your normal password, you must go to https://security.google.com/settings/security/apppasswords

Is that the case? (I'm not able to do it)

What alternatives are there?

PS:

  • OS: Linux Mint 19 Tara
  • ssmtp was tested from a clean install preceded by upgrade/update
  • a couple of years ago, I was a able to send emails using the same OS (earlier version)
  • To make sure the value of AuthPass is correct, I used it to manually log into my google account.
2
  • 1
    Your authentication with Google's smtp is failing. You have to get that right before you try to send messages. Commented Jun 27, 2020 at 19:12
  • Note at present this is required even if your account is not configured to use MFA. I tried creating a fresh account that I knew was not MFA enabled because I didn't want to have to generate an app password and I still got the same error. And, since you cannot create app passwords without MFA enabled, it looks like Google is using this requirement in an effort to combat spam/phishing (MFA uses mobile number, so links a person to the activity in many cases, or requires a would-be spammer to go to the trouble of getting access to an untraceable number if they want to remain anonymous).
    – jaredad7
    Commented Jun 1, 2021 at 18:53

6 Answers 6

9

With the following Google settings:

IMAP enabled
Allow less secure apps is ON

The solution is to set an app-password: enter image description here

Note: App passwords setup at Google require that two-factor authentication be enabled first for the account on which you are trying to configure ssmtp for.

/var/ssmtp/ssmtp.conf:

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster

# The place where the mail goes. The actual machine name is required no 
# MX records are consulted. Commonly mailhosts are named mail.domain.com
# Modified 06/27/2020:
# mailhub=mail
[email protected]
[email protected]
#AuthPass=[usual gmail pwd] # aint' gonna work
AuthPass=[pwd generated by https://myaccount.google.com/apppasswords]
#UseTLS=YES
#mailhub=smtp.gmail.com:465
UseSTARTTLS=Yes
mailhub=smtp.gmail.com:587

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=laptop

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

On the terminal:

$echo "Testing...1...2...3" | ssmtp [email protected]

Received in my gmailbox: enter image description here

PS:

  1. I don't see the usefulness of a vote-down without an explanation.
  2. For the second requirement stated in the question (attachment), mutt works like a charm.

UPDATE on 12/01/2022

Using msmtp and because less secure app disabled

  1. Turn on two step verification
  2. Generate an app password using Name='msmtp'
  3. Do $ touch ~/.msmtprc, and modify its content as follows using 2),
    # content of /home/user/.msmtprc
    # do: `$ chmod 600 /home/user/.msmtprc`
    # gmail
    account [email protected]
    host smtp.gmail.com
    port 587
    tls on
    tls_starttls on
    auth on
    user google-name
    from [email protected]
    # https://myaccount.google.com/apppasswords
    password xxxxxxxxxxxxxxxx
    # default account
    account default : [email protected]
  1. $ chmod 600 /home/user/.msmtp
  2. $ echo "Testing...1...2...3" | msmtp recipient.address@domain

This sends to bcc, look here for alternatives.

1
  • In my case instead of /var/sstmp/, /etc/sstmp/
    – Erwann
    Commented Jul 11, 2020 at 21:20
2

ssmtp is an abandoned software.

You should try msmtp instead. It looks close to ssmtp but it's more maintained and more used.

Moreover, the gmail configuration is simple. Check this good ressource on the arch wiki

1

Google no longer offer the option "Allow less secure apps is ON". After may 2022 , the option will be removed.

https://support.google.com/accounts/answer/6010255?hl=en&utm_source=google-account&utm_medium=profile-less-secure-apps-card

The option App password is not available unless you have enabled 2 step verification.

app password

Add the app ssmtp and generate token , then paste the token in app password setting.

apppwd generatetoken

Copy the token and use as password on the mail app you want to integrate

copy token

enter image description here

Next Enable IMAP on gmail

imap_enable

Note: Be sure to select SSL/TLS option and Not STARTTLS on mail client app config.

0

To answer part of the question (because I am familiar with resolving part of this problem for python), Google does have a new(ish) security feature for 3rd party apps. Essentially, you have to walk through the security page for the account that will be sending the emails and you go through the steps to set up a 3rd party authentication token. From there, you need to copy the 16-digit token and paste it as the password to enable mailing through whichever app you are trying to enable mailing in.

1
  • Your solution lacks real detail and steps/process. Please add detailed information (see the accepted answer for example).
    – number9
    Commented Oct 13, 2022 at 12:43
0

The problem is, as stated earlier, an authentication problem, however, ssmtp can still be made to work. There is a solution here.

Below is the readers digest version of the solution:

Use/configure appPassword in Google

  • Enable two factor authentication (if it isn’t already) on the gmail account you want to link.
  • In the Google account, select “Security”. Select “App Passwords” under “Signing in to Google”.
  • Make a description for your app here. I used “ssmtp on Ubuntu”.
  • Generate the 16 digit app password. Make a note of it!
  • Click done.

Now on the computer that will send the emails do sudo nano /etc/ssmtp/ssmtp.conf

[email protected]
mailhub=smtp.gmail.com:587  
FromLineOverride=YES
[email protected]
AuthPass=myAppPassword  # Note this is the password generated by google 
UseSTARTTLS=YES
UseTLS=YES

To be clear: AuthPass should be your new 16 character App Password and not your gmail password.

This worked for me on Ubuntu server 16.04 and 18.04 LTS

-1

Thank you so much, it worked for me!

To help keep your account secure, from May 30, 2022, ​​Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password...

#------------here is my configuration ssmtp.conf ---------------------

root=postmaster
root=<my_username>@gmail.com

The place where the mail goes. The actual machine name is required no MX records are consulted. Commonly mailhosts are named mail.domain.com

mailhub=smtp.gmail.com:587

Where will the mail seem to come from?

RewriteDomain=<looked_at: /etc/resolv.conf>

hostname=<looked_at: /etc/hostname>

The full hostname. Are users allowed to set their own From: address?
YES - Allow the user to specify their own From: address
NO - Use the system generated From: address

AuthUser=<my_username>@gmail.com AuthPass=<new generated 16-byte code from google>

UseSTARTTLS=YES
TLS_CA_File=/etc/ssl/certs/ca-certificates.crt
FromLineOverride=YES

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .