I want to send email in python and the following code works. However I want to send the email as a google group. Since a google group has no password, I am not able to login to the server. Is there anyway I can go about this?
def sendEmail(self, toEmail, subject, message ):
msg = MIMEMultipart()
password = "*****"
msg['From'] = "abc#gmail.com"
msg['To'] = toEmail
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP('smtp.gmail.com: 587')
server.starttls()
server.login(msg['From'],password)
server.sendmail(msg['From'], msg['To'].split(","), msg.as_string())
server.quit()
logging.debug('sent email to %s', (msg['To']))
It sends like you should use a Service Account to send the email on your behalf.
Here is a pretty good guide (not written by me): https://medium.com/lyfepedia/sending-emails-with-gmail-api-and-python-49474e32c81f
Related
I'm using python and smtplib module to send an email via SMTP server.
I create the header of the email like this, I'm using EmailMessage()
msg = EmailMessage()
msg["From"] = fromAddress
msg["To"] = toAddress
msg["Subject"] = "Hello"
msg["Delivery-Date"]
Is it possible to send an email via an SMTP server (Outlook) by specifying date and time of receipt of the email in the header of the message. With Delivery-Date, it doesn't work.
I want to send an email without login to server in Python. I am using Python 3.6.
I tried some code but received an error. Here is my Code :
import smtplib
smtpServer='smtp.yourdomain.com'
fromAddr='from#Address.com'
toAddr='to#Address.com'
text= "This is a test of sending email from within Python."
server = smtplib.SMTP(smtpServer)
server.set_debuglevel(1)
server.sendmail(fromAddr, toAddr, text)
server.quit()
I expect the mail should be sent without asking user id and password but getting an error :
"smtplib.SMTPSenderRefused: (530, b'5.7.1 Client was not authenticated', 'from#Address.com')"
I am using like this. It's work to me in my private SMTP server.
import smtplib
host = "server.smtp.com"
server = smtplib.SMTP(host)
FROM = "testpython#test.com"
TO = "bla#test.com"
MSG = "Subject: Test email python\n\nBody of your message!"
server.sendmail(FROM, TO, MSG)
server.quit()
print ("Email Send")
import win32com.client as win32
outlook=win32.Dispatch('outlook.application')
mail=outlook.CreateItem(0)
mail.To='To address'
mail.Subject='Message subject'
mail.Body='Message body'
mail.HTMLBody='<h2>HTML Message body</h2>' #this field is optional
# To attach a file to the email (optional):
attachment="Path to the attachment"
mail.Attachments.Add(attachment)
mail.Send()
The code below worked for me.
First, I opened/enabled Port 25 through Network Team and used it in the program.
import smtplib
smtpServer='smtp.yourdomain.com'
fromAddr='from#Address.com'
toAddr='to#Address.com'
text= "This is a test of sending email from within Python."
server = smtplib.SMTP(smtpServer,25)
server.ehlo()
server.starttls()
server.sendmail(fromAddr, toAddr, text)
server.quit()
First, you have to have a SMTP server to send an email. When you don't have one, usually outlook's server is used. But outlook only accepts authenticated users, so if you don't want to login into the server, you have to pick a server that doesn't need authentication.
A second approach is to setup an internal SMTP server. After you setup the internal SMTP server, you can use the "localhost" as the server to send the email. Like this:
import smtplib
receiver = 'someonesEmail#hisDomain.com'
sender = 'yourEmail#yourDomain.com'
smtp = smtplib.SMTP('localhost')
subject = 'test'
body = 'testing plain text message'
msg = 'subject: ' + subject + ' \n\n' + body
smtp.sendmail('sender', receiver, msg)
So I have seen lots of great info on here about sending automated emails using python. However, my task is slightly different. The script im working on needs to send an automated email when some condition is true then the email contains the printed output. But since this is for work the file is stored on a shared server and therefore, I cannot be the one to "send" the email, it needs to send automatically from the file. My question is therefore how this email can be sent with no "from" address, or if this is even possible.
Additionally, how can I make sure that the email contains the printed output? Below is the attached code im using. The variables stored in the list are dataframes.
mylist = [right_branchcode, right_branchname, right_sellingcode, right_advcorp, right_childparent, \
right_eliteCategoryId, right_partyid, right_retailmga]
flag = True
for item in mylist:
if len(item) > 0:
print(item)
flag = True
else:
pass
def send_email(audit):
fromaddr = " >"
toaddr = "recip#mail, recip2#mail"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Alert: Audit Mismatch For DimAdvisor"
body = "Current mismatch in dimAdvisor found on : " + temperature
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp-mail.outlook.com', 587)
server.starttls()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
Any help would be great. Thanks!
This is how you can send email using Python:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
recipients = ['john.doe#example.com', 'john.smith#example.co.uk']
msg = MIMEMultipart()
msg['From'] = "Can be any string you want, use ASCII chars only " # sender name
msg['To'] = ", ".join(recipients) # for one recipient just enter a valid email address
msg['Subject'] = "Subject"
body = "message body"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587) # put your relevant SMTP here
server.ehlo()
server.starttls()
server.ehlo()
server.login('jhon#gmail.com', '1234567890') # use your real gmail account user name and password
server.send_message(msg)
server.quit()
Please note this line:
msg['From'] = "Can be any string you want, use ASCII chars only " # sender name
You can write any string in the From field regardless of any real email address. I didn't try to leave it empty but I guess you can try it yourself :-)
I want to get a log when I email to the wrong email address.
so, I wrote this command.
mailserver = mymailserver
to_email = emailaddress
from_email = fromaddress
subject = SBJECT
original_message = TEXT
message = MESSAGE
server = smtplib.SMTP(mailserver)
debug = server.set_debuglevel(1)
server.sendmail(from_mail,to_mail, message)
server.quit()
print(debug)
I just want to know connection status log.
wchich code should I edit?
I tried this scripts, but it does not work well.
server = smtplib.SMTP(mailserver)
mail_response = server.sendmail(from_mail,to_mail, message)
server.quit()
print(mail_response)
Thank you for helping me.
Try This and read the docs. Hope it will help you
# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.message import EmailMessage
# Open the plain text file whose name is in textfile for reading.
# Or simply skip this part
with open(textfile) as fp:
# Create a text/plain message
msg = EmailMessage()
msg.set_content(fp.read())
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = f'The contents of {textfile}'
msg['From'] = me
msg['To'] = you
# Send the message via our own SMTP server.
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()
So yesterday I had this bit of code written out and it worked perfectly fine, but today it's not sending e-mails anymore. Can someone explain why?
import smtplib
SERVER = 'owa.server.com'
FROM = 'noreply#server.com'
TO = ['person#gmail.com', '1112223344#vtext.com']
name = 'Mr. Man'
SUBJECT = 'Recent Information for: %s' % (name)
TEXT = "Dear " +name+ ",\n\nHello.\n\nSincerely,\nOur Guys Here"
message = """From: %s\r\nTo: %s\r\nSubject: %s\r\n\
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
server = smtplib.SMTP(SERVER, 587)
server.ehlo()
server.starttls()
server.ehlo
server.login('noreply#server.com', 'password')
server.sendmail(FROM, TO, message)
server.quit()
This code is a working snippet. I wasn't getting the e-mails in my personal gmail account because gmail was sending it to the spam folder. I checked to see if it works at my office account, and it did just fine.
import smtplib
# Specifying the from and to addresses
fromaddr = 'fromuser#gmail.com'
toaddrs = 'to#gmail.com'
# Writing the message (this message will appear in the email)
msg = 'Enter you message here'
# Gmail Login
username = 'username'
password = 'password'
# Sending the mail
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
Above standard smtp send works with gmail,
thus it must be your server(whatever you're using) configuration that is at fault.