2013-10-16 2 views
0

전자 메일을 보내는 데 사용하는 다음과 같은 서브 루틴이 있습니다. 사용자 "username1"을 쉼표로 구분 된 전자 메일 ID 인 "to" , 다음에 가입 쉼표로쉼표로 구분 된 전자 메일 ID의 목록에서 변수를 제거합니다.

def email (body,subject,to=None): 
    msg = MIMEText("%s" % body) 
    msg["Content-Type"] = "text/html" 
    msg["From"] = "[email protected]" 
    if to!=None: 
     to=to.strip() 
     to=to.strip('[email protected]') 
     msg["To"] = to 
     print to 

답변

0

분할 그것을 "에서"변수에서 [email protected]을 제거하는 방법, 작업을 나던 = to.strip ('사용자 이름 1')에 어떤 생각을 보인다 사용하고 ... 쉼표 뺀 제외 하나에 그들을 다시 백업

msg['To'] = ','.join(email for email in msg['To'].split(',') if email != '[email protected]') 
또한 "D의 목록을 가지고 그것을 일반화 할 수

오되지 메일 ", 예를 들면 :

DO_NOT_MAIL = ['[email protected]', '[email protected]'] 

def email(body, subject, to=None): 
    msg = MIMEText("%s" % body) 
    msg["Content-Type"] = "text/html" 
    msg["From"] = "[email protected]" 
    msg["To"] = ', '.join(email for email in set(to).difference(DO_NOT_MAIL)) 
0

당신은 목록에을 분리 한 다음 목록에서 원하지 않는 이메일을 제거 할 수 있습니다 :

exclude='[email protected]' 
to=to.split(',') 
to.remove(exclude) 
to=','.join(to) 

여러 주소 목록, 루프 이상이었다 제외하면 목록 :

.... 
for e in exclude: 
    to.remove(e) 
... 
관련 문제