Sending emails from CPanel email using python script - python

I am trying to send emails from the Cpanel email but everything I try I face the same error
[Errno 11001] getaddrinfo failed
this is my code, I tried it with Google domain and it works fine but the Cpanel is not working ? is there a way around it to use python for sending emails ?
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from email.mime.image import MIMEImage
email_user = 'something#Mydomain.com'
email_password = 'password'
email_send = "someemail#gmail.com"
subject = "Testing 2"
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
h = str("Holder")
c = str("course")
t = str("trainer")
html = """\
<html>
<head></head>
<body>
<p >Hello {h} This {t} we need your conf. on {c} </p>
</body>
</html>
""".format(h=h , c=c , t=t)
msg.attach(MIMEText(html,'html'))
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user,email_password)
server.sendmail(email_user,email_send,msg.as_string())
server.quit()
So what do you think?

yes you have to change server = smtplib.SMTP('yourDomain.com',25)

Related

How i can login in Gmail with Python?

The idea of ​​my code is to send a message to the mail ( x ) from the mail that I go to using the Login in the code. 17 line causes an error.
#main.py
import config as cfg
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
msg = MIMEMultipart()
to_email = 'dasdladalffhalf'
msg['Subject'] = input('Topic: ')
msg['From'] = cfg.LOGIN
msg_body = input('Text: ')
msg.attach(MIMEText(msg_body, 'plain'))
server = smtplib.SMTP_SSL('smtp.gmail.com: 587')
server.login(cfg.LOGIN, cfg.PASSWORD)
server.sendmail(cfg.LOGIN, to_email, msg.as_string())
#config.py
LOGIN = 'login'
PASSWORD = "password"
You will have to enable less secure apps in GMail for this to work. https://myaccount.google.com/lesssecureapps
Sometimes you may also have to complete a captcha while logged into GMail - https://accounts.google.com/DisplayUnlockCaptcha
More info: https://support.google.com/accounts/answer/6010255?hl=en

How to send email in Python without authentication

I am trying to send an email in Python and the reference is a Perl script. So I have the following code:
my $msg = MIME::Lite->new(
From =>'myEmail#Domain',
To =>'someone#Domain',
Subject =>"Test",
Type =>'multipart/related'
);
$msg->attach(Type => 'text/html',
Data => qq{
<body>
<h1> any text here </h1>
</body> }
);
$msg->send();
}
Then I wanted to do the same thing in Python, so I got to use the SMTPLib and chose the Outlook's SMTP server. So I got the following Python code:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
email = 'myEmail#Domain'
password = 'myPassword'
send_to = 'myEmail#Domain'
subject = 'test'
message = '''<body>
<h1>any text here</h1>
</body>
'''
msg = MIMEMultipart()
msg['From'] = email
msg['To'] = send_to
msg['Subject'] = subject
msg.attach(MIMEText(message, 'html'))
server = smtplib.SMTP('smtp-mail.outlook.com', 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.send_message(msg)
But the point is that I couldn't find a MIME::Lite like module for Python that didn't need to login in the sender's email.
Is there a module where I could just send an email without authentication or am I forgetting something?

Email with no attachment is showing paperclip

I am using Python to send a simple HTML email and have the basic code:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
def send_test_mail(body):
sender_email = <SenderEmail>
receiver_email = <ReceiverEmail>
msg = MIMEMultipart()
msg['Subject'] = '[Email Test]'
msg['From'] = sender_email
msg['To'] = receiver_email
msgText = MIMEText('<b>%s</b>' % (body), 'html')
msg.attach(msgText)
try:
with smtplib.SMTP_SSL('smtp.gmail.com', 587) as smtpObj:
smtpObj.ehlo()
smtpObj.login(<LoginUserName>, <LoginPassword>)
smtpObj.sendmail(sender_email, receiver_email, msg.as_string())
except Exception as e:
print(e)
if __name__ == "__main__":
send_test_mail("Welcome to Medium!")
This has been put together from a couple of sources from within the stackexchange community but the intention is only to send a straightforward email.
But, when it is received, I get the paperclip icon indicating there is an attachment when there clearly isn't one.
https://i.stack.imgur.com/Ysj3g.png
Python is not my strong point but I'm sure it has more to do with SMTPLIB rather than python.
Does anyone know what is going on here?
Thanks

HTML tables not opening in Outlook (pd.to_html)

I am using pd.to_html for sending my pandas dataframe over email, the emails are opening fine in Gmail browser with all the tables in the email body. But they are not opening in Outlook all the tables are going as an HTML attachment in outlook.
Using the following code for converting my dataframe to HTML.
df_1 = df_1.to_html(index=False,header= True,border=4,justify = 'left',col_space =9)
from IPython.core.display import display, HTML
display(HTML(df_1))
This is how I am sending the Email:-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
email_user = 'xyz#x.com'
email_password = '*********'
email_send = 'xyz#x.com'
subject = 'ABCDEF'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body_1 = """ Hey All,
Please find the data:
"""
msg.attach(MIMEText(body_1,'plain'))
msg.attach(MIMEText(df_1,'html'))
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user,email_password)
server.sendmail(email_user,email_send.split(','),text)
server.quit()
There are no error's email is opening in Gmail properly. But in outlook tables are coming as an attachment.
You can make a single attachment as html.That worked for me.
df_1 = df_1.to_html(index=False,header= True,border=4,justify = 'left',col_space =9)
mailBody= """<p> Hi All, <br><br>
Please find the data: </p> <br><br>
""" + df_1 + """<br> Thanks. """
msg.attach(MIMEText(mailBody,'html'))

Change "from" field option in outlook using python via win32com

In addition to this thread send outlook mail via win32com, i'd like to know if there is a possibility to use mail.From alike method. When you create an email, you can choose from what email you'd like to send it.
And for the future, where could i get this information from? I mean do those commands work with COM object of outlook application?
Here's a code which I have been using for long time and hopefully will work for you as well,
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
def sendMail(to, subject, text):
assert type(to)==list
fro = "abc#xyz.com" # use your from email here
msg = MIMEMultipart()
msg['From'] = fro
msg['To'] = COMMASPACE.join(to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(html, 'html'))
smtp = smtplib.SMTP('mailhost.abcd.co.in') #use your mailhost here, it's dummy.
smtp.sendmail("", to, msg.as_string() )
smtp.close()
TOADDR = ['abc#xyz.com'] # list of emails address to be sent to
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the link you wanted.
</p>
</body>
</html>
"""
sendMail( TOADDR, "hello",html)

Categories

Resources