avatarJ3

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4875

Abstract

="hljs-keyword">import</span> EmailMessage</pre></div><div id="f6bf"><pre><span class="hljs-comment"># Open the plain text file whose name is in textfile for reading.</span> <span class="hljs-keyword">with</span> <span class="hljs-built_in">open</span>(path_to_file, <span class="hljs-string">'r'</span>) <span class="hljs-keyword">as</span> fp: <span class="hljs-comment"># Create a text/plain message</span> msg = EmailMessage() msg.set_content(fp.<span class="hljs-built_in">read</span>())</pre></div><div id="531c"><pre><span class="hljs-comment"># me == the sender's email address</span> <span class="hljs-comment"># you == the recipient's email address</span></pre></div><div id="eb6e"><pre>msg[<span class="hljs-string">'Subject'</span>] = f<span class="hljs-string">'The contents of {textfile}'</span> msg[<span class="hljs-string">'From'</span>] = <span class="hljs-string">'<your_email>'</span> msg[<span class="hljs-string">'To'</span>] = <span class="hljs-string">'<another_email>'</span></pre></div><div id="8bd0"><pre><span class="hljs-comment"># Send the message via our own SMTP server.</span> s = smtplib.SMTP(<span class="hljs-string">'<mensager.of.your.cia>'</span>) s.send_message(msg) s.<span class="hljs-keyword">quit</span>()</pre></div><p id="986f">05#Step — Now the Multiple Emails</p><div id="7ef7"><pre><span class="hljs-comment"># Import smtplib for the actual sending function</span></pre></div><div id="6b69"><pre><span class="hljs-keyword">import</span> smtplib <span class="hljs-title">from</span> datetime <span class="hljs-keyword">import</span> datetime</pre></div><div id="8de9"><pre><span class="hljs-comment"># Import the email modules we'll need</span></pre></div><div id="ee1f"><pre><span class="hljs-keyword">from</span> email.message <span class="hljs-keyword">import</span> EmailMessage <span class="hljs-keyword">server</span> = smtplib.SMTP(<span class="hljs-string">'<mensager.of.your.cia>'</span>)</pre></div><div id="9873"><pre><span class="hljs-keyword">for</span> i, email in enumerate(admin_df[<span class="hljs-string">'Email'</span>]): Responsavel = admin_df.<span class="hljs-keyword">loc</span>[i, <span class="hljs-string">'Responsible'</span>] IP = admin_df.<span class="hljs-keyword">loc</span>[i, <span class="hljs-string">'IP'</span>] Intimation = admin_df.<span class="hljs-keyword">loc</span>[i, <span class="hljs-string">'Intimation'</span>] Default = admin_df.<span class="hljs-keyword">loc</span>[i, <span class="hljs-string">'Default'</span>] CNPJ = admin_df.<span class="hljs-keyword">loc</span>[i, <span class="hljs-string">'CNPJ'</span>] msg = EmailMessage()

