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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/bin/bash

###
#
# Simple Ubuntu Command Line for Gmail
# Nail Version 1.0
#
#
# USAGE:
# chmod 755 ./setup_gmail_command_line.sh
# sudo ./setup_gmail_command_line.sh install
#
# Update GMAIL_USER and GMAIL_PASS below for better automation. Do not
# include @gmail.com with user name.
#
###

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


#
# Non-Editable Settings
#
TODAY=`date +%Y%m%d`
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 check_nail_in_repo() {
PKG=nail
sudo apt-cache showpkg $PKG | grep --silent "Package: $PKG"
HAS_PKG_EQ_ZERO=$?

if [[ "$HAS_PKG_EQ_ZERO" -ne "0" ]]; then
cat <<EOB
nail was not found in your repositories. To complete this script, you
will need to enable a repository with nail in it. A simple way:

$ echo -e "\n# Dapper Universe for nail\ndeb http://us.archive.ubuntu.com/ubuntu dapper main universe\n" | sudo tee -a /etc/apt/sources.list
$ sudo apt-get update
$ sudo apt-cache showpkg nail
EOB
exit 1
fi
}

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

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

function configure_msmtp() {

MSMTP_PATH=~/.msmtprc

if [[ -e $MSMTP_PATH ]]; then
echo "Your config file $MSMTP_PATH will be backed up to $MSMTP_PATH-$TODAY and overwritten"
prompt_
mv $MSMTP_PATH $MSMTP_PATH-$TODAY
fi

touch /tmp/msmtp.log
cat <<EOF >$MSMTP_PATH
# config options: http://msmtp.sourceforge.net/doc/msmtp.html#A-user-configuration-file
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

# isp account (change caps below)
account isp
auth login
host SMTP.YOURISP.COM
port 25
user YOURNAME@ISP.COM
from YOURNAME@ISP.COM
password YOURPASSWORD

# set default account to use (from above)
account default : gmail
EOF

chmod 600 $MSMTP_PATH
}

function configure_nail() {

MAILRC_PATH=~/.mailrc

if [[ -e $MAILRC_PATH ]]; then
echo "Your config file $MAILRC_PATH will be backed up to $MAILRC_PATH-$TODAY and overwritten"
prompt_
mv $MAILRC_PATH $MAILRC_PATH-$TODAY
fi

cat <<EOF >$MAILRC_PATH
# set smtp for nail

# gmail account (default)
# $ nail -s "subject line" -a /path/file 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"

# isp account (add -A option to command line)
# $ nail -A isp -s "subject line" -a /path/file recipient@email.com < /path/body.txt
account isp {
set from="YOURNAME@ISP.COM"
set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a isp"
}
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
$ nail -s "nail 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"
echo -e "testing email from the command line" > /tmp/test_email
nail -s "nail gmail test" $GMAIL_USER@gmail.com < /tmp/test_email
tail /tmp/msmtp.log

cat <<EOB

A test email has been sent to $GMAIL_USER@gmail.com . 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
check_nail_in_repo
install_mail_packages
install_certificate
configure_msmtp
configure_nail
afterword

Change log

r260 by klenwell on Jun 21, 2010   Diff
T631: refinements to nail gmail config
script
Go to: 
Project members, sign in to write a code review

Older revisions

r259 by klenwell on Jun 20, 2010   Diff
T631: adding setup script for ubuntu
command line gmail with nail
All revisions of this file

File info

Size: 5366 bytes, 248 lines
Powered by Google Project Hosting