unable to send email using python - python

What am I doing wrong? Should be so simple.After I execute - nothing happens, no errors, no email. Nothing.
I am using Jupyter.
def send_email(user, pwd, recipient, subject, body):
import smtplib
gmail_user = 'email#email.com'
gmail_pwd = 'pass'
FROM = 'email#email.com'
TO = 'email#email.com'
SUBJECT = 'Test'
TEXT = 'Hello, this is test email'
# Prepare actual message
message = """From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
server.close()
print ('successfully sent the mail')
except:
print ("failed to send mail")

It is only function definiton. You have to execut it like
send_email("me#gmai.com", "MyPaSwOrD", "you#gmail.com", "Money for you", "Hi")
But your code will not use this data because it has hardcoded values inside.

Related

Python send simple mail

Im trying to write a simple script which has to send a simple email in some cases.
I have the following script which works well if im using just only this script.
import smtplib
mail_user = '123#123.com'
mail_password = 'password'
sent_from = mail_user
to = ['reciever#address.com']
subject = 'My subject'
body = 'Hello mail.'
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP_SSL('mail.123.com', 465)
server.ehlo()
server.login(mail_user, mail_password)
server.sendmail(sent_from, to, email_text)
server.close()
print 'Email sent!'
except:
print 'Something went wrong...'
The problem is when im trying to put this code into a def and call from outside the e-mail is missing headers, i mean the email is arriving without sender and without subject. Sender empty and subject empty, but i have only the body.
I also can not get the mail when im sending to another domain, but i think this is because the another domain is rejecting the mail without headers, when using only the script the mail arrives also to other domains.
import smtplib
def sendMail():
mail_user = '123#123.com'
mail_password = 'password'
sent_from = mail_user
to = ['reciever#address.com']
subject = 'My subject'
body = 'Hello mail.'
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP_SSL('mail.123.com', 465)
server.ehlo()
server.login(mail_user, mail_password)
server.sendmail(sent_from, to, email_text)
server.close()
print 'Email sent!'
except:
print 'Something went wrong...'
sendMail();
What is the diffenerece when i put this code into a def? Why this happening? What im doing wrong?
Thanks for help.
In your function version, your email headers have become indented
email_text = """\
From: %s
To: %s
Subject: %s
%s
...
In this string, the To: and Subject: are now indented.
def sendMail():
call it with:
sendMail()
not SendMail()

Sending email incl. Subject field ,using Python SMTP

I used server.sendmail(fromaddr, toaddrs, msg) to send automated emails using python.
How can I add an email subject to the mail?
I tried to look in SMTP documentation and didn't find any documentation for that.
From How to send an email with Gmail as provider using Python? :
def send_email(user, pwd, recipient, subject, body):
import smtplib
gmail_user = user
gmail_pwd = pwd
FROM = user
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
server.close()
print 'successfully sent the mail'
except:
print "failed to send mail"

Gmail SMTP graceful login

I'm using Gmail SMTP to send emails in Python, however sometimes the application may stay idle for an extended period of time.
How do I make sure that the session hasn't expired? Here's my code:
def send_email(user, pwd, recipient, subject, body):
import smtplib
gmail_user = user
gmail_pwd = pwd
FROM = user
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
server.close()
print 'successfully sent the mail'
except:
print "failed to send mail"
Can I always reuse the server session, or can it expire after let's say 5 hours?

Send python email using SMTP with a subject

I am trying to send an email in Python using SMTP, with a From address, To address, BCC address, subject, and message. I have the email sending, and it even sends to the BCC as it should, the only issue is that the message of the email says:
To: example#gmail.com
Subject: Subject goes here
this is the email that I’m sending
when I only want the message itself to show where the message belongs, and the subject of the email isn't set, so there's a blank subject. Here is how I have it set up:
def sendEmail(fromAddress, toAddress, bccAddress, appName, message):
subject = "Subject goes here"
BODY = string.join((
"From: %s\r\n" % fromAddress,
"To: %s\r\n" % toAddress,
"Subject: %s\r\n" % subject,
"\r\n",
message
), "\r\n")
#im using arbitrary values here, when I run it I use actual login info
username = 'example#gmail.com'
password = 'password'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(username,password)
toList = []
bccList = []
toList.append(toAddress)
bccList.append(bccAddress)
server.sendmail(fromAddress, toList + bccList, BODY)
server.quit()
Use the email package (docs).
from email.mime.text import MIMEText
def send_mail(to, from_addr, subject, text):
msg = MIMEText(text)
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = to
s = smtplib.SMTP_SSL("smtp.gmail.com")
s.login(smtp_user, smtp_pass)
# for Python 3
s.send_message(msg)
# OR
# for Python 2 (or 3, will still work)
s.sendmail(from_addr, [to], msg.as_string())
s.quit()

Sending an Email with Python Issue

I have this code and I cannot seem to get it to work. When I run it, the script doesn't finish in IDLE unless I kill it manually. I have looked all over and rewritten the code a few times, and no luck.
import smtplib
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
sender = 'abc#gmail.com'
password = '123'
recipient = 'cba#gmail.com'
subject = 'Test Results'
body = """** AUTOMATED EMAIL ** \r\n Following are
the test results: \r\n"""
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient]
headers = "\r\n".join(headers)
try:
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo()
session.login(sender, password)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
except smtplib.SMTPException:
print "Error: Unable to send email."
session.quit()
Not sure why you're using ehlo; contrary to popular opinion, it's not actually required so long as you set the headers correctly. Here's a tested and working script -- it works on *nix and OSX. Since you're using Windows though, we need to troubleshoot further.
import smtplib, sys
def notify(fromname, fromemail, toname, toemail, subject, body, password):
fromaddr = fromname+" <"+fromemail+">"
toaddrs = [toname+" <"+toemail+">"]
msg = "From: "+fromaddr+"\nTo: "+toemail+"\nMIME-Version: 1.0\nContent-type: text/plain\nSubject: "+subject+"\n"+body
# Credentials (if needed)
username = fromemail
password = password
# The actual mail send
try:
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
print "success"
except smtplib.SMTPException:
print "failure"
fromname = "Your Name"
fromemail = "yourgmailaccount#gmail.com"
toname = "Recipient"
toemail = "recipient#other.com"
subject = "Test Mail"
body = "Body....."
notify(fromname, fromemail, toname, toemail, subject, body, password)

Categories