Export all Mail Messages

Ok, i am a novice (read: VERY NOVICE) programmer. I have managed to cobble together this python script to export all mail messages into a csv file. I have a list of all Message ID’s and i am iterating through the list to call the API and append it to a csv doc.

import urllib3
import json
import csv

with open(‘blim1.csv’, newline=‘’) as csvfile:
idents = csv.reader(csvfile, delimiter=’ ‘, quotechar=’|')
datas = list()
for row in idents:

    http = urllib3.PoolManager()
    r = http.request('GET', "".join(row)+'?include_body=1&api_token=token')
    mails = json.loads(r.data.decode('utf-8'))
    datas.append(mails)

#{'origin': '127.0.0.1'}

with open(‘test.csv’, “w”, encoding=“utf-8”) as output:
writer = csv.writer(output,delimiter=‘,’,lineterminator=‘\n’)
for data in datas:
writer.writerow([data])

Potentially I am doing this in a very stupid fashion. Is there anyway to export all mail messages with the body in it that is more efficient than this and have it in a usable csv format(i am guessing yes).

Thanks in advance.

Hey @robert.millard, here is an example that should be helpful:

import urllib2
import json

message_ids = [672798, 672777]
api_token = ‘***’
url_prefix = ’https://your-company-domain.pipedrive.com/api/v1/mailbox/

for message_id in message_ids:
message_url = url_prefix + ‘mailMessages/’ + str(message_id) + ‘?api_token=’ + api_token
response = urllib2.urlopen(message_url)
data = json.load(response)
print data

1 Like