2

Another program send to my script already finished letter:

http://pastebin.com/XvnMrKzE

So, i parse from_email and to_email, do some changes in text and send it with mailjet.

When i did this with smtp:

def send(sender, to, message):
    smtp = smtplib.SMTP(SERVER, PORT)
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo()
    smtp.login(USER,PASSWORD)
    logger.info('Sending email from %s to %s' % (sender, to))
    smtp.sendmail(sender, to, message)
    logger.info('Done')
    smtp.quit()

It worked fine. Then i need to use mailjet. I created similar function:

def send_with_mailjet(sender, to, message):
    mailjet = Client(auth=('key', 'key'))
    email = {
        'FromName': 'Support',
        'FromEmail': sender,
        'Subject': 'Voice recoginition',
        'Text-Part': message,
        'Html-part': message,
        'Recipients': [{'Email': to},]
    }
    logger.info('Sending email from %s to %s' % (sender, to))
    result = mailjet.send.create(email)
    logger.info('Done. Result: %s' % result)

But i received text, not attachment in mailbox.

Arti
  • 7,356
  • 12
  • 57
  • 122
  • Thanks for choosing Mailjet to power your emails. Your Python API call doesn't seem to specify any attachment. Please refer to this API guide to learn how to define them http://dev.mailjet.com/guides/?python#sending-with-attached-files – arnaud.breton May 31 '16 at 07:58
  • @arnaud.breton yes, i solved this issue, but got another: https://github.com/WoLpH/mailjet/issues/23 Could you check it please ? – Arti May 31 '16 at 08:06

1 Answers1

2

You should be using the official Mailjet wrapper which is a Mailjet-maintained API client. As specified in the documentation, here is the way to send your attachment: http://dev.mailjet.com/guides/?python#sending-with-attached-files

"""
This calls sends an email to the given recipient.
"""
from mailjet import Client
import os
api_key = os.environ['MJ_APIKEY_PUBLIC']
api_secret = os.environ['MJ_APIKEY_PRIVATE']
mailjet = Client(auth=(api_key, api_secret))
data = {
  'FromEmail': 'pilot@mailjet.com',
  'FromName': 'Mailjet Pilot',
  'Subject': 'Your email flight plan!',
  'Text-part': 'Dear passenger, welcome to Mailjet! May the delivery force be with you!',
  'Html-part': <h3>Dear passenger, welcome to Mailjet!</h3>May the delivery force be with you!',
  'Recipients': [{ "Email": "passenger@mailjet.com"}],
  'Attachments':
        [{
            "Content-type": "text/plain",
            "Filename": "test.txt",
            "content": "VGhpcyBpcyB5b3VyIGF0dGFjaGVkIGZpbGUhISEK"
        }]
}
result = mailjet.send.create(data=data)
print result.status_code
print result.json()
Guillaume Badi
  • 749
  • 5
  • 15