classification
Title: Escape needed in the email documentation example
Type: Stage: resolved
Components: Documentation Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Antonio Gutierrez, Lewis Ball, barry, docs@python, maxking, r.david.murray, terry.reedy
Priority: normal Keywords:

Created on 2020-07-24 16:35 by Antonio Gutierrez, last changed 2020-07-25 10:41 by Antonio Gutierrez. This issue is now closed.

Messages (7)
msg374197 - (view) Author: Antonio Gutierrez (Antonio Gutierrez) Date: 2020-07-24 16:35
In the main documentation, the example:

#!/usr/bin/env python3

import smtplib

from email.message import EmailMessage
from email.headerregistry import Address
from email.utils import make_msgid

# Create the base text message.
msg = EmailMessage()
msg['Subject'] = "Ayons asperges pour le déjeuner"
msg['From'] = Address("Pepé Le Pew", "pepe", "example.com")
msg['To'] = (Address("Penelope Pussycat", "penelope", "example.com"),
             Address("Fabrette Pussycat", "fabrette", "example.com"))
msg.set_content("""\
Salut!

Cela ressemble à un excellent recipie[1] déjeuner.

[1] http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718

--Pepé
""")

# Add the html version.  This converts the message into a multipart/alternative
# container, with the original text message as the first part and the new html
# message as the second part.
asparagus_cid = make_msgid()
msg.add_alternative("""\
<html>
  <head></head>
  <body>
    <p>Salut!</p>
    <p>Cela ressemble à un excellent
        <a href="http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718">
            recipie
        </a> déjeuner.
    </p>
    <img src="cid:{asparagus_cid}" />
  </body>
</html>
""".format(asparagus_cid=asparagus_cid[1:-1]), subtype='html')
# note that we needed to peel the <> off the msgid for use in the html.

# Now add the related image to the html part.
with open("roasted-asparagus.jpg", 'rb') as img:
    msg.get_payload()[1].add_related(img.read(), 'image', 'jpeg',
                                     cid=asparagus_cid)

# Make a local copy of what we are going to send.
with open('outgoing.msg', 'wb') as f:
    f.write(bytes(msg))

# Send the message via local SMTP server.
with smtplib.SMTP('localhost') as s:
    s.send_message(msg)



in the line <img src="cid:{asparagus_cid}" /> the " has to be escape, <img src=\"cid:{asparagus_cid}\" /> , otherwise it wont read the image.
msg374206 - (view) Author: Lewis Ball (Lewis Ball) * Date: 2020-07-24 19:25
So I have just tried it out and the example seems to work fine for me as it is (tested using 3.8.2).

Note that escaping the " in this case makes no difference to the string:
```
>>> """<img src="cid:{cid}" />""" == """<img src=\"cid:{cid}\" />"""
True
```
msg374208 - (view) Author: Antonio Gutierrez (Antonio Gutierrez) Date: 2020-07-24 19:38
I forget to mention that I am receiving the mails with gmail, I don't know
if that is important or not. It shouldn't be different right, but for me it
is, if i don't use the escape the image is send and I can get it from the
attachment, but I am unable to see it in the html message. Maybe I am
doing something wrong.

El vie., 24 jul. 2020 a las 21:25, Lewis Ball (<report@bugs.python.org>)
escribió:

>
> Lewis Ball <lrjball@gmail.com> added the comment:
>
> So I have just tried it out and the example seems to work fine for me as
> it is (tested using 3.8.2).
>
> Note that escaping the " in this case makes no difference to the string:
> ```
> >>> """<img src="cid:{cid}" />""" == """<img src=\"cid:{cid}\" />"""
> True
> ```
>
> ----------
> nosy: +Lewis Ball
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue41387>
> _______________________________________
>
msg374211 - (view) Author: Antonio Gutierrez (Antonio Gutierrez) Date: 2020-07-24 20:04
OK, first I am sorry , I did try again when I read your message( because it
has all the sense), and yes it works, probably I corrected something else
that made it work, i don't know. I'll try to make better reports in the
future.

El vie., 24 jul. 2020 a las 21:38, Antonio Gutierrez (<
report@bugs.python.org>) escribió:

>
> Antonio Gutierrez <antdcs@gmail.com> added the comment:
>
> I forget to mention that I am receiving the mails with gmail, I don't know
> if that is important or not. It shouldn't be different right, but for me it
> is, if i don't use the escape the image is send and I can get it from the
> attachment, but I am unable to see it in the html message. Maybe I am
> doing something wrong.
>
> El vie., 24 jul. 2020 a las 21:25, Lewis Ball (<report@bugs.python.org>)
> escribió:
>
> >
> > Lewis Ball <lrjball@gmail.com> added the comment:
> >
> > So I have just tried it out and the example seems to work fine for me as
> > it is (tested using 3.8.2).
> >
> > Note that escaping the " in this case makes no difference to the string:
> > ```
> > >>> """<img src="cid:{cid}" />""" == """<img src=\"cid:{cid}\" />"""
> > True
> > ```
> >
> > ----------
> > nosy: +Lewis Ball
> >
> > _______________________________________
> > Python tracker <report@bugs.python.org>
> > <https://bugs.python.org/issue41387>
> > _______________________________________
> >
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue41387>
> _______________________________________
>
msg374217 - (view) Author: Lewis Ball (Lewis Ball) * Date: 2020-07-24 23:10
Okay no worries, glad it is all sorted now :)
msg374245 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-07-25 05:42
Antonio, when replying by email, please delete the quoted message (except possibly for a line or two that you want to quote).  When posted on the web page, the quote is distracting noise that takes up vertical space.
msg374250 - (view) Author: Antonio Gutierrez (Antonio Gutierrez) Date: 2020-07-25 10:41
>
> OK, I will do that.
>
>
History
Date User Action Args
2020-07-25 10:41:29Antonio Gutierrezsetmessages: + msg374250
2020-07-25 05:42:39terry.reedysetnosy: + terry.reedy
messages: + msg374245
2020-07-24 23:10:00Lewis Ballsetmessages: + msg374217
2020-07-24 20:45:54r.david.murraysetstatus: open -> closed
resolution: not a bug
stage: resolved
2020-07-24 20:04:45Antonio Gutierrezsetmessages: + msg374211
2020-07-24 19:38:15Antonio Gutierrezsetmessages: + msg374208
2020-07-24 19:25:08Lewis Ballsetnosy: + Lewis Ball
messages: + msg374206
2020-07-24 18:05:46SilentGhostsetnosy: + barry, r.david.murray, maxking

title: Wrong example, need scpae \" -> Escape needed in the email documentation example
2020-07-24 16:36:52Antonio Gutierrezsetassignee: docs@python

nosy: + docs@python
components: + Documentation
versions: + Python 3.8
2020-07-24 16:35:57Antonio Gutierrezcreate