Mailing List Archive

Including a Variable In the HTML Tags When Sending An Email
Hello all

I was hoping someone could help me with the following coding problem.

I am trying to send an email where the body of the email is taken from a data frame, which i have managed to do.

However i want to start the email by saying Hi Name, where Name is a variable that contains the person's name to whom i am sending the email to - This is the bit i cannot get working.

The code i have so far is as follows:-

[python]
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

mail=smtplib.SMTP('smtp.gmail.com', 123)
mail.ehlo()
mail.starttls()
mail.login("Email","Pwd")

From_Address = ["From_Email"]
To_Address = [Report_Data_Frame.iloc[0,10]]
CC_Address = ["CC_Email", "CC_Email", "CC_Email"]
Subject_Email = "Email_Subject"
Body = Email_Body_Data_Frame
Name = "Tom"


html = """\
<html>

<head>
Hi Name Goes HERE!!!
<br>
<br>
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT<br> <br>
</head>

<body>

{0}

</body>

<br>

TEXT TEXT <br><br>
TEXT TEXT <br><br>
TEXT TEXT <br><br>
TEXT TEXT <br>


</html>
""".format(Body.to_html())

msg = MIMEMultipart()
msg['From'] = ', '.join(From_Address)
msg['To'] = ', '.join(To_Address)
msg['Cc'] = ', '.join(CC_Address)
msg['Subject'] = Subject_Email

message = MIMEText(html,'html')
msg.attach(message)
mail.sendmail(From_Address, (To_Address + CC_Address), msg.as_string())
[/python]

In this case the variable Name is Tom and i want to include Tom in the email.

Can anyone help?

Still a newbie; approx 3 weeks playing with Python (cut and past most of this code)

Any help will be greatly appericated.

Thank you.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Including a Variable In the HTML Tags When Sending An Email [ In reply to ]
All the text that you want the user to see needs to be in the <body> of
the message. <head> is for metadata and stuff to setup some formatting.

You might want to study up a bit on HTML formatting too. Depending on
what the data frame is like, you may need to enclose it in some sort of
container, like a <div>

On 8/8/20 10:29 AM, sammy.jackson987@gmail.com wrote:
> Hello all
>
> I was hoping someone could help me with the following coding problem.
>
> I am trying to send an email where the body of the email is taken from a data frame, which i have managed to do.
>
> However i want to start the email by saying Hi Name, where Name is a variable that contains the person's name to whom i am sending the email to - This is the bit i cannot get working.
>
> The code i have so far is as follows:-
>
> [python]
> import smtplib
> from email.mime.multipart import MIMEMultipart
> from email.mime.text import MIMEText
>
> mail=smtplib.SMTP('smtp.gmail.com', 123)
> mail.ehlo()
> mail.starttls()
> mail.login("Email","Pwd")
>
> From_Address = ["From_Email"]
> To_Address = [Report_Data_Frame.iloc[0,10]]
> CC_Address = ["CC_Email", "CC_Email", "CC_Email"]
> Subject_Email = "Email_Subject"
> Body = Email_Body_Data_Frame
> Name = "Tom"
>
>
> html = """\
> <html>
>
> <head>
> Hi Name Goes HERE!!!
> <br>
> <br>
> TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT<br> <br>
> </head>
>
> <body>
>
> {0}
>
> </body>
>
> <br>
>
> TEXT TEXT <br><br>
> TEXT TEXT <br><br>
> TEXT TEXT <br><br>
> TEXT TEXT <br>
>
>
> </html>
> """.format(Body.to_html())
>
> msg = MIMEMultipart()
> msg['From'] = ', '.join(From_Address)
> msg['To'] = ', '.join(To_Address)
> msg['Cc'] = ', '.join(CC_Address)
> msg['Subject'] = Subject_Email
>
> message = MIMEText(html,'html')
> msg.attach(message)
> mail.sendmail(From_Address, (To_Address + CC_Address), msg.as_string())
> [/python]
>
> In this case the variable Name is Tom and i want to include Tom in the email.
>
> Can anyone help?
>
> Still a newbie; approx 3 weeks playing with Python (cut and past most of this code)
>
> Any help will be greatly appericated.
>
> Thank you.


