Python SMTPLIB Exchange Server connection to send HTML e-mail

from email.mime.text import MIMEText
from email.header import Header
import smtplib


def mail(applicationname, passvalue, maxdev, begindate, enddate, cc, reason):
    smtpaddr = 'smtp.algex.asmpt.com'
    myemail = "[email protected]"
    password = 'Spsaccount'
    # f = open('/usr/local/zabbix/.passwd','r')
    # password = f.readline().strip()

    # recvmail=sys.argv[1]
    # subject=sys.argv[2]
    # content=sys.argv[3]
    subject = "auto generate key"
    recvmail = "[email protected]"
    content = '''<html><head>
        <title>Quick Start Guide: WLAN Guest Connection</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <style type="text/css">
            p {
                font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
                font-size: x-small;
                font-style: normal;
            }
            li {
                font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
                font-size: x-small;
                font-style: normal;
                line-height: normal;
                padding: 8px;
            }
            td {
                font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
                font-size: x-small;
                font-style: normal;
                vertical-align: top;
                text-align: left;
            }
            th {
                font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
                font-size: x-small;
                font-style: bold;
                color: #000000;
                vertical-align: top;
                text-align: left;
            }
            .bg_black-white {
                font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
                font-size: x-small;
                font-style: normal;
                font-weight: bold;
                color: #FFFFFF;
                background-color: #000000;
                vertical-align: top;
                text-align: left;
                font-weight: bold;
            }
            .bg_gray-white {
                font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
                font-size: x-small;
                font-style: normal;
                font-weight: bold;
                color: #FFFFFF;
                background-color: #CCCCCC;
                vertical-align: top;
                text-align: left;
                font-weight: bold;
            }
            .bg_light-gray {
                font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
                font-size: x-small;
                font-style: normal;
                font-weight: bold;
                color: #000000;
                background-color: #CCCCCC;
                vertical-align: top;
                text-align: left;
                font-weight: bold;
            }
            .bg_darkgray-white {
                font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
                font-size: x-small;
                font-style: normal;
                font-weight: bold;
                color: #FFFFFF;
                background-color: #888888;
                vertical-align: top;
                text-align: left;
                font-weight: bold;
            }
            .bg_alternate01 {
                font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
                font-size: x-small;
                font-style: normal;
                font-weight: bold;
                color: #000000;
                background-color: #FFCC77;
                vertical-align: top;
                text-align: left;
                font-weight: bold;
            }
            h1 {
                font-family: Georgia, "Times New Roman", Times, serif;
                font-size: large;
                font-style: normal;
                font-weight: bold;
                color: #666666;
            }
            h2 {
                font-family: Verdana, Arial, Helvetica, sans-serif;
                font-size: small;
                font-style: normal;
                font-weight: bolder;
                color: #990066;
                border-top: thin dotted #990033;
                border-right: thin none #990033;
                border-bottom: thin none #990033;
                border-left: thin none #990033;
                padding-top: 12px;
                padding-right: 0px;
                padding-bottom: 6px;
                padding-left: 0px;
            }
            .boilerplate {
                font-family: Verdana, Arial, Helvetica, sans-serif;
                font-size: xx-small;
                font-style: normal;
                font-weight: normal;
                color: #999999;
                text-align: center;
                border: none;
            }
            h3 {
                font-family: Georgia, "Times New Roman", Times, serif;
                font-size: small;
                font-style: italic;
                font-weight: bold;
                color: #336633;
            }
            .text {
                font-family: Arial, Helvetica, sans-serif;
                font-size: x-small;
                font-style: normal;
                font-weight: bold;
                color: #FFFFFF;
                background-color: #CC0000;
                vertical-align: top;
                text-align: left;
                font-weight: bold;
           }
            </style>
    <style type="text/css">
    table.gridtable {
        font-family: verdana,arial,sans-serif;
        font-size:11px;
        color:#333333;
        border-width: 1px;
        border-color: #666666;
        border-collapse: collapse;
    }
    table.gridtable th {
        border-width: 1px;
        padding: 6px;
        border-style: solid;
        border-color: #666666;
        background-color: #dedede;
    }
    table.gridtable td {
        border-width: 1px;
        padding: 10px;
        border-style: solid;
        border-color: #666666;
        background-color: #ffffff;
    }
    </style>
    </head>
    <body>


    <p>Dear <strong>%s</strong>, </p>
    <p>Your request has been grandted as below details, thanks.</p>
    <!-- Table goes in the document BODY -->
    <table class="gridtable">
    <tr>
        <th>User</th><th>Workplace</th><th>Key</th><th>Max devices</th><th>Begin date</th><th>End date</th><th>Reason</th>
    </tr>
    <tr>
        <td>%s</td><td>ALG</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>
    </tr>
    </table>

    <h2>How to connect</h2>
    <p>Using your guest pass to connect requires a series of two procedures:
     (1) connecting your device to the "ASMPT-Guest" network, then (2) logging
     in as a qualified guest.</p>
    <h3>Finding the Wireless "ASMPT-Guest" Network</h3>
    <table border="0" cellpadding="6" cellspacing="1">
        <tbody><tr>
            <td class="bg_alternate01"> 1 </td>
            <td>On your PC/Windows desktop, check the system tray for a Wireless Connection icon.</td>
        </tr>
        <tr>
            <td class="bg_alternate01"> 2 </td>
            <td>Click on this icon to display the wireless connection list</td>
        </tr>
        <tr>
            <td class="bg_light-gray"> </td>
            <td>"ASMPT-Guest" signal will be listed in the wireless connection list</td>
        </tr>
        <tr>
            <td class="bg_alternate01"> 3 </td>
            <td>Select "ASMPT-Guest" and click <b>Connect</b>.</td>
        </tr>
        <tr>
            <td class="bg_alternate01"> 4 </td>
            <td>If a Wireless Network Connection confirmation dialog box asks you to confirm "connecting to an unsecured network", click <b>Connect Anyway</b>.</td>
        </tr>
        <tr>
            <td class="bg_light-gray"> </td>
            <td>A connection status dialog may appear, while a network address is obtained and initial connection established.</td>
        </tr>
        <tr>
            <td class="bg_alternate01"> 5 </td>
            <td>If the Wireless Network Connection window displays <b>"Connected"</b>, you can close this window and proceed to the next procedure.</td>
        </tr>
    </tbody></table>
    <h3>Logging into the Network as a Guest</h3>
    <table border="0" cellpadding="6" cellspacing="1">
        <tbody><tr>
            <td class="bg_alternate01"> 1 </td>
            <td>Start a web browser and try to connect to any valid Internet site.</td>
        </tr>
        <tr>
            <td class="bg_alternate01"> 2 </td>
            <td>If a security warning for the Subscriber Portal page appears, trust the Subscriber Portal site and do all the actions that are required to continue browsing</td>
        </tr>
        <tr>
            <td class="bg_light-gray"> </td>
            <td>The Subscriber Portal "Sign In" page appears.</td>
        </tr>
        <tr>
            <td class="bg_alternate01"> 3 </td>
            <td>At the <b>Guest Pass</b> text box, enter the text of your guest pass key (by typing or pasting), read the <b>Terms of Use</b> section, check <b>Accept Terms of Use</b> and click <b>Sign In</b>.</td>
        </tr>
        <tr>
            <td class="bg_light-gray"> </td>
            <td>The guest wireless network "Sign Out" page appears and indicates that you are now signed in to the Subscriber Portal.</td>
        </tr>
        <tr>
            <td class="bg_alternate01"> 4 </td>
            <td>If the page you tried to open is not displayed, use the browser's popup blocker to display it.</td>
        </tr>
        <tr>
            <td class="bg_alternate01"> 5 </td>
            <td>You can now check your personal email and browse the Web.</td>
        </tr>
        <tr>
            <td class="bg_alternate01"> 6 </td>
            <td>When you finish browsing, click on <b>Sign Out</b> at the Sign Out page.</td>
        </tr>
    </tbody></table>
    <h2>Important</h2>
    <p>With a guest pass, you have access to the internet and World Wide
    web, but you cannot use any services such as file sharing, intranets,
    corporate email or printers.  If these services are needed, you can turn to SOG team for help to set you up with
    full user access to the secure network.</p>
    <hr>





    </body></html>''' % (applicationname, applicationname, passvalue, maxdev, begindate, enddate, reason)
    msg = MIMEText(content, "HTML", "utf-8")
    msg['Subject'] = Header(subject, 'utf-8').encode()
    msg['From'] = myemail
    msg['To'] = recvmail

    try:
        smtp = smtplib.SMTP(smtpaddr,587)  
        # smtp.set_debuglevel (1) # enabled debugging
        smtp.starttls()
        smtp.login(myemail, password)
        smtp.sendmail(myemail, recvmail.split(','), msg.as_string())
        smtp.quit()
        print("success")
    except Exception as e:
        print("fail: " + str(e))

In the beginning of smtp = smtplib.SMTP (smtpaddr, 25), the error message is: No suitable authentication method found at this time the solution is to change the phrase smtp = smtplib.SMTP (smtpaddr, 587), then you can. successfully sent

The question now is, when the exchange with individual mailbox to run this code, will get an error: 535, b'5.7.3 Authentication unsuccessful ', is estimated to be a problem exchange Account Settings, and then after the next update to resolve.

Guess you like

Origin www.cnblogs.com/jpinsz/p/12418876.html