how to send mails from CLI via gmail

Hello Community,

I am new to opensuse but have plenty of experience in ubuntu and mandriva.

I am looking for a way of sending emails from CLI via my gmail account.

in ubuntu I use mailx+exim4. in mandriva i use msmtp

but in opensuse i cant install exim4, and the installation of msmtp has a completely different architecture and i cant find the configuration file i modified in my mandriva computer to send mails via gmail.

Does anyone know of a simple and quick installation of some MTA in opensuse which can satisfy my need?

Thanks

Yan

I don’t know a quick and simple way, but you can configure postfix to relay mails with a sender address ending in @gmail.com via gmail. There are lots of tutes to do this. The reason it’s not straightforward is it involves configuring TLS.

If you search this forum, I seem to recall I might have described the changes in /etc/sysconfig/postfix to accomplish this.

yanvolking wrote:
> I am looking for a way of sending emails from CLI via my gmail
> account.

have you tried Alpine…i’d guess it would work and appear the same on
most all distros…then when you jump from one to the other you
wouldn’t have to shop for a new email client…


palladium

On 02/18/2010 06:46 AM, yanvolking wrote:
>
> Hello Community,
>
> I am new to opensuse but have plenty of experience in ubuntu and
> mandriva.
>
> I am looking for a way of sending emails from CLI via my gmail
> account.
>
> in ubuntu I use mailx+exim4. in mandriva i use msmtp
>
> but in opensuse i cant install exim4, and the installation of msmtp has
> a completely different architecture and i cant find the configuration
> file i modified in my mandriva computer to send mails via gmail.
>
> Does anyone know of a simple and quick installation of some MTA in
> opensuse which can satisfy my need?

Add the following stanza to ~/.mailrc

account gmail {
set smtp-use-starttls
set smtp=smtp://smtp.gmail.com:587
set smtp-auth=login
set smtp-auth-user=your-user-name@gmail.com
set smtp-auth-password=your-password
set from=“the return address you want”
}

To send mail, use

mail -A gmail -s “test 1” larry < /dev/null

The above command sent an empty mail message with subject “test 1” to the
nickname larry in .mailrc.

Hi IwFinger,

Thanks so much for your suggestion:

Add the following stanza to ~/.mailrc

account gmail {
set smtp-use-starttls
set smtp=smtp://smtp.gmail.com:587
set smtp-auth=login
set smtp-auth-user=your-user-name@gmail.com
set smtp-auth-password=your-password
set from=“the return address you want”
}

I can now send emails directly from CLI command using mail without the messages being labeled as spam.

Have a great day.

Yan

I wrote a script to configure Postfix on openSUSE in order to use a smarthost for external mails (only). It’s not more difficult than configuring exim or sendmail (I did to). I would say it’s even a little bit easier. I don’t know if you can use gmail for that purpose and I don’t know if this script will work for you, probably not (and I might have forgotten one or two variables or left some syntax errors, since it is just a snip of a much larger script). But it might show you what settings to apply and where.

The purpose of these changes is to bring sendmail to use an external smtp for Internet mails (wether you use it from CLI or from a mail client) while using you own smtp (= your computer) inside your lan and local transport for system mails. I don’t know if it that’s what you want.
Good luck.

  • in openSUSE sendmail = postfix
#! /bin/bash

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# !!! define this variables first !!!
myhost=toto
mydomain=dummy.net
mylan="192.168.0.0
enveloppe=gmail.com
admin2add="jim jack joe" 
user2add="jim jack joe" 
smarthost=smtp.gmail.com


# Do not change (should be OK) 
senmdailcf=/etc/postfix/main.cf
 
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


#patching /etc/sysconfig/mail
grep -q 'SMTPD_LISTEN_REMOTE="no"' /etc/sysconfig/mail  && cat << EOFSYSCONFIFMAIL | patch -p0
--- /etc/sysconfig/mail.orig	2009-05-25 05:09:59.000000000 -0700
+++ /etc/sysconfig/mail	2009-05-25 05:10:13.000000000 -0700
@@ -29,7 +29,7 @@
 # If set to "no" or empty then only mail from localhost
 # will be accepted.
 #
-SMTPD_LISTEN_REMOTE="no"
+SMTPD_LISTEN_REMOTE="yes"

 
 ## Type:        yesno
 ## Default:     no
EOFSYSCONFIFMAIL


# configuring postfix
echo "*** configuring postfix..."

echo " - creating /etc/postfix/sender_canonical.db ..."
admin=${admin2add%% *}
printf "root
%s
%s
%s
" $user2add | awk 'BEGIN { EMAIL = "@'"$myhost.$mydomain"'" ; printf "root    '"$admin@$enveloppe"'
" } ; { if ($1 == "root") print $1 EMAIL, "	'"$admin@$enveloppe"'" ; else print $1 EMAIL, "	"  $1"@'"$enveloppe"'"}' > /etc/postfix/sender_canonical

/usr/sbin/postmap /etc/postfix/sender_canonical
echo " - creating /etc/postfix/transport.db ..."

cat > /etc/postfix/transport << EOFTRANSPORT
$myhost.$mydomain        local:
localhost.$mydomain      local:
$mydomain               : 
.$mydomain              :
*                       smtp:${smarthost}]
EOFTRANSPORT
/usr/sbin/postmap /etc/postfix/transport

if  ! -f $sendmailcf.orig ] ; then
	echo " - patching $sendmailcf ..."
	cat << EOFPOSTFIXMAIN | patch -b -p0
--- /etc/postfix/main.cf.orig	2009-11-12 14:40:45.000000000 -0800
+++ /etc/postfix/main.cf	2009-11-27 07:55:02.000000000 -0800
@@ -420,7 +420,7 @@
 # "Maildir/" for qmail-style delivery (the / is required).
 #
 #home_mailbox = Mailbox
-home_mailbox = Maildir/
+#home_mailbox = Maildir/
  
 # The mail_spool_directory parameter specifies the directory where
 # UNIX-style mailboxes are kept. The default setting depends on the
@@ -699,11 +699,12 @@
 program_directory = /usr/lib/postfix
 inet_interfaces = all
 masquerade_domains = 
-mydestination = \$myhostname, localhost.\$mydomain
+mydestination = \$myhostname, localhost.\$mydomain, localhost
 defer_transports = 
-mynetworks_style = subnet
+mynetworks = ${mylan}/24, 127.0.0.0/8
 disable_dns_lookups = no
 relayhost = 
+transport_maps = hash:/etc/postfix/transport
 content_filter = 
 mailbox_command = 
 mailbox_transport = 
EOFPOSTFIXMAIN
fi

  -f /etc/mail/aliases.db ] || newaliases
	
# restarting postfix
/etc/rc.d/postfix start || /etc/rc.d/postfix reload

echo "sendmail successfully configured" | mail root