2017-12-29 32 views
1

私はsmtp server.butを使用してpythonから電子メールを送信しようとしていますが、これは エラーをスローします。どうすれば解決できますか? また、私はここで、この機能 を使用するようにGmailからの許可を得ています。このエラー はTypeError通じコードPythonから電子メールを送信

import smtplib 


content='Hello I am just checking email.' 
mail=smtplib.SMTP('smtp.gmail.com',587) 
mail.ehlo() 
mail.starttls() 
mail.login('My email','Mypassword') 
mail.send('From email','destiation password',content) 
mail.close() 

このコードされています(送信)は2つの位置引数を取りますが、4は

を与えられたこの問題を修正してくださいエラー。

+0

は代わりに私はすでに答えを得た 'send' –

答えて

3

sendmailは、あなたが使うべきものです。

smtplib.SMTP.sendmail(self, from_addr, to_addrs, msg, mail_options=[], rcpt_options=[]) 

This command performs an entire mail transaction.

The arguments are:

- from_addr : The address sending this mail. 
- to_addrs  : A list of addresses to send this mail to. A bare 
       string will be treated as a list with 1 address. 
- msg   : The message to send. 
- mail_options : List of ESMTP options (such as 8bitmime) for the 
       mail command. 
- rcpt_options : List of ESMTP options (such as DSN commands) for 
       all the rcpt commands. 
0
import smtplib 
import email 
from email.MIMEMultipart import MIMEMultipart 
from email.Utils import COMMASPACE 
from email.MIMEBase import MIMEBase 
from email.parser import Parser 
from email.MIMEImage import MIMEImage 
from email.MIMEText import MIMEText 
from email.MIMEAudio import MIMEAudio 
import mimetypes 

def send(user, password, fromaddr, to, subject, body): 
smtp_host = 'smtp.gmail.com' 
smtp_port = 587 
server = smtplib.SMTP() 
server.connect(smtp_host,smtp_port) 
server.ehlo() 
server.starttls() 
server.login(user, password) 

msg = email.MIMEMultipart.MIMEMultipart() 
msg['From'] = fromaddr 
msg['To'] = email.Utils.COMMASPACE.join(to) 
msg['Subject'] = subject 
msg.attach(MIMEText(body)) 
server.sendmail(user,to,msg.as_string()) 
+0

のsendmail''てみてください –

関連する問題