--
Richard Damon

--
https://mail.python.org/mailman/listinfo/python-list
Re: Including a Variable In the HTML Tags When Sending An Email [ In reply to ]
On Saturday, August 8, 2020 at 3:46:04 PM UTC+1, Richard Damon wrote:
> All the text that you want the user to see needs to be in the <body> of
> the message. <head> is for metadata and stuff to setup some formatting.
>
> You might want to study up a bit on HTML formatting too. Depending on
> what the data frame is like, you may need to enclose it in some sort of
> container, like a <div>
>
> On 8/8/20 10:29 AM, sammy.jackson987@gmail.com wrote:
> > Hello all
> >
> > I was hoping someone could help me with the following coding problem.
> >
> > I am trying to send an email where the body of the email is taken from a data frame, which i have managed to do.
> >
> > However i want to start the email by saying Hi Name, where Name is a variable that contains the person's name to whom i am sending the email to - This is the bit i cannot get working.
> >
> > The code i have so far is as follows:-
> >
> > [python]
> > import smtplib
> > from email.mime.multipart import MIMEMultipart
> > from email.mime.text import MIMEText
> >
> > mail=smtplib.SMTP('smtp.gmail.com', 123)
> > mail.ehlo()
> > mail.starttls()
> > mail.login("Email","Pwd")
> >
> > From_Address = ["From_Email"]
> > To_Address = [Report_Data_Frame.iloc[0,10]]
> > CC_Address = ["CC_Email", "CC_Email", "CC_Email"]
> > Subject_Email = "Email_Subject"
> > Body = Email_Body_Data_Frame
> > Name = "Tom"
> >
> >
> > html = """\
> > <html>
> >
> > <head>
> > Hi Name Goes HERE!!!
> > <br>
> > <br>
> > TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT<br> <br>
> > </head>
> >
> > <body>
> >
> > {0}
> >
> > </body>
> >
> > <br>
> >
> > TEXT TEXT <br><br>
> > TEXT TEXT <br><br>
> > TEXT TEXT <br><br>
> > TEXT TEXT <br>
> >
> >
> > </html>
> > """.format(Body.to_html())
> >
> > msg = MIMEMultipart()
> > msg['From'] = ', '.join(From_Address)
> > msg['To'] = ', '.join(To_Address)
> > msg['Cc'] = ', '.join(CC_Address)
> > msg['Subject'] = Subject_Email
> >
> > message = MIMEText(html,'html')
> > msg.attach(message)
> > mail.sendmail(From_Address, (To_Address + CC_Address), msg.as_string())
> > [/python]
> >
> > In this case the variable Name is Tom and i want to include Tom in the email.
> >
> > Can anyone help?
> >
> > Still a newbie; approx 3 weeks playing with Python (cut and past most of this code)
> >
> > Any help will be greatly appericated.
> >
> > Thank you.
>
>
> --
> Richard Damon

Thank you Richard for your response.

I have moved all the text i want the user to see into the body of the email.

I still cannot get my email to display the name.

Name = "Tim"

How would i include this variable in my HTML/Python code?

Any ideas?

Thank you.

--
https://mail.python.org/mailman/listinfo/python-list
Re: Including a Variable In the HTML Tags When Sending An Email [ In reply to ]
On 8/8/20 10:58 AM, sammy.jackson987@gmail.com wrote:
> Thank you Richard for your response.
>
> I have moved all the text i want the user to see into the body of the email.
>
> I still cannot get my email to display the name.
>
> Name = "Tim"
>
> How would i include this variable in my HTML/Python code?
>
> Any ideas?
>
> Thank you.

