I'm Jan and it's my first post here and the following code is also my first python code. So please don't judge me, if the code is not well shaped :) and don't wonder I had to reduce my mail body.
With the following code I try to generate several msg file depending on a user list called "customer_names". The idea is to iterate through this list and to adjust the email body espacially the placeholder for "Customer". The rest of the body content is not so important. Everything works good except the iteration through the list. I have a suggestion that I may need to increment the index for the list in the loop. Do you have any ideas.
import win32com.client as win32
import datetime
import random
# List of customer names
customer_names = ['Name1','Name2','Name3']
current_customer_index = 0
# List of email providers
email_providers = ['provider1', 'provider2', 'provider3']
# set up the Outlook application
outlook = win32.Dispatch('outlook.application')
# create a new email
mail = outlook.CreateItem(0)
# set the subject and recipients
mail.Subject = "Request for Customer Information"
mail.To = "user#emailprovider.net"
# Message body for the email
message = f"Dear User,\n\nThe information we require from [Customer] is as follows:\n\n- Email Address: [Email Address] \n\nWe kindly request that you send the requested information to us within [number of days] days. \n\n Kind regards."
# set the number of days for the customer to respond
num_days = 7
# set the importance of the email to normal
mail.Importance = 1 # 0=Low, 1=Normal, 2=High
# set the sensitivity of the email to normal
mail.Sensitivity = 0 # 0=Normal, 1=Personal, 2=Private, 3=Confidential
# set the read receipt option to true
mail.ReadReceiptRequested = True
# add a reminder for the sender to follow up in 3 days
mail.FlagRequest = "Follow up"
mail.FlagDueBy = (datetime.date.today() + datetime.timedelta(days=3)).strftime('%m/%d/%Y')
# Generate a random email
for i in range(len(customer_names)):
customer = customer_names[i]
message_with_data = message.replace("[Customer]", customer)
message_with_data = message_with_data.replace("[number of days]", str(num_days))
mail.Body = message_with_data
file_name = "Request_" + customer + ".msg"
file_path = "C:/Users/user/Desktop/Email/" + file_name
mail.SaveAs(file_path)
Related
I've been trying to make a python script to send a happy birthday email on a given date, that notifies me who it sent an email to via text message.
I have 3 files.
First I have a brithdays.csv file which I enter all the birthday data that is going to use.
Here's an example of what's inside the file:
name,email,year,month,day
Ivy,Testemail#mail.com,1,3,25
Rose,Testemail#mail.com,1,3,28
Kimberly,Testemail#mail.com,1,4,10
Then I have a letter template which the script reads and replaces [NAME] with the name in the CSV file :
Greetings,
Im wishing [NAME] a very happy birthday! [NAME], 😊 I hope you enjoy your day and wishing you more life.
Thank you,
And then I have my actual code
############## IMPORTS ##############
import smtplib
import datetime as dtime
import pandas as pd
import random
from time import strftime
from email.mime.text import MIMEText
############## Reading Data and Check Current Day & Month ##############
# READ CSV BIRTHDAY FILE
df = pd.read_csv("birthdays.csv")
# PRINT CURRENT DAY
current_day = dtime.datetime.now().day
current_month = dtime.datetime.now().month
##################ENTER LOGIN HERE#############################
LOGIN = "EMAIL"
PASS = "PASSWORD"
############## LOGIC ##############
# save the rows that has the current day in new variable
new_df = df.loc[df['day'] == current_day]
# check the length of new_df of the current month so if the result is larger than 1
# so there is birthdays on this day
if len(new_df.loc[new_df['month'] == current_month]) > 0:
# check the length of people having birthday in this day
for i in range(len(new_df.loc[new_df['month'] == current_month])):
# OPEN BIRTHDAY TEMPLATE
with open(f"./letter_1.txt") as letter_file:
# READING FILE
letter_contents = letter_file.read()
#CREATE NAME VARIABLE
name = df["name"][i]
# replace [NAME] with actual name on the data
if len(new_df["name"]) > 1:
the_letter = letter_contents.replace("[NAME]", new_df["name"][i])
the_email = new_df["email"][i]
else:
the_letter = letter_contents.replace("[NAME]", new_df["name"].item())
the_email = new_df["email"].item()
# SMTPLIB LOGIN TO SEND EMAIL
# CONNECTION
with smtplib.SMTP("smtp.outlook.com") as con:
# START
con.starttls()
# LOGIN
con.login(user=LOGIN, password=PASS)
# create the msg
msg = MIMEText(the_letter, 'html')
msg["From"] = LOGIN
msg["To"] = the_email
msg["Subject"] = "Happy Birthday " + name + "!!!"
msg["Cc"] = "CC EMAILS"
# SEND EMAIL
con.send_message(msg)
#SENDS TEXT MESSAGE CONFIRMATION
msg = MIMEText ("Sent Happy Birthday Email to " + name + " on " + str(text.strftime('%Y-%m-%d %H:%M:%S %p')))
msg["From"] = LOGIN
msg["To"] = "VERIZONPHONENUMBER#vtext.com"
# SEND TEXT
con.send_message(msg)
# LOGS OUT OF EMAIL
con.quit()
I think the issue lies in Line 45 where I create the name variable.
My issue is that every time it sends an email, the name in the Subject of the email and in the text message I receive it's the first name of the list.
For example if I run the script, the email would look something like this
Happy Birthday Ivy!!!
Greetings,
Im wishing Rose a very happy birthday! Rose, 😊 I hope you enjoy your day and wishing you more life.
I'm trying to make the name that appears on the subject match with the name that appears on the body of text.
msg = MIMEText ("Sent Happy Birthday Email to " + name + " on " + str(text.strftime('%Y-%m-%d %H:%M:%S %p')))
Here you are using the name variable when generating the body but earlier in your code you have:
name = df["name"][i]
Which grabs the name from the unfiltered DataFrame but you use the filtered DataFrame to generate the subject line here:
# replace [NAME] with actual name on the data
if len(new_df["name"]) > 1:
the_letter = letter_contents.replace("[NAME]", new_df["name"][i])
the_email = new_df["email"][i]
else:
the_letter = letter_contents.replace("[NAME]", new_df["name"].item())
the_email = new_df["email"].item()
The i-th element in the filtered DataFrame might not be the i-th element in your original DataFrame so you need to declare name as follows:
name = new_df["name"][i]
Every morning I get spot data on FX volumes via an email, I'd like to build a process to search two pieces of data within the body of the email and save them as a new variable which I can then refer to later.
I've got the process to search my emails, order them according to date and check whether the entered data exists within the emails, but because the data is contained within a format between two commas, I am unsure how to take that data out and assign it to a new variable.
Format for example is this:
BWP/USD,0
CHF/AMD T,0
This is what I've achieved thus far:
import win32com.client
import os
import time
import re
# change the ticker to the one you're looking for
FX_volume1 = "BWP/USD"
FX_volume2 = "CHF/AMD"
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
messages.Sort("[ReceivedTime]", True)
# find spot data
for message in messages:
if message.subject.startswith("FX SPOT FIGURES"):
if FX_volume1 and FX_volume2 in message.body:
data = message.body
print(data)
else:
print('No data for', FX_volume1, 'or', FX_volume2, 'was found')
break
Any idea how to take this forward?
Thanks for any assistance/pointers
import win32com.client
import os
import time
import re
# change the ticker to the one you're looking for
FX_volume1 = "BWP/USD"
FX_volume2 = "CHF/AMD"
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
messages.Sort("[ReceivedTime]", True)
# find spot data
for message in messages:
if message.subject.startswith("FX SPOT FIGURES"):
case1 = re.match(FX_volume1 + ",(\d*)", message.body)
case2 = re.match(FX_volume2 + ",(\d*)", message.body)
case (1 and 2) will be match objects if a match is found, else they will be None. To retrieve your values just do val = case1.group(1). Hence:
EDIT:
if case1 not None:
FX_vol1_val = case1.group(1)
if case2 not None:
FX_vol2_val = case1.group(1)
For more info on match objects:
https://docs.python.org/3/library/re.html#match-objects
If you are expecting floats, see the following link:
Regular expression for floating point numbers
EDIT 2:
Hi, so as you couldn't get it working I gave it a quick try and it worked for me with the following example. Just to add to regex notation, anything that you put in brackets (), if the pattern matches, the contents between the brackets will be stored.
import re
my_text = "BWP/USD,1"
FX_pattern = "BWP/USD," # added the comma here for my convinience
my_match = re.match(FX_pattern, "(\d*)")
print("Group 0:", my_match.group(0))
print("Group 1:", my_match.group(1))
Printout:
Group 0: BWP/USD,1
Group 1: 1
I have a problem with a code which is supposed to download your emails in eml files.
Its supposed to go through the INBOX email listing, retrieve the email content and attachments(if any) and create an .eml file which contains all that.
What it does is that it works with content type of text and the majority multiparts. If an email in the listing contains utf-8B in its header, it simply acts like its the end of the email listing, without displaying any error.
The code in question is:
result, data = p.uid('search',None, search_criteria) # search_criteria is defined earlier in code
if result == 'OK':
data = get_newer_emails_first(data) # get_newer_emails_first() is a function defined to return the list of UIDs in reverse order (newer first)
context['emailsum'] = len(data) # total amount of emails based on the search_criteria parameter.
for num in data:
mymail2 = {}
result,data1 = p.iud('fetch', num, '(RFC822)')
email_message = email.message_from_bytes(data[0][1])
fullemail = email_message.as_bytes()
default_charset = 'ASCII'
if email_message.is_multipart():
m_subject = make_header(decode_header(email_message['Subject']))
else:
m_subject = r''.join([ six.text_type(t[0], t[1] or default_charset) for t in email.header.decode_header(email_message['Subject']) ])
m_from = string(make_header(decode_header(email_message['From'])))
m_date = email_message['Date']
I have done my tests and discovered that while the fullemail variable contains the email properly (thus it reads the data from the actual email successfully), the problem should be in the if else immediately after, but I cannot find what the problem is exactly.
Any ideas?
PS: I accidentally posted this question as a guest, but I opted to delete it and repost it from my account.
Apparently the error lay in my code in the silliest of ways.
Instead of:
m_from = string(make_header(decode_header(email_message['From'])))
m_date = email_message['Date']
It should be:
m_from = str(make_header(decode_header(email_message['From'])))
m_date = str(make_header(decode_header(email_message['Date'])))
I am trying to send meeting invites from outlook using python. I also want to reflect that in the outlook calendar of the receiver. I am using the following code
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
def sendRecurringMeeting():
appt = outlook.CreateItem(1) # AppointmentItem
appt.Start = "2018-11-14 15:30" # yyyy-MM-dd hh:mm
appt.Subject = "Important"
appt.Duration = 60 # In minutes (60 Minutes)
appt.Location = "Floor 5"
appt.MeetingStatus = 1 # 1 - olMeeting; Changing the appointment to meeting. Only after changing the meeting status recipients can be added
appt.Recipients.Add("mabc#abc.com") # Don't end ; as delimiter
appt.Recipients.Add("xyz#xyz.com")
appt.ReminderSet = True
appt.ReminderMinutesBeforeStart = 15
# Set Pattern, to recur every day, for the next 5 days
pattern = appt.GetRecurrencePattern()
pattern.RecurrenceType = 0
pattern.Occurrences = "1"
appt.Save()
appt.Send()
sendRecurringMeeting()
Here I am trying to send meeting invites from outlook with Python. Generally when we send a meeting invite, It starts showing in their calendar that you have a meeting today at the mentioned time. By using the above code, I am able to send a meeting invite but it is not showing in their calendar i.e it is not giving reminders.
I have following script which processes emails and save them to csv file. there will be advancement to script where I will use mechanize lib to process the extracted emails data for further processing on an another web interface. There are times it may fail now I can trap that specific email without having any problem but how can I forward the trapped email to a different address where I can process it manually or see what's wrong with it?
Here's the script
import ConfigParser
import poplib
import email
import BeautifulSoup
import csv
import time
DEBUG = False
CFG = 'email' # 'email' or 'test_email'
#def get_config():
def get_config(fnames=['cron/orderP/get_orders.ini'], section=CFG):
"""
Read settings from one or more .ini files
"""
cfg = ConfigParser.SafeConfigParser()
cfg.read(*fnames)
return {
'host': cfg.get(section, 'host'),
'use_ssl': cfg.getboolean(section, 'use_ssl'),
'user': cfg.get(section, 'user'),
'pwd': cfg.get(section, 'pwd')
}
def get_emails(cfg, debuglevel=0):
"""
Returns a list of emails
"""
# pick the appropriate POP3 class (uses SSL or not)
#pop = [poplib.POP3, poplib.POP3_SSL][cfg['use_ssl']]
emails = []
try:
# connect!
print('Connecting...')
host = cfg['host']
mail = poplib.POP3(host)
mail.set_debuglevel(debuglevel) # 0 (none), 1 (summary), 2 (verbose)
mail.user(cfg['user'])
mail.pass_(cfg['pwd'])
# how many messages?
num_messages = mail.stat()[0]
print('{0} new messages'.format(num_messages))
# get text of messages
if num_messages:
get = lambda i: mail.retr(i)[1] # retrieve each line in the email
txt = lambda ss: '\n'.join(ss) # join them into a single string
eml = lambda s: email.message_from_string(s) # parse the string as an email
print('Getting emails...')
emails = [eml(txt(get(i))) for i in xrange(1, num_messages+1)]
print('Done!')
except poplib.error_proto, e:
print('Email error: {0}'.format(e.message))
mail.quit() # close connection
return emails
def parse_order_page(html):
"""
Accept an HTML order form
Returns (sku, shipto, [items])
"""
bs = BeautifulSoup.BeautifulSoup(html) # parse html
# sku is in first <p>, shipto is in second <p>...
ps = bs.findAll('p') # find all paragraphs in data
sku = ps[0].contents[1].strip() # sku as unicode string
shipto_lines = [line.strip() for line in ps[1].contents[2::2]]
shipto = '\n'.join(shipto_lines) # shipping address as unicode string
# items are in three-column table
cells = bs.findAll('td') # find all table cells
txt = [cell.contents[0] for cell in cells] # get cell contents
items = zip(txt[0::3], txt[1::3], txt[2::3]) # group by threes - code, description, and quantity for each item
return sku, shipto, items
def get_orders(emails):
"""
Accepts a list of order emails
Returns order details as list of (sku, shipto, [items])
"""
orders = []
for i,eml in enumerate(emails, 1):
pl = eml.get_payload()
if isinstance(pl, list):
sku, shipto, items = parse_order_page(pl[1].get_payload())
orders.append([sku, shipto, items])
else:
print("Email #{0}: unrecognized format".format(i))
return orders
def write_to_csv(orders, fname):
"""
Accepts a list of orders
Write to csv file, one line per item ordered
"""
outf = open(fname, 'wb')
outcsv = csv.writer(outf)
for poNumber, shipto, items in orders:
outcsv.writerow([]) # leave blank row between orders
for code, description, qty in items:
outcsv.writerow([poNumber, shipto, code, description, qty])
# The point where mechanize will come to play
def main():
cfg = get_config()
emails = get_emails(cfg)
orders = get_orders(emails)
write_to_csv(orders, 'cron/orderP/{0}.csv'.format(int(time.time())))
if __name__=="__main__":
main()
As we all know that POP3 is used solely for retrieval (those who know or have idea how emails work) so there is no point using POP3 for the sake of message sending that why I mentioned How to forward an email message captured with poplib to a different email address? as an question.
The complete answer was
smtplib can be used for that sake to forward an poplib captured email message, all you need to do is to capture the message body and send it using smtplib to the desired email address. Furthermore as Aleksandr Dezhin quoted I will agree with him as some SMTP servers impose different restrictions on message they are processed.
Beside that you can use sendmail to achieve that if you are on Unix machine.