In my implementation of smtpd.SMTPServer, I can receive messages via the process_message() callback. How could I send a reply message back to the sender?
This is a script that can send email it works for me. I don't know if is that you want but I hope its help you.
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
fromaddr = "YOUR ADDRESS"
toaddr = "ADDRESS YOU WANT TO SEND TO"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE MAIL"
body = "YOUR MESSAGE HERE"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "YOUR PASSWORD")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
Related
I'm trying to send message on email using Python smtplib and EmailMessage().
import smtplib
from email.message import EmailMessage
def email_alert(subject, body, to):
msg = EmailMessage()
msg.set_content(body)
msg['subject'] = subject
msg['to'] = to
user = 'username#gmail.com'
msg['from'] = user
password = 'app_password'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(user, password)
server.send(msg)
server.quit()
email_alert("hey", "Hello world","another#mail.com")
But error occures "TypeError: memoryview: a bytes-like object is required, not 'EmailMessage'".
What's the problem with the code? I saw video where this code worked.
Working code
import smtplib
from email.message import EmailMessage
def email_alert(subject, body, to):
msg = EmailMessage()
msg.set_content(body)
msg['subject'] = subject
msg['to'] = to
user = 'username#gmail.com'
msg['from'] = user
password = 'app_password'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(user, password)
server.send_message(msg) # <- UPDATED
server.quit()
email_alert("hey", "Hello world","another#mail.com")
I tried with "allow less secure app" and "unlockCaptcha", but still not work.
How to know if the problem is with my account or with the code?
Thanks for attention, here teh code:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
# Import the email modules we'll need
from email.message import EmailMessage
# create message object instance
msg = MIMEMultipart()
message = "Prova"
# setup the parameters of the message
password="pass"
msg['From'] = "senders#example.com"
msg['To'] = "receiver#example.com"
msg['Subject'] = "Prova"
# add in the message body
msg.attach(MIMEText(message, 'plain'))
#create server
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
# Login Credentials for sending the mail
server.login(msg['From'], password)
# send the message via the server.
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
print(f"successfully sent email to {msg['To']}")
I'm writing a little script to send emails and I would like to track who opened the email with the python module pytracking like so:
import smtplib
import pytracking
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
fromaddr = "info#XXXXXXXXXX.com"
toaddr = "XXXXXXXXa#gmail.com"
msg = MIMEMultipart()
#Not sure if this is the right location for the pixel
pixel= (pixel_byte_string, mime_type) = pytracking.get_open_tracking_pixel()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "grande"
body = "bla bla bla"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('mail.XXXXXXXX.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('info#XXXXXXXX.com','XXXXXXXX')
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
print('DONE')
I read the full documentation of this module here , but I can not find from where to actually check if the pixel has been opend or which command shell I run to do so.
Can anyone advise how to check if the pixel has been opened?
Thanks
I have written a python script to send email from my gmail account to my another email. the code is:
import smtplib
fromaddr = 'my#gmail.com'
toaddrs = 'to#gmail.com'
msg = 'my message'
username = 'my#gmail.com'
password = 'myPass'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
print("Done")
I'm also getting the "Done" output as the email is being sent. But the problem is: I can't seem to receive the email. It doesn't show up in the inbox. I can't find the problem :( Can anyone please help?
Thanks in advance...
Check this out, this works for me perfectly...
def send_mail(self):
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
gmailUser = 'myemail#gmail.com'
gmailPassword = 'P#ssw0rd'
recipient = 'sendto#gmail.com'
message='your message here '
msg = MIMEMultipart()
msg['From'] = gmailUser
msg['To'] = recipient
msg['Subject'] = "Subject of the email"
msg.attach(MIMEText(message))
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
Enable less secure apps on your gmail account and for Python3 use:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
gmailUser = 'XXXXX#gmail.com'
gmailPassword = 'XXXXX'
recipient = 'XXXXX#gmail.com'
message = f"""
Your message here...
"""
msg = MIMEMultipart()
msg['From'] = f'"Your Name" <{gmailUser}>'
msg['To'] = recipient
msg['Subject'] = "Your Subject..."
msg.attach(MIMEText(message))
try:
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
print ('Email sent!')
except:
print ('Something went wrong...')
my question is whether I can send an email via python in a c++ program.
That is my actual python script.
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
# From
fromaddr = "...#gmail.com"
# To
toaddr = "..."
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
# subject
msg['Subject'] = "..."
# Text
body = "..."
msg.attach(MIMEText(body, 'plain'))
#smtplib import
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login("username", "password")
text = msg.as_string()
#send email
server.sendmail(fromaddr, toaddr, text)
My plan ist it, that I will open python and import the script via the commandline in the programm.
Is there a better way to do it?
The problem is that c++ open via system("python") the commandline, but now I have to write import mail.py in the command line.
Can I do this automaticly with an other order? I will open python, import mail and quit with one order. Is that possible?
Thank You!
Here is the program for an email.
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(email, password)
smtpserver.sendmail(email, recipient, text)
print 'Message Sent!'
smtpserver.close()
The variables: email is your email address, password is the password to your email, recipient is the receiver of the email, and text is the text to send.