I'm trying to send email with python.
In the function server.sendmail, when I replace message by a "example text with double quote", I receive my email with the good message.
Now, when I try to send a file content saved in my "message" variable, I receive a empty mail, but if I print my variable, the content is correct.
Have you an idea ?
Thx !
import smtplib, ssl
import os
smtp_adress = 'smtp.gmail.com'
smtp_port = 465
email_adress = 'xxx#gmail.com'
email_password = 'xxx'
email_receiver = 'xxx#hotmail.fr'
# message
file = open('dl_link', 'r')
line = file.readlines()
file.close()
message = line[0]
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_adress, smtp_port, context=context) as server:
server.login(email_adress, email_password)
server.sendmail(email_adress, email_receiver, message)
print(message)
Related
How can I send multiple mails with a hidden recipient?
I can currently send mails, but people who receive it can see the recipient.
My code looks like this:
import smtplib
from email.message import EmailMessage
email_subject = "Good morning"
sender_email_address = "user#outlook.com"
receivers_email_address = ['reciever1#gmail.com', 'reciever2#gmail.com']
email_smtp = "smtp.office365.com"
email_password = "MyPassword"
# Create an email message object
message = EmailMessage()
# Configure email headers
message['Subject'] = email_subject
message['From'] = sender_email_address
message['To'] = ", ".join(receivers_email_address)
# Read file containing html
with open('message.html', 'r') as file:
file_content = file.read()
# Add message content as html type
message.set_content(file_content, subtype='html')
# Set smtp server and port
server = smtplib.SMTP(email_smtp, '587')
# Identify this client to the SMTP server
server.ehlo()
# Secure the SMTP connection
server.starttls()
# Login to email account
server.login(sender_email_address, email_password)
# Send email
server.send_message(message)
# Close connection to server
server.quit()
You don't need to send multiple messages. Just don't put the recipients explicitly in the headers.
The following implements this by putting the recipients in the Bcc: header.
import smtplib
from email.message import EmailMessage
email_subject = "Good morning"
sender_email_address = "user#outlook.com"
receivers_email_address = ['reciever1#gmail.com', 'reciever2#gmail.com']
email_smtp = "smtp.office365.com"
email_password = "MyPassword"
message = EmailMessage()
message['Subject'] = email_subject
message['From'] = sender_email_address
# The message needs to have a To: header
# - putting yourself is an old convention
message['To'] = sender_email_address
message['Bcc'] = ",".join(receivers_email_address)
with open('message.html', 'r') as file:
file_content = file.read()
message.set_content(file_content, subtype='html')
with smtplib.SMTP(email_smtp, '587') as server:
server.ehlo()
server.starttls()
server.login(sender_email_address, email_password)
server.send_message(message)
server.quit()
Of course, if you really need to To: header to indicate the actual recipient, you will need to generate a unique message for each of them.
This relies on the SMTP server to read and strip off the Bcc: header. If you can't rely on yours to do that, you can explicitly use the legacy sendmail method of the smtplib module, which lets you explicitly pass in the list of actual recipients separately from the message. Then you can also avoid the pesky copy to yourself.
To briefly recap, SMTP doesn't really care what's in the To: or Cc: headers; the actual list of recipients is communicated separately, before you submit the actual message. This is called the SMTP envelope.
I made a loop to send individually. Instead of sending it to multiple people in a group.
import smtplib
from email.message import EmailMessage
email_subject = "Good morning"
sender_email_address = "user#outlook.com"
receivers_email_address = ['reciever1#gmail.com', 'reciever2#gmail.com']
email_smtp = "smtp.office365.com"
email_password = "MyPassword"
# Read file containing html
with open('message.html', 'r') as file:
file_content = file.read()
# Set smtp server and port
server = smtplib.SMTP(email_smtp, '587')
# Identify this client to the SMTP server
server.ehlo()
# Secure the SMTP connection
server.starttls()
# Login to email account
server.login(sender_email_address, email_password)
# Send email
for receiver in receivers_email_address:
# Create an email message object
message = EmailMessage()
message['Subject'] = email_subject
message['From'] = sender_email_address
message['To'] = receiver
# Add message content as html type
message.set_content(file_content, subtype='html')
server.send_message(message)
# Close connection to server
server.quit()
I was asked to recive an email using any coding language ( I know java and C but python will be much easier I think but I hope someone can help me choose) then look for the word "cat" inside the body of the email then run the python file attachment which is attached to the mail and send back the output of the file to the sender if the word cat is in the message ( if the word wasnt there or the file returned an exception then return the the problem ) here is what I did so far :
import email
import imaplib
import smtplib
from email.message import EmailMessage
def send_email():
sender_email = 'momo#gmail.com'
password = '123456789'
rec_email ="koko#gmail.com"
message = input(str("enter your message below : "))
server= smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(sender_email, password )
print("login success")
server.sendmail(sender_email , rec_email, message )
print("Email has been sent to " , rec_email )
def recive_and_check():
EMAIL = 'nono#gmail.com'
PASSWORD = '123456789'
SERVER = 'imap.gmail.com'
# connect to the server and go to its inbox
mail = imaplib.IMAP4_SSL(SERVER)
mail.login(EMAIL, PASSWORD)
mail.select("inbox")
#I used several tutorials on youtube from here don't really understand what I wrote
status, data = mail.search(None, 'ALL')
inbox_item_list = data[0].split()
most_recent = inbox_item_list[-1]
oldest = inbox_item_list [0]
email_data = mail.fetch(most_recent,'(RFC822)')
raw_email = email_data[0][1].decode("utf-8")
email_message = email.message_from_string(raw_email)
email_message: EmailMessage = email.message_from_bytes(envelope, _class=EmailMessage)
for email_message_part in email_message.walk():
if email_message_part.is_attachment():
Run(email_message_part)
I'm a beginner trying to send a personalized email with python.
import smtplib, ssl
def read_creds():
user = passw = ""
with open("credentials.txt", "r") as f:
file = f.readlines()
user = file[0].strip()
passw = file[1].strip()
return user, passw
port = 465
sender, password = read_creds()
receive = sender
message = """\
Subject: Python
This is from python!
"""
context = ssl.create_default_context()
print("Starting to send")
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
server.login(sender, password)
server.sendmail(sender, receive, message)
print("email sent.")
I want to add a personalized code (which could be, for example, 123456) to every email, but from the tutorial I've read I can't understand how to do it.
You need to identify the way to retrieve a list of contacts from a file or from the database. For the simplicity I'll use an array.
Start with updating your message to support custom messages:
message = """
Subject: Message subject
Hi {name}, you have got a personal message!
"""
Then update the logic behind sending the emails and add recipients into the array (in real life use the DB or file):
recipients = [{"email":"email1#aol.com", "name":"Misha"},{"email":"email2#aol.com", "name":"Andrey"}]
print("Starting to send")
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
server.login(sender, password)
for recipient in recipients:
server.sendmail(sender, recipient["email"], message.format(name=name))
I found a small program that will send my phone a text message through my gmail, but when I send the text it adds on "[Attachment(s) removed]", is there any way to remove that?
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
email = "Your Email"
pas = "Your Pass"
sms_gateway = 'number#tmomail.net'
# The server we use to send emails in our case it will be gmail but every email provider has a different smtp
# and port is also provided by the email provider.
smtp = "smtp.gmail.com"
port = 587
# This will start our email server
server = smtplib.SMTP(smtp,port)
# Starting the server
server.starttls()
# Now we need to login
server.login(email,pas)
# Now we use the MIME module to structure our message.
msg = MIMEMultipart()
msg['From'] = email
msg['To'] = sms_gateway
# Make sure you add a new line in the subject
msg['Subject'] = "You can insert anything\n"
# Make sure you also add new lines to your body
body = "You can insert message here\n"
# and then attach that body furthermore you can also send html content.
msg.attach(MIMEText(body, 'plain'))
sms = msg.as_string()
server.sendmail(email,sms_gateway,sms)
# lastly quit the server
server.quit()
When you are doing the server.sendmail step just send the body string. So instead it would be:
import smtplib
email = "Your Email"
pas = "Your Pass"
sms_gateway = 'number#tmomail.net'
smtp = "smtp.gmail.com"
port = 587
# This will start our email server
server = smtplib.SMTP(smtp,port)
# Starting the server
server.starttls()
# Now we need to login
server.login(email,pas)
body = "Yo, im done."
server.sendmail(email,sms_gateway,body)
# lastly quit the server
server.quit()
i have an output.txt which i want to send its content as an email body.
1.i want to remove specific lines from the content ( not delete from the file itself)
2.i want to be able to convert it to html and to desing some of specific lines size and maybe even color.
i have a working script which sends the mail.
there are some problems:
im not able to remove specific line when using file.read().
when trying readlines() the type is list and the email body is getting messed up.(join doesnt work on list with bytes type)
import smtplib
import email.message
def send_email(subject, msg):
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
ID = 'XXXX'
PASSWORD = 'XXXXXXX'
email_reciever = 'XXXXXX'
server.login(ID, PASSWORD)
message = 'Subject:{} \n\n {}'.format(subject, msg)
server.sendmail(ID, email_reciever, message)
print('Succes')
except Exception as e:
# Print any error messages to stdout
print(e)
finally:
server.quit()
filename = r".\Reports\Report.txt"
with open(filename, "rb") as filename:
text = filename.read()
msg = email.message.Message()
msg.add_header('Content-Type', 'text/plain')
msg.set_payload(text)
subject = "Report"
send_email(subject, msg)