body = <span class="hljs-keyword">f</span><span class="hljs-string">""</span><span class="hljs-comment">"</span> To{IP} - <span class="hljs-keyword">N</span>°: {CNPJ} {Responsible}</pre></div><div id="e240"><pre> Dear Administrator - DIMP (Declaration <span class="hljs-keyword">of</span> Means <span class="hljs-keyword">of</span> Payments) -
<span class="hljs-keyword">Public</span> Sector:</pre></div><div id="24eb"><pre> We request the (re)transmission of DIMP files related to the <span class="hljs-built_in">period</span>(s) recorded below (month/year):</pre></div><div id="45a0"><pre> {Default.<span class="hljs-built_in">split</span>(<span class="hljs-string">';'</span>)}</pre></div><div id="547e"><pre><span class="hljs-symbol"> Motivations:</span></pre></div><div id="13a8"><pre> EXCLUSIVELY FOR UPDATING <span class="hljs-keyword">AND </span>UPDATING PURPOSES IN THE <span class="hljs-keyword">INSTITUTIONAL </span>DATABASE OF THIS CIA<span class="hljs-comment">; PLEASE SEND ALL </span> FILES WITH <span class="hljs-keyword">NORMAL </span>PURPOSE.</pre></div><div id="907e"><pre> Thank you.</pre></div><div id="dc3a"><pre><span class="hljs-built_in"> Sincerely,</span></pre></div><div id="cec1"><pre> ------------------------ <span class="hljs-tag"><<span class="hljs-name">your_name</span>></span> <span class="hljs-tag"><<span class="hljs-name">your_title</span>></span> <span class="hljs-tag"><<span class="hljs-name">your_sector</span>></span> EMAIL: <span class="hljs-tag"><<span class="hljs-name">your_email</span>></span> TEL: <span class="hljs-tag"><<span class="hljs-name">your_contact</span>></span></pre></div><div id="b889"><pre> ------------------- <span class="hljs-string">"""</span></pre></div><div id="4347"><pre>msg.set_content(<span class="hljs-keyword">body</span>)</pre></div><div id="a863"><pre><span class="hljs-comment"># me == the sender's email address</span> <span class="hljs-comment"># you == the recipient's email address</span></pre></div><div id="c1a4"><pre>msg[<span class="hljs-string">'Subject'</span>] = <span class="hljs-string">f" Report of O

Options

missions DA <span class="hljs-subst">{IP}</span> - <span class="hljs-subst">{CNPJ}</span> - FOR FINANCE CIA - AT - Request No. <span class="hljs-subst">{Intimation}</span> "</span></pre></div><div id="89da"><pre>msg[<span class="hljs-string">'From'</span>] = <span class="hljs-string">'<your_email>'</span> msg[<span class="hljs-string">'To'</span>] = email</pre></div><div id="84e1"><pre><span class="hljs-comment"># Send the message via our own SMTP server.</span> <span class="hljs-comment">#server = smtplib.SMTP('<mensager.of.your.cia>')</span></pre></div><div id="6f37"><pre><span class="hljs-function"><span class="hljs-title">print</span><span class="hljs-params">(<span class="hljs-string">"Email successfully sent!"</span>)</span></span></pre></div><div id="d855"><pre>Email successfully sent!!</pre></div><p id="7338">That’s it!</p><p id="1229">Bye o/</p><p id="8d72">👉Jupiter notebook <a href="https://drive.google.com/file/d/11IcWm94SiUl4XvLLVcws9ZnFEGD_NQlG/view?usp=sharing">link</a> :)</p><p id="a223">👉<a href="https://drive.google.com/drive/folders/1OgbZvCopANMtnZfRHRfouhaqaR5tlHLN?usp=sharing">Google Drive</a></p><p id="426f">👉<a href="https://github.com/giljr/my_jupyter_notebook/tree/master/PPY_11">git</a></p><h2 id="98ac">Credits & References:</h2><p id="363a"><a href="https://stackoverflow.com/questions/41845518/jupyter-anaconda-load-text-file-into-python/48785326">https://stackoverflow.com/questions/41845518/jupyter-anaconda-load-text-file-into-python/48785326</a> Official smtplib — SMTP protocol client: <a href="https://docs.python.org/3/library/smtplib.html">https://docs.python.org/3/library/smtplib.html</a> examples: <a href="https://docs.python.org/3/library/email.examples.html#email-examples">https://docs.python.org/3/library/email.examples.html#email-examples</a> com HTML: <a href="https://code.tutsplus.com/pt/tutorials/sending-emails-in-python-with-smtp--cms-29975">https://code.tutsplus.com/pt/tutorials/sending-emails-in-python-with-smtp--cms-29975</a> Python: “subject” not shown when sending email using smtplib module: <a href="https://stackoverflow.com/questions/7232088/python-subject-not-shown-when-sending-email-using-smtplib-module">https://stackoverflow.com/questions/7232088/python-subject-not-shown-when-sending-email-using-smtplib-module</a> Jupyter Anaconda: load text file into python: <a href="https://stackoverflow.com/questions/41845518/jupyter-anaconda-load-text-file-into-python/48785326">https://stackoverflow.com/questions/41845518/jupyter-anaconda-load-text-file-into-python/48785326</a> date: 10/2021</p><p id="b34e"><b>00</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/lambda-in-python-421b0c18e825"><b>Lambda in Python </b></a>— Python Lambda Desmistification</p><p id="272c"><b>01</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/send-emails-using-python-jupyter-notebook-94d14a5a5655"><b>Send Email in Python</b></a> — Using Jupyter Notebook — How To Send Gmail In Python</p><p id="988c"><b>02</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/automate-your-email-marketing-with-python-f0d68234b789"><b>Automate Your Email With Python & Outlook</b></a><b> </b>— How To Create An Email Trigger System in Python</p><p id="7ec3"><b>03</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/manipulating-files-with-python-3f9a781287e9"><b>Manipulating Files With Python</b></a> — Manage Your Lovely Photos With Python!</p><p id="c2ad"><b>04</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/pandas-dataframe-advanced-48f83a5b097f"><b>Pandas DataFrame Advanced </b></a>— A Complete Notebook Review</p><p id="eec4"><b>05</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/is-this-leap-year-python-calendar-3d1a61f2c4a7"><b>Is This Leap Year? Python Calendar</b> </a>— How To Calculate If The Year Is Leap Year and How Many Days Are In The Month</p><p id="1e24"><b>06</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/list-comprehension-in-python-c22c4b0a6a8a"><b>List Comprehension In Python </b></a>— Locked-in Secrets About List Comprehension</p><p id="2018"><b>07</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/graphs-in-python-b7d243737b77"><b>Graphs — In Python </b></a>— Extremely Simple Algorithms in Python</p><p id="8f78"><b>08</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/decorator-in-python-62c00f7e818"><b>Decorator in Python </b></a>— How To Simplifying Your Code And Boost Your Function</p><p id="3897"><b>10</b>#Episode#PurePythonSeries — CS50 — <a href="https://readmedium.com/cs50-a-taste-of-python-a4ac87883ff4"><b>A Taste of Python</b></a> — Harvard Mario’s Challenge Solver</p><p id="4706"><b>11</b>#Episode#PurePythonSeries — Python — Send Email Using SMTP— Send Mail To Any Internet Machine (SMTP or ESMTP) (this one)</p></article></body>

Python — Send Email Using SMTP

Send Mail To Any Internet Machine (SMTP or ESMTP) #PurePythonSeries — Episode #11

Hi in theses last two episodes (01 and 02) of this series we use MS Outlook and yagmail.

But theses two solution brought a problem: If your email is not native, it fatally will lead your email to spam, because the servers do not match.

But using SMTP there is no issue. You access the server itself.

Image from link

Let’s Get Started:

Step-by-Step Index:

Create an SMTP object to connect to the server.
Log in to your account.
Define your message header and your login credentials.
Create a MIMEMultipart object and associate the relevant header with it - example: From:, To:, and Subject.
Associate a message with the MIMEMultipart message object.
Finally, send the message.

01#Step —Login to SMTP Server

import smtplib
server = smtplib.SMTP('<mensager.of.your.cia>', 25)
server.starttls()
server.login("<Your-email>", "<Your-password>")
Out[1]:
(235, b'2.7.0 Authentication successful')

02#Step — Login to SMTP ServerSend email

msg = "Testing SMTP EMAIL "
server.sendmail("<Your-email>", "<Other-email>", msg)
server.quit()

Simple like that!

Now Let’s use Pandas to load the file that brings the list to where we wanto to send email.

03#Step — Load email’s list (Excel file)

# Using PANDAS
import pandas as pd
list_table = pd.read_excel('email_schedule_table.xlsx')
display(list_table)

[this prints your table list]

04#Step — First Single test

# Import smtplib for the actual sending function
import smtplib
import os
base_path = "<to.readme.file>"
filename = "readme"
path_to_file = os.path.join(base_path, filename)
# Import the email modules we'll need
from email.message import EmailMessage
# Open the plain text file whose name is in textfile for reading.
with open(path_to_file, 'r') as fp:
    # Create a text/plain message
    msg = EmailMessage()
    msg.set_content(fp.read())
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = f'The contents of {textfile}'
msg['From'] = '<your_email>'
msg['To'] = '<another_email>'
# Send the message via our own SMTP server.
s = smtplib.SMTP('<mensager.of.your.cia>')
s.send_message(msg)
s.quit()

05#Step — Now the Multiple Emails

# Import smtplib for the actual sending function
import smtplib
from datetime import datetime
# Import the email modules we'll need
from email.message import EmailMessage
server = smtplib.SMTP('<mensager.of.your.cia>')
for i, email in enumerate(admin_df['Email']):
   Responsavel = admin_df.loc[i, 'Responsible']
   IP = admin_df.loc[i, 'IP']
   Intimation = admin_df.loc[i, 'Intimation']
   Default = admin_df.loc[i, 'Default']
   CNPJ = admin_df.loc[i, 'CNPJ']
   msg = EmailMessage()
   
   body = f"""\
   To{IP} - N°: {CNPJ}
   {Responsible}
   Dear Administrator - DIMP (Declaration of Means of Payments) -    
   Public Sector:
   We request the (re)transmission of DIMP files related to the
     period(s) recorded below (month/year):
    {Default.split(';')}
    Motivations:
   EXCLUSIVELY FOR UPDATING AND UPDATING PURPOSES IN THE 
   INSTITUTIONAL DATABASE OF THIS CIA; PLEASE SEND ALL    
   FILES WITH NORMAL PURPOSE.
   Thank you.
   Sincerely,
   ------------------------
   <your_name>
   <your_title>
   <your_sector>
   EMAIL: <your_email>
   TEL: <your_contact>
   ------------------- """
msg.set_content(body)
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = f" Report of Omissions DA {IP} - {CNPJ} - FOR FINANCE CIA - AT - Request No. {Intimation} "
msg['From'] = '<your_email>'
msg['To'] = email
# Send the message via our own SMTP server.
#server = smtplib.SMTP('<mensager.of.your.cia>')
print("Email successfully sent!")
Email successfully sent!!

That’s it!

Bye o/

👉Jupiter notebook link :)

👉Google Drive

👉git

Credits & References:

https://stackoverflow.com/questions/41845518/jupyter-anaconda-load-text-file-into-python/48785326 Official smtplib — SMTP protocol client: https://docs.python.org/3/library/smtplib.html examples: https://docs.python.org/3/library/email.examples.html#email-examples com HTML: https://code.tutsplus.com/pt/tutorials/sending-emails-in-python-with-smtp--cms-29975 Python: “subject” not shown when sending email using smtplib module: https://stackoverflow.com/questions/7232088/python-subject-not-shown-when-sending-email-using-smtplib-module Jupyter Anaconda: load text file into python: https://stackoverflow.com/questions/41845518/jupyter-anaconda-load-text-file-into-python/48785326 date: 10/2021

00#Episode#PurePythonSeries — Lambda in Python — Python Lambda Desmistification

01#Episode#PurePythonSeries — Send Email in Python — Using Jupyter Notebook — How To Send Gmail In Python

02#Episode#PurePythonSeries — Automate Your Email With Python & Outlook — How To Create An Email Trigger System in Python

03#Episode#PurePythonSeries — Manipulating Files With Python — Manage Your Lovely Photos With Python!

04#Episode#PurePythonSeries — Pandas DataFrame Advanced — A Complete Notebook Review

05#Episode#PurePythonSeries — Is This Leap Year? Python Calendar — How To Calculate If The Year Is Leap Year and How Many Days Are In The Month

06#Episode#PurePythonSeries — List Comprehension In Python — Locked-in Secrets About List Comprehension

07#Episode#PurePythonSeries — Graphs — In Python — Extremely Simple Algorithms in Python

08#Episode#PurePythonSeries — Decorator in Python — How To Simplifying Your Code And Boost Your Function

10#Episode#PurePythonSeries — CS50 — A Taste of Python — Harvard Mario’s Challenge Solver

11#Episode#PurePythonSeries — Python — Send Email Using SMTP— Send Mail To Any Internet Machine (SMTP or ESMTP) (this one)

Email
Python
Smtp Server
Send Email
How To
Recommended from ReadMedium