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?
Related
I am trying to embed tweets in an email. For that, I get the oembed tweet using the tweepy API and then insert it as HTML. However, when I send the email, I notice that I only receive the body text of the tweet. Is there a way to insert the full tweet (that is including images/vide/gif...) in an email using python?
Here is my code:
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
sender_email = ""
receiver_email = ""
password = ''
message = MIMEMultipart("alternative")
message["Subject"] = "Email test"
message["From"] = sender_email
message["To"] = receiver_email
# Create the plain-text and HTML version of your message
text = "hello world"
# Here is the HTML directly that I mine via tweepy.api.get_oembed():
html = """\
<html>
<body>
<blockquote class="twitter-tweet">
<p lang="en" dir="ltr">you good?</p>— Twitter (#Twitter) March 30, 2022
</blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</html>
"""
# Turn these into plain/html MIMEText objects
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
# Add HTML/plain-text parts to MIMEMultipart message
# The email client will try to render the last part first
message.attach(part1)
message.attach(part2)
# Create secure connection with server and send email
server = smtplib.SMTP('smtp.office365.com', 587) ### put your relevant SMTP here
server.ehlo()
server.starttls()
server.ehlo()
server.login(sender_email, password) ### if applicable
server.send_message(message)
server.quit()
I'm using SMTP mail but when i run the script they get sent to the spam folder
import os
import imghdr
import smtplib
from email.message import EmailMessage
email_add = "email#domain.com"
pwd = 'your password'
msg = EmailMessage()
msg['Subject'] = "Grabbing dinner?"
msg['From'] = email_add
msg['To'] = 'dadadagautam#gmail.com'
msg.add_alternative("""\
<h1>Some html here </h1>
""", subtype='html')
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(email_add, pwd)
smtp.send_message(msg)
The email should be sent to the primary section instead of being sent to spam
I have a python script to send email and it works just fine but the problem is when I check my email inbox.
I want that username to be customize username and not the whole email address.
The format you should use for the from address is:
Your Name <username#domain.com>
If you are using multipart message, and render markdown, if you want beautiful messages.
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
multipart_msg = MIMEMultipart("alternative")
multipart_msg["Subject"] = message.splitlines()[0]
multipart_msg["From"] = DISPLAY_NAME + f' <{SENDER_EMAIL}>'
multipart_msg["To"] = receiver
text = message
html = markdown.markdown(text)
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
multipart_msg.attach(part1)
multipart_msg.attach(part2)
server.sendmail(SENDER_EMAIL, receiver,
multipart_msg.as_string())
I got this code at https://en.wikibooks.org/wiki/Python_Programming/Email
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
fromaddr = "youremailid#gmail.com"
toaddr = "target#example.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = ""
body = "This is just a test email. Do not reply to this"
msg.attach(MIMEText(body, 'plain'))
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login("youremailusername", "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
Format for from address is
username#server.com
Format for to address is
username#server.com
and smtplib.SMTP accepts 0 or 2 parameters.
The first parameter is type str and the second parameter is type int
I'm trying to send and email with html and txt. But I need the contents of the .txt file into the email html body. And so far I can only get the txt file to work or the html, but not both. Any ideas?
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
sender = "sender#gmail.com"
receiver = "receiver#gmail.com"
msg = MIMEMultipart('alternative')
msg['Subject'] = "update"
msg['From'] = sender
msg['To'] = receiver
f1 = (open("email_data.txt"))
text = MIMEText(f1.read(),'plain')
html = """\
<html>
<head></head>
Header
<body>
<p>Something<br>
Update<br>
Need the contents of the text file to open here
</p>
</body>
</html>
"""
#part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(text)
msg.attach(part2)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.ehlo()
server.login("sender", "password")
server.sendmail(sender, receiver, msg.as_string())
print 'Email sent'
server.quit()
Great case for yagmail.
import yagmail
yag = yagmail.SMTP('username','password')
html = '<h1>some header text</h1><p>{}</p>'.format(f1.read())
yag.send('toaddr#gmail.com', 'subject', html)
Done.
Best to read the yagmail documentation at the link above to see what magic is actually happening.
I think you're looking for string formatting, the most basic use case is
"""the text i want to insert is the following {} """.format(text)
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)