I am trying to send email with below code.
import smtplib
from email.mime.text import MIMEText
sender = \'sender@sender.com\'
def mail_me(cont, receiver):
Just tested the following code with gmx.com and it works fine. Although, whether you get the same mileage is a moot point.
I have replaced all references to my email service with gmail
#!/usr/bin/python
#from smtplib import SMTP # Standard connection
from smtplib import SMTP_SSL as SMTP #SSL connection
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
sender = 'example@gmail.com'
receivers = ['example@gmail.com']
msg = MIMEMultipart()
msg['From'] = 'example@gmail.com'
msg['To'] = 'example@gmail.com'
msg['Subject'] = 'simple email via python test 1'
message = 'This is the body of the email line 1\nLine 2\nEnd'
msg.attach(MIMEText(message))
ServerConnect = False
try:
smtp_server = SMTP('smtp.gmail.com','465')
smtp_server.login('#name#@gmail.com', '#password#')
ServerConnect = True
except SMTPHeloError as e:
print "Server did not reply"
except SMTPAuthenticationError as e:
print "Incorrect username/password combination"
except SMTPException as e:
print "Authentication failed"
if ServerConnect == True:
try:
smtp_server.sendmail(sender, receivers, msg.as_string())
print "Successfully sent email"
except SMTPException as e:
print "Error: unable to send email", e
finally:
smtp_server.close()
This should work:
msg['From'] = "Your name <Your email>"
Example below:
import smtplib
from email.mime.text import MIMEText
def send_email(to=['example@example.com'], f_host='example.example.com',
f_port=587, f_user='example@example.com', f_passwd='example-pass',
subject='default subject', message='content message'):
smtpserver = smtplib.SMTP(f_host, f_port)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(f_user, f_passwd) # from email credential
msg = MIMEText(message, 'html')
msg['Subject'] = 'My custom Subject'
msg['From'] = "Your name <Your email>"
msg['To'] = ','.join(to)
for t in to:
smtpserver.sendmail(f_user, t, msg.as_string()) # you just need to add
this in for loop in your code.
smtpserver.close()
print('Mail is sent successfully!!')
cont = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.google.com">link</a> you wanted.
</p>
</body>
</html>
"""
try:
send_email(message=cont)
except:
print('Mail could not be sent')
import smtplib
from email.mime.text import MIMEText
def send_email(to=['example@example.com'], f_host='example.example.com', f_port=587, f_user='example@example.com', f_passwd='example-pass', subject='default subject', message='content message'):
smtpserver = smtplib.SMTP(f_host, f_port)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(f_user, f_passwd) # from email credential
msg = MIMEText(message, 'html')
msg['Subject'] = 'My custom Subject'
msg['From'] = "Admin"
msg['To'] = ','.join(to)
for t in to:
smtpserver.sendmail(f_user, t, msg.as_string()) # you just need to add this in for loop in your code.
smtpserver.close()
print('Mail is sent successfully!!')
cont = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.google.com">link</a> you wanted.
</p>
</body>
</html>
"""
try:
send_email(message=cont)
except:
print('Mail could not be sent')
above method I have tried to send mail which is worked for me even I am able to send mail to my gmail account(in spam folder). Let me know if you face any other related problem.
A space is not a valid character in an email address. Special characters are only allowed in the external representation that shall be enclosed in double quotes. Additionaly, most SMTP servers actually use header rewriting to ensure that addresses are in standard format. As yours actually contains a space it is splitted and as it is not enclosed in quotes, the server address is appended to it.
You have just to replace msg['From'] = "XYZ ABC"
with
msg['From'] = '"XYZ ABC"'
forcing inclusion of the address in double quotes.
The name comes from the FROM
header. Refer to this answer please Specify a sender when sending mail with Python (smtplib)
This should fix it:
Replace mail_me(cont,['xyz@xyzcom'])
with
mail_me(cont,'xyz@xyz.com')