Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien

met python gemaakt HTML mail verzenden levert error op.

Pagina: 1
Acties:

  • dezejongeman
  • Registratie: Juli 2006
  • Laatst online: 17-10 12:31
ik ben al de hele dag aan het stoeien met python om een HTML opmaakte mail te verzenden naar een mail adres.

de mail wordt verzonden maar de melding blijft komen.

de cgi module draait op iis7.5

de melding "
HTTP Error 502.2 - Bad Gateway
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are "Successfully sent email ".

blijft maar terug komen en ik ben er vrijwel van overtuigd dat het door mijn programming skills komt.

aan deze code gaat nog een simpel html formuliertje aan vooraf waar een mail adres in te vullen is.

Python:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import cgi, cgitb
import smtplib


form = cgi.FieldStorage()  # wordt in opgslagen


message = """From: iemand <mail@iets.com>
To: To Person <to@todomain.com>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP HTML e-mail test
tekst van het mailtje
<b>tekst in t mailtje</b>
<h1>grote letters</h1>
"""

sender = '*@*.*'
receivers = form.getvalue('mail')

try:
   smtpObj = smtplib.SMTP('*.*.*.8',25) # smtp 
   smtpObj.sendmail(sender, receivers, message)
   print "Successfully sent email"
except:
   print "Error: unable to send email"

  • Elijan9
  • Registratie: Februari 2004
  • Laatst online: 20-11 15:32
Zoals de foutmelding al aangeeft, moet een CGI applicatie ook minimaal enkele HTTP headers zetten. Je wilt nu meteen aan de body beginnen, maar je zult toch echt eerst minimaal het "Content-Type" moeten opgeven bij het header gedeelte en je moet ook een lege regel printen tussen de HTTP headers en de HTTP body.

Dit zou je op weg moeten helpen, inclusief de links bij "See also":
https://wiki.python.org/moin/CgiScripts

War is when the young and stupid are tricked by the old and bitter into killing each other. - Niko Bellic


  • wartos
  • Registratie: December 2006
  • Laatst online: 21-11 14:38
Ik heb zelf ook ooit eens een scriptje geschreven om makkelijk mailtjes te versturen - voornamelijk bedoeld om logfiles van een remote computer naar mezelf te sturen...
Anyway, ik gebruik gmail om die te versturen, maar het principe om een html mail te maken is wel gelijkaardig.


Python:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
# This Python file uses the following encoding: utf-8

import sys
import types
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

class Mail(object):
    
    def __init__(self):
        self.username = 'from@example.com'
        self.password = 'MySuperDuperPassword'
    
    def send(self, message, subject, to, attachments=None):
        msg = MIMEMultipart('alternative')
        msg['Subject'] = subject
        msg['From'] = self.username
        msg['To'] = to

        if type(attachments) != types.NoneType:
            for attachment in attachments:
                filename = attachment
                f = file(filename)
                attachment = MIMEText(f.read())
                attachment.add_header('Content-Disposition', 'attachment', filename=filename)           
                msg.attach(attachment)

        body = message

        content = MIMEText(body, 'html')
        msg.attach(content)

        try:
            server = smtplib.SMTP('smtp.gmail.com:587')
            server.ehlo()
            server.starttls()
            server.login(self.username, self.password)
            server.sendmail(self.username, [to], msg.as_string())
            server.quit()
            print 'Succesfully send mail'
        except Exception as inst:
            print 'Could not send mail:'
            print inst

if __name__ == "__main__":

    mail = Mail()
    message = """\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>
       <b>Here</b>is some <i>HTML</i> as you wanted.
    </p>
  </body>
</html>
"""
    mail.send(message=message, subject='Testmail with html', to='to@example.com', attachments=[])


Het belangrijkste is dat je met MIMEText je content creeert.
Python:
1
2
3
4
5
6
7
8
9
10
11
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = from
msg['To'] = to

body = message
content = MIMEText(body, 'html')
msg.attach(content)