Since you are already using .format to insert the message body, you
might as well do something similar to insert the name, adding more
placeholders for the format to insert into.

--
Richard Damon

--
https://mail.python.org/mailman/listinfo/python-list
Re: Including a Variable In the HTML Tags When Sending An Email [ In reply to ]
On 2020-08-08 15:58, sammy.jackson987@gmail.com wrote:
> On Saturday, August 8, 2020 at 3:46:04 PM UTC+1, Richard Damon wrote:
>> All the text that you want the user to see needs to be in the <body> of
>> the message. <head> is for metadata and stuff to setup some formatting.
>>
>> You might want to study up a bit on HTML formatting too. Depending on
>> what the data frame is like, you may need to enclose it in some sort of
>> container, like a <div>
>>
>> On 8/8/20 10:29 AM, sammy.jackson987@gmail.com wrote:
>> > Hello all
>> >
>> > I was hoping someone could help me with the following coding problem.
>> >
>> > I am trying to send an email where the body of the email is taken from a data frame, which i have managed to do.
>> >
>> > However i want to start the email by saying Hi Name, where Name is a variable that contains the person's name to whom i am sending the email to - This is the bit i cannot get working.
>> >
>> > The code i have so far is as follows:-
>> >
>> > [python]
>> > import smtplib
>> > from email.mime.multipart import MIMEMultipart
>> > from email.mime.text import MIMEText
>> >
>> > mail=smtplib.SMTP('smtp.gmail.com', 123)
>> > mail.ehlo()
>> > mail.starttls()
>> > mail.login("Email","Pwd")
>> >
>> > From_Address = ["From_Email"]
>> > To_Address = [Report_Data_Frame.iloc[0,10]]
>> > CC_Address = ["CC_Email", "CC_Email", "CC_Email"]
>> > Subject_Email = "Email_Subject"
>> > Body = Email_Body_Data_Frame
>> > Name = "Tom"
>> >
>> >
>> > html = """\
>> > <html>
>> >
>> > <head>
>> > Hi Name Goes HERE!!!
>> > <br>
>> > <br>
>> > TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT<br> <br>
>> > </head>
>> >
>> > <body>
>> >
>> > {0}
>> >
>> > </body>
>> >
>> > <br>
>> >
>> > TEXT TEXT <br><br>
>> > TEXT TEXT <br><br>
>> > TEXT TEXT <br><br>
>> > TEXT TEXT <br>
>> >
>> >
>> > </html>
>> > """.format(Body.to_html())
>> >
>> > msg = MIMEMultipart()
>> > msg['From'] = ', '.join(From_Address)
>> > msg['To'] = ', '.join(To_Address)
>> > msg['Cc'] = ', '.join(CC_Address)
>> > msg['Subject'] = Subject_Email
>> >
>> > message = MIMEText(html,'html')
>> > msg.attach(message)
>> > mail.sendmail(From_Address, (To_Address + CC_Address), msg.as_string())
>> > [/python]
>> >
>> > In this case the variable Name is Tom and i want to include Tom in the email.
>> >
>> > Can anyone help?
>> >
>> > Still a newbie; approx 3 weeks playing with Python (cut and past most of this code)
>> >
>> > Any help will be greatly appericated.
>> >
>> > Thank you.
>>
>>
>> --
>> Richard Damon
>
> Thank you Richard for your response.
>
> I have moved all the text i want the user to see into the body of the email.
>
> I still cannot get my email to display the name.
>
> Name = "Tim"
>
> How would i include this variable in my HTML/Python code?
>
> Any ideas?
>
> Thank you.
>
I can't see why you're having a problem putting the name into the HTML
when you're already managing to put the text of the dataframe into it...
--
https://mail.python.org/mailman/listinfo/python-list
Re: Including a Variable In the HTML Tags When Sending An Email [ In reply to ]
On Saturday, August 8, 2020 at 5:03:07 PM UTC+1, MRAB wrote:
> On 2020-08-08 15:58, sammy.jackson987@gmail.com wrote:
> > On Saturday, August 8, 2020 at 3:46:04 PM UTC+1, Richard Damon wrote:
> >> All the text that you want the user to see needs to be in the <body> of
> >> the message. <head> is for metadata and stuff to setup some formatting.
> >>
> >> You might want to study up a bit on HTML formatting too. Depending on
> >> what the data frame is like, you may need to enclose it in some sort of
> >> container, like a <div>
> >>
> >> On 8/8/20 10:29 AM, sammy.jackson987@gmail.com wrote:
> >> > Hello all
> >> >
> >> > I was hoping someone could help me with the following coding problem.
> >> >
> >> > I am trying to send an email where the body of the email is taken from a data frame, which i have managed to do.
> >> >
> >> > However i want to start the email by saying Hi Name, where Name is a variable that contains the person's name to whom i am sending the email to - This is the bit i cannot get working.
> >> >
> >> > The code i have so far is as follows:-
> >> >
> >> > [python]
> >> > import smtplib
> >> > from email.mime.multipart import MIMEMultipart
> >> > from email.mime.text import MIMEText
> >> >
> >> > mail=smtplib.SMTP('smtp.gmail.com', 123)
> >> > mail.ehlo()
> >> > mail.starttls()
> >> > mail.login("Email","Pwd")
> >> >
> >> > From_Address = ["From_Email"]
> >> > To_Address = [Report_Data_Frame.iloc[0,10]]
> >> > CC_Address = ["CC_Email", "CC_Email", "CC_Email"]
> >> > Subject_Email = "Email_Subject"
> >> > Body = Email_Body_Data_Frame
> >> > Name = "Tom"
> >> >
> >> >
> >> > html = """\
> >> > <html>
> >> >
> >> > <head>
> >> > Hi Name Goes HERE!!!
> >> > <br>
> >> > <br>
> >> > TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT<br> <br>
> >> > </head>
> >> >
> >> > <body>
> >> >
> >> > {0}
> >> >
> >> > </body>
> >> >
> >> > <br>
> >> >
> >> > TEXT TEXT <br><br>
> >> > TEXT TEXT <br><br>
> >> > TEXT TEXT <br><br>
> >> > TEXT TEXT <br>
> >> >
> >> >
> >> > </html>
> >> > """.format(Body.to_html())
> >> >
> >> > msg = MIMEMultipart()
> >> > msg['From'] = ', '.join(From_Address)
> >> > msg['To'] = ', '.join(To_Address)
> >> > msg['Cc'] = ', '.join(CC_Address)
> >> > msg['Subject'] = Subject_Email
> >> >
> >> > message = MIMEText(html,'html')
> >> > msg.attach(message)
> >> > mail.sendmail(From_Address, (To_Address + CC_Address), msg.as_string())
> >> > [/python]
> >> >
> >> > In this case the variable Name is Tom and i want to include Tom in the email.
> >> >
> >> > Can anyone help?
> >> >
> >> > Still a newbie; approx 3 weeks playing with Python (cut and past most of this code)
> >> >
> >> > Any help will be greatly appericated.
> >> >
> >> > Thank you.
> >>
> >>
> >> --
> >> Richard Damon
> >
> > Thank you Richard for your response.
> >
> > I have moved all the text i want the user to see into the body of the email.
> >
> > I still cannot get my email to display the name.
> >
> > Name = "Tim"
> >
> > How would i include this variable in my HTML/Python code?
> >
> > Any ideas?
> >
> > Thank you.
> >
> I can't see why you're having a problem putting the name into the HTML
> when you're already managing to put the text of the dataframe into it...

