コード例 #1
1
ファイル: mail.py プロジェクト: Akhilrajns/python-mailer
def mail(email):
    host = 'smtp.gmail.com'
    port = '587'
    user = '******'
    password = '******'

    fromaddr = "Mailer Application"
    fromAddress = Utils.formataddr((fromaddr,user))
    toAddress = "*****@*****.**"


    randno = random.randrange(0, 99999)


    subject = "Python mailer %d" % randno
    msgRoot = MIMEMultipart('related')
    msgRoot['Subject'] = subject
    msgRoot['From'] = fromAddress
    msgRoot['To'] = toAddress
    msgRoot.preamble = 'This is a multi-part message in MIME format.'

    # Encapsulate the plain and HTML versions of the message body in an
    # 'alternative' part, so message agents can decide which they want to display.
    msgAlternative = MIMEMultipart('alternative')
    msgRoot.attach(msgAlternative)

    msgText = MIMEText('This is the alternative plain text message.')
    msgAlternative.attach(msgText)


    ft = open('mail-content.html', 'rb')
    msgTexts = MIMEText(ft.read(), 'html',_charset="utf-8")
    ft.close()
    msgAlternative.attach(msgTexts)

    # We reference the image in the IMG SRC attribute by the ID we give it below
    #msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><img src="cid:image2"><br>Nifty!', 'html')
    #msgAlternative.attach(msgText)


    # This example assumes the image is in the current directory
    fp = open('images/moneybooker.jpeg', 'rb')
    msgImage = MIMEImage(fp.read())
    fp2 = open('images/payex.png', 'rb')
    msgImage2 = MIMEImage(fp2.read())
    fp.close()
    fp2.close()

    # Define the image's ID as referenced above
    msgImage.add_header('Content-ID', '<image1>')
    msgRoot.attach(msgImage)
    msgImage2.add_header('Content-ID', '<image2>')
    msgRoot.attach(msgImage2)



    smtp = smtplib.SMTP()
    smtp.connect(host, port)
    smtp.ehlo()
    smtp.starttls()
    smtp.login(user, password)
    #mailid = re.sub("([(',)])","",str(email))
    #print 'mail send to ',mailid

    try:
        smtp.sendmail(user, toAddress, msgRoot.as_string())
        print 'Success'
    except Exception, exc:
        print 'Mail send Failed',exc
コード例 #2
0
ファイル: helper.py プロジェクト: anand700/savier
def prepare(
    message,
    from_name,
    from_email,
    to=[],
    cc=[],
    bcc=[],
    subject='(no subject)',
):
    if not to and not cc and not bcc:
        raise MailError("No recipients specified")
    message['From'] = Utils.formataddr((from_name, from_email))
    message['Subject'] = subject
    if isinstance(to, (unicode, str)):
        to = [to]
    if to:
        message['To'] = ', '.join(to)
    if isinstance(cc, (unicode, str)):
        cc = [cc]
    if cc:
        message['Cc'] = ', '.join(cc)
    if isinstance(bcc, (unicode, str)):
        bcc = [bcc]
    if bcc:
        message['Bcc'] = ', '.join(bcc)
    return message
コード例 #3
0
    def configure_sender(self, sender):
        """Properly formats the sender address.

        :param sender: A dictionary containing information about the sender.

        """
        return Utils.formataddr((sender['real_name'], sender['address']))
コード例 #4
0
ファイル: emailer.py プロジェクト: professor0x/Alarmageddon
    def configure_sender(self, sender):
        """Properly formats the sender address.

        :param sender: A dictionary containing information about the sender.

        """
        return Utils.formataddr((sender['real_name'], sender['address']))
コード例 #5
0
ファイル: helper.py プロジェクト: karatemelli/smart-home
def prepare(
    message,
    from_name,
    from_email,
    to=[], 
    cc=[], 
    bcc=[], 
    subject='(no subject)',
):
    if not to and not cc and not bcc:
        raise MailError("No recipients specified")
    message['From'] = Utils.formataddr((from_name, from_email))
    message['Subject'] = subject
    if isinstance(to, (unicode, str)):
        to=[to]
    if to:
        message['To'] = ', '.join(to)
    if isinstance(cc, (unicode, str)):
        cc=[cc]
    if cc:
        message['Cc'] = ', '.join(cc)
    if isinstance(bcc, (unicode, str)):
        bcc=[bcc]
    if bcc:
        message['Bcc'] = ', '.join(bcc)
    return message
