My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash

###
#
# Mailx for Gmail Command Line for Ubuntu
# Version 1.0
#
# USAGE:
# sudo ./setup_gmail_command_line.sh install
#
#
###

#
# Editable Settings
#
GMAIL_USER='YOUR_GMAIL_USER_NAME'
GMAIL_PASS='YOUR_GMAIL_USER_PASSWORD'


#
# Non-Editable Settings
#
SCRIPT=$(readlink -f $0)
SCRIPTDIR=`dirname $SCRIPT`
ARGS=("$@")
ARG_COUNT="$#"


#
# main functions
#
function parse_commandline() {
echo "parsing command line: ${ARG_COUNT} parameters"

if [ "${ARGS[0]}" == "install" ]; then
return
else
echo "to run: $SCRIPT install"
exit 1
fi
}

function preamble() {
cat <<EOB

*** Ubuntu Gmail Command Line Script ***
*** http://klenwell.com/is/UbuntuCommandLineGmail ***

SETTINGS
script : $SCRIPT
gmail user : $GMAIL_USER
gmail pass : $GMAIL_PASS

NOTES
You can edit the user and password settings by editing
this script. If you do not do this, you will need to
edit the two config files at the end of the script.
Instructions will be provided when the script at the end.

You will be asked for your sudo password. This is to
install the necessary packages.

EOB
prompt_
}

function install_mail_packages() {
sudo apt-get install msmtp heirloom-mailx
}

function install_certificate() {
sudo apt-get install ca-certificates
sudo update-ca-certificates
}

function configure_msmtp() {
cat <<EOF > ~/.msmtprc
# config options: http://msmtp.sourceforge.net/doc/msmtp.html
defaults
logfile /tmp/msmtp.log

# gmail account
account gmail
auth on
host smtp.gmail.com
port 587
user $GMAIL_USER@gmail.com
password $GMAIL_PASS
from $GMAIL_USER@gmail.com
tls on
tls_trust_file /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt

# set default account to use (not necessary with single account)
account default : gmail
EOF

chmod 600 ~/.msmtprc
}

function configure_mailx() {
cat <<EOF > ~/.mailrc
# set smtp for mailx

# gmail account (default)
# $ mailx -s "subject line" -a /path/attachment recipient@email.com < /path/body.txt
set from="$GMAIL_USER@gmail.com ($GMAIL_USER)"
set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a gmail"
EOF
}

function afterword() {

cat <<EOB

-- The necessary packages and files have now been installed.

EOB

if [ "$GMAIL_USER" == 'YOUR_GMAIL_USER_NAME' ]; then
cat <<EOB

NOTE: You still need to complete the following steps:

1. Set your email address and password in the
file ~/.msmtprc (replace CAPS):

$ gedit ~/.msmtprc

2. Set your your user name in the file
~/.mailrc (replace CAPS):

$ gedit ~/.mailrc

3. Send a test email:

$ echo -e "testing email from the command line" > /tmp/test_email
$ mailx -s "mailx gmail test" YOURNAME@gmail.com < /tmp/test_email
$ tail /tmp/msmtp.log

4. Check your email to confirm that you received it.

EOB
else
# send test email
echo "sending test email now"
mailx -s "mailx gmail test" $GMAIL_USER@gmail.com < /tmp/test_email
tail /tmp/msmtp.log

cat <<EOB

A test email has been sent to $GMAIL_USER. If successful, you
can login to your account now and find it. You can also check
the mail log:

$ tail /tmp/msmtp.log

EOB
fi
}


#
# support functions
#
function prompt_() {
read -s -n1 -p "Hit [ENTER] or [SPACE] to continue, any other key to quit " INPUT_
if [ ! -e $INPUT_ ]; then
echo -e "\nquitting now...\n"
exit 1
fi
echo -e "\n"
}


#
# main
#
parse_commandline
preamble
install_mail_packages
install_certificate
configure_msmtp
configure_mailx
afterword

Change log

r285 by klenwell on Feb 19, 2012   Diff
updates gmail command line script for
mailx
Go to: 
Project members, sign in to write a code review

Older revisions

r257 by klenwell on May 24, 2010   Diff
T619: adding mailx setup script
All revisions of this file

File info

Size: 3635 bytes, 186 lines
Powered by Google Project Hosting