themartorana / python-postmark

Postmark library for Python 2.6 and greater
http://davemartorana.com
MIT License
199 stars 83 forks source link

Fix for text/* Issue: Check if the instance is a string prior to running b64encode to allow… #91

Closed dfranc3373 closed 3 years ago

dfranc3373 commented 3 years ago

… for text/* types on Django without manually attaching

nicholasserra commented 3 years ago

Hey Derrick, thanks for this! Can you provide some more context? Is there an exception or bug happening without this patch? Is a regression test possible for this patch?

dfranc3373 commented 3 years ago

Hey Derrick, thanks for this! Can you provide some more context? Is there an exception or bug happening without this patch? Is a regression test possible for this patch?

Great questions! So originally I was trying to send a CSV generated from csv library with StringIO with an attachment of text/csv type, and when printing the stack tracd I noticed that the CSV string was being converted to a tuple of multiple strings (my guess is from the length of the file) vs the original string.

Because of the new structure, b64encode was failing to run correctly since it was not a string.

Originally tried running encode on the string before calling the library but the bytes sent were the converted to the same structure.

nicholasserra commented 3 years ago

Got it. If you can send some code steps to reproduce that would be cool. I want to replicate this and probably get a test in if it's merged.

dfranc3373 commented 3 years ago

Here is some similar code to what I was running

Code Run:

from django.core.mail import get_connection, EmailMultiAlternatives

mail = EmailMultiAlternatives(subject, msg_plain, from_email, recipients, connection=connection)

# Load up CSV
with open(filename, 'r') as file:
   data = file.read()

    attachments = [(
            'Example.csv',
            data,
            'text/csv',
        )]
    for filename, content, mime_type in attachments:
        mail.attach(filename, content, mime_type)

    mail.send()

Error thrown:

return mail.send()
  File "............python3.7/site-packages/django/core/mail/message.py", line 291, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "............python3.7/site-packages/postmark/django_backend.py", line 66, in send_messages
    sent, instance = self._send(email_messages)
  File "............python3.7/site-packages/postmark/django_backend.py", line 138, in _send
    to_send = self._build_message(messages[0])
  File "............python3.7/site-packages/postmark/django_backend.py", line 108, in _build_message
    content = base64.b64encode(content)
  File "............python3.7/base64.py", line 58, in b64encode
    encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'

CSV:

1,Sylvia,Jones,18,Uvodud Lane,Opgidjir,ND,51557,$8213.46,WHITE,02/05/1926
2,Luella,Hudson,18,Zakrom Square,Siwbilot,HI,36247,$4396.26,YELLOW,01/28/1963
3,Mittie,Hanson,46,Ropew Lane,Logfagkot,AL,59483,$7671.53,RED,12/20/2003
4,Ronald,Peterson,65,Havez Point,Dekogwe,GA,16723,$5436.09,YELLOW,03/26/1921
5,Genevieve,Robbins,32,Ifad Key,Romavad,RI,04186,$9428.91,RED,04/23/1919
6,Bradley,Lawrence,19,Miuva Junction,Memsoptuh,PA,51040,$5569.25,YELLOW,04/10/2028
7,Max,Pena,44,Zipuv Street,Natavtem,VT,87782,$6172.72,BLUE,05/13/2034
8,Christian,Rogers,48,Zivo Key,Duvupvu,FL,50382,$26.72,GREEN,12/17/2001
9,Stella,Conner,25,Rufjah Square,Nicekal,FL,04334,$3817.15,YELLOW,06/30/1970
10,Mollie,Valdez,65,Puoz Court,Gabevuz,MT,40606,$7532.53,GREEN,03/12/2062
11,Allie,Riley,49,Gaah Path,Habispaz,LA,12151,$9236.12,WHITE,02/19/1956
12,Anthony,Hicks,62,Awegu Loop,Emujhe,ID,45984,$2432.62,GREEN,03/23/2030
13,Ethel,Roy,41,Locte Terrace,Pehowbej,MS,25663,$2163.42,WHITE,08/18/2004
14,Rosie,Clark,38,Felri Circle,Eswupis,NE,70814,$2997.88,BLUE,04/29/2033
15,Kathryn,Rose,26,Icip Heights,Tiffugad,TX,34775,$7060.06,BLUE,04/17/1936
16,Alice,Phelps,46,Zofsep Turnpike,Wefbotra,MO,33638,$9153.69,GREEN,05/20/1936
17,Aaron,Tyler,62,Lafi Point,Ovuecune,NC,01940,$8969.44,RED,05/31/1903
18,Noah,Watson,40,Onuhud Extension,Cebtowju,AL,96770,$4880.37,WHITE,05/26/1963
19,Hilda,Newman,30,Milek Path,Tikiffom,WV,62047,$5613.31,GREEN,04/18/2043
20,Darrell,Jimenez,56,Derni Glen,Culmioho,IL,37024,$994.72,BLUE,11/10/2068
21,Bradley,Pena,44,Loso Path,Fesaplev,LA,94188,$2033.01,GREEN,04/15/1995
22,Brett,Dixon,25,Temas View,Ostuji,ID,11129,$2507.63,GREEN,01/03/1942
23,Eunice,Johnston,41,Sacjoj Way,Sesniof,OR,36402,$837.77,RED,01/20/1919
24,Bessie,Obrien,51,Dupfez Terrace,Dugufat,NY,25194,$2663.83,YELLOW,05/19/2040
25,Cynthia,Walker,27,Bugi Square,Elopezam,ME,88738,$6505.82,WHITE,06/07/1939
26,Jeff,Fields,33,Poij Way,Temavo,TN,47806,$1941.32,GREEN,09/15/1980
27,Michael,Ballard,54,Taliz Glen,Zivuat,AR,36058,$7582.90,WHITE,12/30/1917
28,Johanna,Hammond,65,Riti Junction,Betnusso,DE,38254,$3440.00,RED,06/07/1926
29,Todd,Dean,22,Hobag Path,Weliteosi,ID,77714,$2734.01,RED,01/12/1935
30,Brent,Guzman,20,Sapa Plaza,Himlujab,NM,80552,$8054.85,WHITE,03/13/2026
31,Josie,Tran,32,Tikjoj Highway,Iwcoko,AR,65251,$9503.26,GREEN,04/22/1952
32,Effie,Floyd,32,Nakwu Circle,Hanujoku,ME,61785,$6697.75,YELLOW,10/26/1972
33,Terry,Mann,34,Tifocu Grove,Zeforva,TX,74931,$2583.08,BLUE,06/14/1915
34,Gary,Rogers,54,Zuopo Place,Nujgunce,SD,17221,$3644.73,GREEN,07/07/2063
35,Earl,Ross,37,Mamlod Place,Elfopva,RI,29877,$1100.50,WHITE,05/28/1970
36,Alex,Sherman,20,Kagij Turnpike,Gilaku,ID,52401,$5640.08,YELLOW,01/25/1960
37,Harriet,Bowen,24,Motfe Court,Umvucgo,IN,30749,$1564.70,RED,09/27/1989
38,Matthew,Elliott,26,Cigag Drive,Suduszam,LA,70539,$8204.21,YELLOW,07/06/2015
39,Myra,Byrd,18,Vobjac Heights,Faherofi,GA,43083,$6888.62,YELLOW,02/09/1919
40,Ruth,Ellis,61,Puol Center,Sehuvlu,RI,91130,$614.86,BLUE,10/19/2056
41,Jeffery,Collier,19,Enowe Drive,Mafreal,ME,65721,$8453.93,BLUE,07/26/1955
42,Lillian,Allison,42,Peow Junction,Otzalbuz,MS,59157,$7848.60,RED,02/05/2065
43,Albert,Franklin,48,Zufmum Turnpike,Bagovzig,WA,58039,$6887.25,YELLOW,10/25/1976
44,Devin,Welch,20,Ofvo Way,Lihunluz,MD,11516,$9783.87,GREEN,08/31/2003
45,Lela,Jacobs,44,Veco Loop,Wikpufa,IA,75378,$9944.54,YELLOW,03/27/1903
46,Randy,Burke,33,Tojvo Center,Ewedehru,MN,48130,$6269.32,BLUE,06/30/1983
47,Peter,Simon,53,Zappat Square,Idabola,WI,13828,$3026.00,YELLOW,02/06/2013
48,Eleanor,Carlson,31,Puin Extension,Vudketjo,RI,47650,$709.85,RED,08/30/1932
49,Estella,Crawford,39,Masdez Point,Kesiecu,NV,14999,$447.87,WHITE,05/05/2017
50,Lilly,Diaz,47,Kolfan Junction,Meulji,FL,33982,$8428.03,YELLOW,09/25/1916
51,Matilda,Phillips,45,Odto Lane,Ubetunow,RI,06551,$5210.29,RED,10/29/1943
52,Phillip,Stanley,62,Hukki Place,Getuowu,KY,10162,$3673.28,WHITE,07/29/1920
53,Aaron,Carter,50,Walvu Court,Lajawpum,CA,03313,$3404.15,RED,03/24/2005
54,Elizabeth,Buchanan,23,Adtav Center,Wipkoru,ND,23918,$6395.14,BLUE,10/22/2066
55,Jordan,Jones,33,Tacca Road,Mukguzwa,VA,26300,$9765.76,RED,11/17/1955
56,Sally,Burgess,56,Duvu Place,Nerzabbe,WY,22741,$5364.07,BLUE,04/28/1902
57,Phoebe,Parks,61,Mabavo Turnpike,Ohmule,WA,17856,$8019.18,BLUE,02/28/2065
58,Joe,Joseph,31,Vefzum Park,Gujcakip,AZ,36567,$1114.68,BLUE,05/11/1954
59,Tillie,Cooper,35,Dettuf Highway,Ativijig,CO,80467,$7953.02,WHITE,07/12/2002
60,Caroline,Vaughn,50,Lijram Point,Huonoduk,IN,27017,$2311.58,WHITE,03/20/1934
61,Esther,Jacobs,39,Pene Glen,Rabcarvis,SD,16650,$9539.78,GREEN,12/10/1944
62,Richard,Soto,52,Kobdoh Park,Nijateb,NM,50951,$7644.97,GREEN,04/14/2065
63,Ricky,Kelly,49,Kowzi Street,Didagnip,VT,16472,$8826.02,GREEN,05/24/2055
64,Keith,Ramos,57,Vahoz Heights,Uphuzi,OH,92083,$4139.57,YELLOW,02/10/1950
65,Emma,Luna,62,Elbed Heights,Ojomudne,NJ,54566,$8863.79,WHITE,02/02/1995
66,Frances,Palmer,38,Hohda Junction,Tipgatac,AR,14857,$2258.35,YELLOW,03/02/1908
67,Lillian,Sparks,40,Hezti Street,Koamite,IN,07163,$1568.43,RED,12/17/2038
68,Hettie,Neal,35,Nipce Trail,Himupef,AL,24512,$1259.11,GREEN,05/08/1970
69,Johnny,Ford,64,Apgoc Extension,Juseske,NM,11945,$5626.62,GREEN,09/03/1927
70,Mabelle,Mann,58,Miro Pass,Fotuben,KY,06765,$9223.42,BLUE,10/13/2025
71,Bobby,Cannon,39,Kihu Avenue,Niunwur,SD,37908,$6171.46,GREEN,07/04/2006
72,Jordan,Garza,41,Jugci Place,Badele,CA,15606,$4543.67,GREEN,11/20/1935
73,Lillie,Ruiz,34,Zudhil Loop,Takuviza,MO,36384,$9578.96,GREEN,01/27/1903
74,Darrell,Farmer,60,Ovecu Street,Uwagured,MI,61187,$5368.79,BLUE,01/29/1940
75,Mable,Frank,19,Nobwa Key,Osedetez,KS,40753,$2879.31,BLUE,04/24/1912
76,Lenora,Dixon,39,Tazko Path,Cewcafif,IA,07111,$6190.16,RED,06/24/1962
77,Sam,Hardy,25,Culku Street,Hegzihku,GA,28637,$7326.04,YELLOW,04/03/1986
78,Eric,Johnston,60,Nime Mill,Pohafpe,TX,22226,$1887.54,RED,02/22/1959
79,Steve,McCormick,47,Wagec Heights,Fasubreh,PA,03519,$465.49,GREEN,03/10/1920
80,Maude,Logan,39,Izzat Road,Fikjeluf,NM,71283,$7387.07,BLUE,11/20/2015
81,Adelaide,Malone,64,Tici Grove,Hukovic,DE,71093,$251.88,BLUE,11/10/2025
82,Olga,Moran,28,Dovul River,Ejkewnew,AZ,09458,$353.37,BLUE,08/15/2012
83,Ophelia,Hammond,26,Tizef Street,Zowuwbub,IL,44379,$5547.82,WHITE,10/04/2069
84,Iva,Hall,63,Mete Way,Zowjirwi,VA,19497,$9680.11,WHITE,10/25/1903
85,Nettie,Butler,44,Pobsek Parkway,Pegejrut,OR,02419,$1138.03,WHITE,02/09/1937
86,Elijah,Cole,28,Apopef Loop,Vurvicges,AL,92753,$4688.77,GREEN,04/24/1977
87,Ophelia,Lambert,56,Buse Grove,Urtuferu,WV,43258,$8594.99,WHITE,04/06/2066
88,Abbie,Cruz,51,Akmuw Lane,Weugiban,HI,91416,$7817.94,YELLOW,03/05/2001
89,Ryan,Fuller,24,Pegtib Grove,Orhejuv,ND,55136,$7481.41,YELLOW,12/09/2029
90,Warren,Barton,20,Fohu Square,Eszouni,CT,36020,$9664.55,WHITE,08/15/2047
91,Gary,Hernandez,34,Voeko Boulevard,Adunonuve,NJ,73171,$7404.82,GREEN,03/24/1939
92,Verna,Bass,31,Nivca Point,Noekumom,GA,43431,$806.78,GREEN,04/11/1925
93,Joseph,Kelley,50,Haofo Lane,Nidvicoco,NJ,80471,$2026.31,BLUE,03/18/2051
94,Victoria,Torres,59,Wugi Place,Ikhuvek,OK,24898,$1758.33,RED,03/27/1970
95,Russell,Newton,47,Labca Drive,Uggucnir,DC,99594,$5040.19,RED,02/13/2015
96,Ryan,Williamson,29,Geko Glen,Epwabima,OR,69779,$4083.77,GREEN,12/28/1986
97,Barry,Bush,47,Kikor Pike,Gidamras,WI,29490,$1642.43,RED,09/19/1919
98,Floyd,Hubbard,58,Bepe Grove,Vesuciro,AL,27973,$4469.23,GREEN,12/07/2000
99,Benjamin,Lowe,36,Ukaeki Circle,Tondero,IN,17148,$8627.43,YELLOW,03/15/1939
100,Katie,Gomez,55,Hiri Glen,Ovkamlav,VA,99328,$2782.76,WHITE,09/01/1987
nicholasserra commented 3 years ago

Thanks! I'll add a test to this upstream.