2016-04-21 17 views
1

Meine E-Mail ist: [email protected]Senden E-Mail über Python und Google SMTP-Server

Ich möchte eine E-Mail von [email protected] zu Mine schicken.

Ich möchte mich nicht per Fernzugriff mit meinem Google Mail-Konto verbinden, um eine E-Mail zu senden.

Das ist, was ich brauche:

Ich bin Sarah Connor, und ich möchte in meinem Briefkasten ([email protected]), eine E-Mail von [email protected] ...

Also ich erhalten ve dieses Skript verwenden zu tun, so:

import requests 
import smtplib 

from email.mime.image import MIMEImage 
from email.mime.multipart import MIMEMultipart 

COMMASPACE = ', ' 

msg = MIMEMultipart() 
msg['Subject'] = 'Our family reunion' 
fromm = "[email protected]" 
to = "[email protected]" 
msg['From'] = fromm 
msg['To'] = COMMASPACE.join(to) 
msg.preamble = 'Our family reunion' 

requests.get("http://smtp.gmail.com", verify=False) 
s = smtplib.SMTP_SSL("smtp.gmail.com", 587) 
s.starttls() 
s.login('[email protected]', 'mypassword12345') #here I login to the SMTP server from Google to be able to send emails... 
s.sendmail(fromm, to, msg.as_string()) 
s.close() 

ich habe folgende Fehlermeldung:

raise ConnectionError(err, request=request) 
requests.exceptions.ConnectionError: ('Connection aborted.', error(10054, 'An existing connection was forcibly closed by the remote host')) 

Und von here gesehen, scheint es nicht, dass ich irgendwelche dieser Probleme habe.

Weiß jemand, wie kann ich das beheben?

Dank

Antwort

1

diese sendet eine Mail von ein Google Mail-Konto auf andere Konten

import smtplib 
from email.mime.text import MIMEText 


class GmailHandler(): 
    """ 
    IMPORTANT NOTE: 
    in order to access a gmail account with this handler, 
    your account needs 'foreign-access' enabled (follow these steps): 
    login to the account 
    go here--> https://accounts.google.com/b/0/DisplayUnlockCaptcha 
    press 'Continue' 
    Done. 
    """ 

    def __init__(self, gmail, password): 
     self.gmail = gmail 
     self.password = password 

    def send_mail(self, receivers, subject, text): 

     if not isinstance(receivers, list): 
      receivers = [receivers] 

     # Send the message via our own SMTP server, but don't include the envelope header 
     smtp = smtplib.SMTP("smtp.gmail.com", 587) 
     smtp.ehlo() 
     smtp.starttls() 
     smtp.ehlo() 
     smtp.login(self.gmail, self.password) 

     for receiver in receivers: 

      msg = MIMEText(text) 
      msg['Subject'] = subject 
      msg['From'] = self.gmail 
      msg['To'] = receiver 
      smtp.sendmail(self.gmail, receiver, str(msg)) 

     smtp.quit() 
+0

ok danke, aber das ist nicht, was ich brauche :(Ich muss eine E-Mail von einer anderen E-Mail senden, an meine E-Mail ... nicht von einem Gmail-Konto. Wie ich sagte: "Ich möchte nicht remote einloggen Mein Google Mail-Konto, um eine E-Mail zu senden. " – waas1919

1

Verwenden smtplib.SMTP statt smtplib.SMTP_SSL.