Hi Richard

The issue i am am having is that the Name is a variable stored as a str and my data in my dataframe is stored as a variable of type dataframe.

If i use place holders i.e. {0} and {1} where {0} is the name and {1} is the dataframe i get an error for the following line of code:-
.format((Name,Body).to_html()) which states 'tuple' object has no attribute 'to_html'.

My amended code look like:-

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

mail=smtplib.SMTP('smtp.gmail.com', 123)
mail.ehlo()
mail.starttls()
mail.login("Email","Pwd")

From_Address = ["From_Email"]
To_Address = [Report_Data_Frame.iloc[0,10]]
CC_Address = ["CC_Email", "CC_Email", "CC_Email"]
Subject_Email = "Email_Subject"


Name = "Tom"
Body = Email_Body_Data_Frame


html = """\
<html>
<head>
</head>
<body>
Hi {0}
<br>
<br>
TEXT TEXT TEXT TEXT TEXT
<br>
<br>
{1}
<br>
TEXT TEXT TEXT TEXT TEXT <br><br>

</body>

<br>

</html>
""".format((Name,Body).to_html())

If i convert the dataframe to a string then it messes up all the columns.

Any ideas?

Thanks











--
https://mail.python.org/mailman/listinfo/python-list
Re: Including a Variable In the HTML Tags When Sending An Email [ In reply to ]
On 8/8/20 8:03 PM, sammy.jackson987@gmail.com wrote:
> If i use place holders i.e. {0} and {1} where {0} is the name and {1} is the dataframe i get an error for the following line of code:-
> .format((Name,Body).to_html()) which states 'tuple' object has no attribute 'to_html'.

