Python: Send an email at a specific time via SMTP - python

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.

Related

How do I input text into a Python script on a remote server?

I have a python script saved on an Amazon EC2 instance that sends an email:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
me = "filleremail#gmail.com"
my_password = r"password"
you = "receiver#gmail.com"
msg = MIMEMultipart('alternative')
msg['Subject'] = "Alert"
msg['From'] = me
msg['To'] = you
#TODO: How do I get email text from user
part2 = MIMEText(email_text)
msg.attach(part2)
# Send the message via gmail's regular server, over SSL - passwords are being sent, afterall
s = smtplib.SMTP_SSL('smtp.gmail.com')
# uncomment if interested in the actual smtp conversation
# s.set_debuglevel(1)
# do the smtp auth; sends ehlo if it hasn't been sent already
s.login(me, my_password)
s.sendmail(me, you, msg.as_string())
s.quit()
I want to submit text from a mobile app using a URL to fill the email_text variable. I have some experience with POST and GET requests but not on connecting them through Python. Can someone please point me in the right direction?

Setting reply-to as bcc

I am using python mime and I would like to implement that the receiver of the mail replies to two different mail addresses, if he clicks the "reply to" button in his mail client.
msg['To'] = receiver_email
msg['Subject'] = my_subj
msg.attach(MIMEText(my_msg, 'plain'))
msg.add_header('reply-to', reply_to)
msg.add_header('Bcc', reply_to_as_bcc)
However the reply_to_as_bcc address just receives a copy of the mail I am originally sending. How to implement that the reply_to_as_bcc address is occurring in the bcc field, if the user owning the receiver_email account is clicking reply to?

How to get smtp email log

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()

How to send emails programmatically and hide the sender address?

I would like to send mail notifications to my customers via python. The problem is the sender mail account needs to be hidden. Just to be clear - this is not for phishing or spamming, only personal use!
I used smtplib and setup a new 'noreply' account in gmail, but even when providing an alias to the message, the 'mail from:' header contains my actual mail.
import smtplib
from email.mime.text import MIMEText
from email.utils import *
email_sender = 'noreply%%#gmail.com'
email_receiver = 'example%%%#gmail.com'
subject = 'Python!'
msg = MIMEText('This is the body of the message.')
msg['To'] = formataddr(('Recipient', 'example%%%#gmail.com'))
msg['From'] = formataddr(('Author', 'author#example.com'))
msg['Subject'] = 'Simple test message'
connection = smtplib.SMTP('smtp.gmail.com', 587)
connection.starttls()
connection.login(email_sender, 'password')
connection.sendmail(msg['From'], email_receiver, msg.as_string())
connection.quit()
I get the mail in to my inbox as expected but when clicking 'more details' the original sender address appears.
The first argument to sendmail is the envelope sender, and should be just the email terminus, not a formatted address; so passing in msg['From'] there is doubly wrong (one, because you don't want to show it; and two, because you are passing in the entire From: header with display name and all).

Send an email as a google group from python

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

Categories