コード例 #6
0
ファイル: utils.py プロジェクト: pombredanne/Euphorie
def CreateEmailTo(sender_name, sender_email, recipient, subject, body):
    mail = MIMEMultipart('alternative')
    mail['From'] = emailutils.formataddr((sender_name, sender_email))
    mail['To'] = recipient
    mail['Subject'] = Header(subject.encode('utf-8'), 'utf-8')
    mail['Message-Id'] = emailutils.make_msgid()
    mail['Date'] = emailutils.formatdate(localtime=True)
    mail.set_param('charset', 'utf-8')
    if isinstance(body, unicode):
        mail.attach(MIMEText(body.encode('utf-8'), 'plain', 'utf-8'))
    else:
        mail.attach(MIMEText(body))

    return mail
コード例 #7
0
    def address_to_encoded_header(address):
        """RFC2047-encode an address if necessary.

        :param address: An unicode string, or UTF-8 byte string.
        :return: A possibly RFC2047-encoded string.
        """
        # Can't call Header over all the address, because that encodes both the
        # name and the email address, which is not permitted by RFCs.
        user, email = Utils.parseaddr(address)
        if not user:
            return email
        else:
            return Utils.formataddr((str(Header.Header(safe_unicode(user))),
                email))
コード例 #8
0
    def configure_recipients(self, recipients):
        """Properly formats the list of recipient addresses.

        :param recipients: A list containing dictionaries of information about
          the recipients.

        """
        # Recipients are expected to be in an
        # array of objects containing {real_name, address}

        addresses = []

        for recipient in recipients:
            addresses.append(Utils.formataddr((recipient['real_name'],
                                               recipient['address'])))

        # sendmail requires the recipients to be an array of addresses
        return addresses
コード例 #9
0
ファイル: emailer.py プロジェクト: professor0x/Alarmageddon
    def configure_recipients(self, recipients):
        """Properly formats the list of recipient addresses.

        :param recipients: A list containing dictionaries of information about
          the recipients.

        """
        # Recipients are expected to be in an
        # array of objects containing {real_name, address}

        addresses = []

        for recipient in recipients:
            addresses.append(
                Utils.formataddr(
                    (recipient['real_name'], recipient['address'])))

        # sendmail requires the recipients to be an array of addresses
        return addresses
コード例 #10
0
ファイル: mail.py プロジェクト: Akhilrajns/python-mailer
def mail(email):
    host = 'smtp.gmail.com'
    port = '587'
    user = '******'
    password = '******'

    fromaddr = "Mailer Application"
    fromAddress = Utils.formataddr((fromaddr, user))
    toAddress = "*****@*****.**"

    randno = random.randrange(0, 99999)

    subject = "Python mailer %d" % randno
    msgRoot = MIMEMultipart('related')
    msgRoot['Subject'] = subject
    msgRoot['From'] = fromAddress
    msgRoot['To'] = toAddress
    msgRoot.preamble = 'This is a multi-part message in MIME format.'

    # Encapsulate the plain and HTML versions of the message body in an
    # 'alternative' part, so message agents can decide which they want to display.
    msgAlternative = MIMEMultipart('alternative')
    msgRoot.attach(msgAlternative)

    msgText = MIMEText('This is the alternative plain text message.')
    msgAlternative.attach(msgText)

    ft = open('mail-content.html', 'rb')
    msgTexts = MIMEText(ft.read(), 'html', _charset="utf-8")
    ft.close()
    msgAlternative.attach(msgTexts)

    # We reference the image in the IMG SRC attribute by the ID we give it below
    #msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><img src="cid:image2"><br>Nifty!', 'html')
    #msgAlternative.attach(msgText)

    # This example assumes the image is in the current directory
    fp = open('images/moneybooker.jpeg', 'rb')
    msgImage = MIMEImage(fp.read())
    fp2 = open('images/payex.png', 'rb')
    msgImage2 = MIMEImage(fp2.read())
    fp.close()
    fp2.close()

    # Define the image's ID as referenced above
    msgImage.add_header('Content-ID', '<image1>')
    msgRoot.attach(msgImage)
    msgImage2.add_header('Content-ID', '<image2>')
    msgRoot.attach(msgImage2)

    smtp = smtplib.SMTP()
    smtp.connect(host, port)
    smtp.ehlo()
    smtp.starttls()
    smtp.login(user, password)
    #mailid = re.sub("([(',)])","",str(email))
    #print 'mail send to ',mailid

    try:
        smtp.sendmail(user, toAddress, msgRoot.as_string())
        print 'Success'
    except Exception, exc:
        print 'Mail send Failed', exc