Program entry point.
()
| 161 | return data |
| 162 | |
| 163 | def main(): |
| 164 | """Program entry point.""" |
| 165 | |
| 166 | # Store when program started. |
| 167 | started = datetime.datetime.now() |
| 168 | |
| 169 | # Get parameters supplied to application. |
| 170 | argParser = getProgramArgumentParser() |
| 171 | args = argParser.parse_args() |
| 172 | |
| 173 | # Logic for displaying version details or program help. |
| 174 | if args.version: |
| 175 | printVersionDetailsAndExit() |
| 176 | if not (args.smtphost and args.to and args.frm and args.subject and args.body): |
| 177 | if args.version: |
| 178 | printVersionDetailsAndExit() |
| 179 | argParser.print_help() |
| 180 | sys.exit(SYS_EXIT_CODE_CMD_LINE_ERROR) |
| 181 | |
| 182 | # Process program arguments to get email parts. |
| 183 | # From can only have one value regardless if stored in file or not (first line in file used). |
| 184 | fromVal = getFileContentsOrParameterValue(args.frm)[0] |
| 185 | toData = getFileContentsOrParameterValue(args.to) |
| 186 | subjectVal = getFileContentsOrParameterValue(args.subject)[0] |
| 187 | bodyData = getFileContentsOrParameterValue(args.body) |
| 188 | smtpHostVal = getFileContentsOrParameterValue(args.smtphost)[0] |
| 189 | |
| 190 | # Build multipart MIME message (email). |
| 191 | multipartMimeMsg = email.mime.multipart.MIMEMultipart() |
| 192 | multipartMimeMsg['Date'] = email.utils.formatdate(localtime=True) |
| 193 | multipartMimeMsg['From'] = fromVal |
| 194 | multipartMimeMsg['To'] = COMMA_SPACE.join(toData) |
| 195 | multipartMimeMsg['Subject'] = subjectVal |
| 196 | multipartMimeMsg.attach(email.mime.text.MIMEText(email.utils.CRLF.join(bodyData))) |
| 197 | |
| 198 | # Process optional arguments. |
| 199 | if args.cc: |
| 200 | ccData = getFileContentsOrParameterValue(args.cc) |
| 201 | multipartMimeMsg['Cc'] = COMMA_SPACE.join(ccData) |
| 202 | toData.extend(ccData) # TODO: check? |
| 203 | # TODO: FUNC_BCC |
| 204 | #if args.bcc: |
| 205 | # bccData = getFileContentsOrParameterValue(args.bcc) |
| 206 | # multipartMimeMsg['Bcc'] = COMMA_SPACE.join(bccData) |
| 207 | # toData.extend(bccData) # TODO: similar to CC but is blindness enforced? |
| 208 | |
| 209 | # Python 3.3 supports with statement (context manager) for smtplib.SMTP(). |
| 210 | # http://docs.python.org/dev/library/smtplib.html |
| 211 | # with smtplib.SMTP(smtpHostVal) as smtpSvr: |
| 212 | try: |
| 213 | smtpSvr = smtplib.SMTP(smtpHostVal) |
| 214 | if args.debug: |
| 215 | # Increase display verbosity. |
| 216 | smtpSvr.set_debuglevel(1) |
| 217 | smtpSvr.sendmail(fromVal, toData, multipartMimeMsg.as_string()) |
| 218 | finally: |
| 219 | smtpSvr.quit() |
| 220 |
no test coverage detected