Wednesday, April 20, 2016

email sending

http://tecadmin.net/ways-to-send-email-from-linux-command-line/

http://www.binarytides.com/linux-mail-command-examples/

http://www.tutorialspoint.com/python/python_sending_email.htm

https://www.scottbrady91.com/Email-Verification/Python-Email-Verification-Script

http://stackoverflow.com/questions/3739909/how-to-strip-all-whitespace-from-string

http://stackoverflow.com/questions/7232088/python-subject-not-shown-when-sending-email-using-smtplib-module

http://stackoverflow.com/questions/1546367/python-how-to-send-mail-with-to-cc-and-bcc

http://stackoverflow.com/questions/73781/sending-mail-via-sendmail-from-python

echo "This is the body" | mail -s "Subject" -aFrom:Harry\<harry@gmail.com\> gopal.singh@ruckuswireless.com

# Python script:

# email_receivers must be a list for multiple receivers. One receiver is OK to specify as string.

import smtplib

email_sender = 'gopal.singh@ruckuswireless.com'
email_receivers = email_sender
message = 'Test Message'

def send_email(sender, receivers, message):
try:
  smtpObj = smtplib.SMTP('localhost')
  smtpObj.sendmail(sender, receivers, message)      
  print "Successfully sent email"
except smtplib.SMTPException:
  print "Error: unable to send email"


def main():
send_email(email_sender, email_receivers, message)

if __name__ == '__main__':
    main()


# Email with CC and BCC
toaddr = 'buffy@sunnydale.k12.ca.us'
cc = ['alexander@sunydale.k12.ca.us','willow@sunnydale.k12.ca.us']
bcc = ['chairman@slayerscouncil.uk']
fromaddr = 'giles@sunnydale.k12.ca.us'
message_subject = "disturbance in sector 7"
message_text = "Three are dead in an attack in the sewers below sector 7."
message = "From: %s\r\n" % fromaddr
        + "To: %s\r\n" % toaddr
        + "CC: %s\r\n" % ",".join(cc)
        + "Subject: %s\r\n" % message_subject
        + "\r\n"
        + message_text
toaddrs = [toaddr] + cc + bcc
server = smtplib.SMTP('smtp.sunnydale.k12.ca.us')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, message)
server.quit()


# Using sendmail

def sendMail():
    sendmail_location = "/usr/sbin/sendmail" # sendmail location
    p = os.popen("%s -t" % sendmail_location, "w")
    p.write("From: %s\n" % "from@somewhere.com")
    p.write("To: %s\n" % "to@somewhereelse.com")
    p.write("Subject: thesubject\n")
    p.write("\n") # blank line separating headers from body
    p.write("body of the mail")
    status = p.close()
    if status != 0:
           print "Sendmail exit status", status


#

from email.mime.text import MIMEText
from subprocess import Popen, PIPE

msg = MIMEText("Here is the body of my message")
msg["From"] = "me@example.com"
msg["To"] = "you@example.com"
msg["Subject"] = "This is the subject."
p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE, universal_newlines=True)
p.communicate(msg.as_string())


=====
# using yahoo

#!/usr/bin/env python
# coding: utf-8
import re
import subprocess
import shlex
import os
import argparse, sys


def runcmd(cmd):
    cmd =  shlex.split(cmd)
    try:
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, error = proc.communicate()
    return out + error
    except:
    print 'Invalid Command: ' + str(cmd)

import smtplib
from email.mime.text import MIMEText
SMTP_SERVER = "smtp.mail.yahoo.com"
SMTP_PORT = 587
SMTP_USERNAME = "hurrymanjee"
SMTP_PASSWORD = "######"
EMAIL_FROM = "hurrymanjee@yahoo.com"
EMAIL_TO = "dummyrain@sbcglobal.net"
EMAIL_SUBJECT = "REMINDER:"
co_msg = """
Hello, [username]! Just wanted to send a friendly appointment
reminder for your appointment:
[Company]
Where: [companyAddress]
Time: [appointmentTime]
Company URL: [companyUrl]
Change appointment?? Add Service??
change notification preference (text msg/email)
"""
def send_email():
    msg = MIMEText(co_msg)
    msg['Subject'] = EMAIL_SUBJECT + "Company - Service at appointmentTime"
    msg['From'] = EMAIL_FROM
    msg['To'] = EMAIL_TO
    debuglevel = True
    mail = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
    mail.set_debuglevel(debuglevel)
    mail.starttls()
    mail.login(SMTP_USERNAME, SMTP_PASSWORD)
    mail.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
    mail.quit()

if __name__=='__main__':
    send_email()

1 comment: