himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

error in MIME... can't send email

Tue Mar 22, 2016 4:56 pm

Hello..

I have tried following code for email...

Code: Select all

# Import smtplib to provide email functions
import smtplib
 
# Import the email modules
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
 
# Define email addresses to use
addr_to   = 'domain1@gmail.com'
addr_from = 'domain1@gmail.com'
 
# Define SMTP email server details
smtp_server = 'domain1@gmail.com'
smtp_user   = 'domain1@gmail.com'
smtp_pass   = '123excerty'
 
# Construct email
msg = MIMEMultipart('alternative')
msg['To'] = addr_to
msg['From'] = addr_from
msg['Subject'] = 'Test Email From RPi'
 
# Create the body of the message (a plain-text and an HTML version).
text = "This is a test message.\nText and html."
html = """\
<html>
  <head></head>
  <body>
    <p>This is a test message.</p>
    <p>Text and HTML</p>
  </body>
</html>
"""
 
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
 
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
 
# Send the message via an SMTP server
s = smtplib.SMTP(smtp_server)
s.login(smtp_user,smtp_pass)
s.sendmail(addr_from, addr_to, msg.as_string())
s.quit()
but I got this error

Traceback (most recent call last):
File "email1.py", line 46, in <module>
s = smtplib.SMTP(smtp_server)
File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known


please can anyone solve this for me...

PS: i havent changed any config file...

I have kept the same config file that I used in my previous email program for sending simple text.. :evil:

please help me :shock:

Return to “Beginners”