Guest User

Untitled

a guest
Aug 28th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. Permission Denied error while sending mail using Python smtplib
  2. #from email import Encoders
  3. from email.mime.base import MIMEBase
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.text import MIMEText
  6. from email.utils import COMMASPACE, formatdate
  7. import os
  8. import smtplib
  9. from base64 import encode
  10. from email import encoders
  11.  
  12. def sendMail(to, subject, text, files=[],server="smtp.mydomain.com"):
  13. assert type(to)==list
  14. assert type(files)==list
  15. fro = "From <myemail@mydomain.com>"
  16.  
  17. msg = MIMEMultipart()
  18. msg['From'] = fro
  19. msg['To'] = COMMASPACE.join(to)
  20. msg['Date'] = formatdate(localtime=True)
  21. msg['Subject'] = subject
  22.  
  23. msg.attach( MIMEText(text) )
  24.  
  25. for file in files:
  26. part = MIMEBase('application', "octet-stream")
  27. part.set_payload( open(file,"rb").read() )
  28. encoders.encode_base64(part)
  29. part.add_header('Content-Disposition', 'attachment; filename="%s"'
  30. % os.path.basename(file))
  31. msg.attach(part)
  32.  
  33. smtp = smtplib.SMTP_SSL(server, 465)
  34. smtp.ehlo()
  35. smtp.set_debuglevel(1)
  36. smtp.ehlo()
  37. smtp.login("myemail@mydomain.com", "mypassword")
  38. smtp.ehlo()
  39. smtp.sendmail(fro, to, msg.as_string() )
  40. smtp.close()
  41. print("Email send successfully.")
  42.  
  43. sendMail(
  44. ["recipient"],
  45. "hello","cheers",
  46. []
  47. )
  48.  
  49. raise SMTPSenderRefused(code, resp, from_addr)
  50. smtplib.SMTPSenderRefused: (501, b'5.7.1 <myemail@mydomain.com>... Permission denied', 'myemail@mydomain.com')
Add Comment
Please, Sign In to add comment