I would do it as

.format(Name, Body.to_html()) if your names really are just straight
letters.

If they might have some funny characters that need html handling, you
could do:

import html


.format(html.escape(Name), Body.to_html())


Note that different types need to be treated differently to get them
into clean html.


--
Richard Damon

--
https://mail.python.org/mailman/listinfo/python-list
Re: Including a Variable In the HTML Tags When Sending An Email [ In reply to ]
On Sunday, August 9, 2020 at 1:32:30 AM UTC+1, Richard Damon wrote:
> On 8/8/20 8:03 PM, sammy.jackson987@gmail.com wrote:
> > If i use place holders i.e. {0} and {1} where {0} is the name and {1} is the dataframe i get an error for the following line of code:-
> > .format((Name,Body).to_html()) which states 'tuple' object has no attribute 'to_html'.
>
> I would do it as
>
> .format(Name, Body.to_html()) if your names really are just straight
> letters.
>
> If they might have some funny characters that need html handling, you
> could do:
>
> import html
>
>
> .format(html.escape(Name), Body.to_html())
>
>
> Note that different types need to be treated differently to get them
> into clean html.
>
>
> --
> Richard Damon


Richard I love you.

The following change you recommended worked.


--
https://mail.python.org/mailman/listinfo/python-list
Re: Including a Variable In the HTML Tags When Sending An Email [ In reply to ]
On 8/9/20 6:22 PM, sammy.jackson987@gmail.com wrote:
> On Sunday, August 9, 2020 at 1:32:30 AM UTC+1, Richard Damon wrote:
>> On 8/8/20 8:03 PM, sammy.jackson987@gmail.com wrote:
>>> If i use place holders i.e. {0} and {1} where {0} is the name and {1} is the dataframe i get an error for the following line of code:-
>>> .format((Name,Body).to_html()) which states 'tuple' object has no attribute 'to_html'.
>> I would do it as
>>
>> .format(Name, Body.to_html()) if your names really are just straight
>> letters.
>>
>> If they might have some funny characters that need html handling, you
>> could do:
>>
>> import html
>>
>>
>> .format(html.escape(Name), Body.to_html())
>>
>>
>> Note that different types need to be treated differently to get them
>> into clean html.
>>
>>
>> --
>> Richard Damon
>
> Richard I love you.
>
> The following change you recommended worked.
>
>
Now spend a bit of time understanding why that works, how it is
different from what you did, and what you can learn from it so you can
do other things than just follow a rote recipe.

--
Richard Damon

--
https://mail.python.org/mailman/listinfo/python-list