-1

I am working on an email sending project in python

when i pass %s in post method with a variable its discarding all new lines and sending it to email so im getting all stuffed emails can somebody please help

post method :

r = requests.post('https://maker.ifttt.com/trigger/Custom Newsfeed/with/key/nYmoaoROeu6Mf2SrBOgUg?value1=%s' % (s))

so if s contains a news with headline and body, in email im getting it all concatenated together

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Achutha K
  • 37
  • 5
  • You need to provide more details. What is `s` that is being passed in here? What is the result you are getting and how does that differ from what you expect? – Daniel Roseman Nov 12 '17 at 19:38
  • s has a title and body of news something like : abcdefg xyz now when i send this in post method it gives me like : abcdefgxyz and i expect it to send an email to me like exactly s, i.e, abcdefg xyz – Achutha K Nov 12 '17 at 19:43
  • consider there is a newline after abcdefg – Achutha K Nov 12 '17 at 19:44
  • Could you please check my answer? what what happens if you do print(url) after declaring url? i.e. second line of my example. – alexisdevarennes Nov 12 '17 at 19:58
  • updated answer, you can either urlencode the newlines or use urllib and run urlencode() – alexisdevarennes Nov 12 '17 at 20:13

2 Answers2

-1

If on python3, try 'value1={}'.format(s) Or if on 3.6, try f'value1={s}'

CHURLZ
  • 166
  • 10
  • i am in 3.6.1 the one you told to try with 3.6 is not working, infact it is not even considering s but sending email but the one you told to try with python3 is working but same result as i was facing initially – Achutha K Nov 12 '17 at 19:59
  • Don't miss the f in front of the string. It's valid syntax from 3.6 onwards. – CHURLZ Nov 12 '17 at 20:00
  • Also please provide more info on the errors and return in your op – CHURLZ Nov 12 '17 at 20:01
  • if i add that f in front of the string then im getting syntax error like : File "", line 1 url = 'https://maker.ifttt.com/trigger/Custom Newsfeed/with/key/nYmoaoROeu6Mf2SrBOgUg?f'value1={s}' ^ SyntaxError: invalid syntax – Achutha K Nov 12 '17 at 20:10
  • The keyword f needs to go in front of the whole string. In my example, value represented the whole URL – CHURLZ Nov 12 '17 at 21:31
-1

Use format() to construct that url. Also, learn about using named placeholders using format(), the example provided below makes use of them.

s = s.replace('\n', '%0A')  # Replaces \n with urlencoded \n (%0A)
url = 'https://maker.ifttt.com/trigger/Custom Newsfeed/with/key/nYmoaoROeu6Mf2SrBOgUg?value1={value1}'.format(value1=s)
r = requests.post(url)

You could also use urlencode.

>>> import urllib
>>> f = { 'value1' : s}
>>> urllib.urlencode(f)
'value1=cool+event'
alexisdevarennes
  • 5,437
  • 4
  • 24
  • 38
  • this is giving the same result – Achutha K Nov 12 '17 at 20:05
  • What is the result exactly? Can you do print(url) so we see what happens to the url after calling format() ? – alexisdevarennes Nov 12 '17 at 20:06
  • Works perfectly here on 2.7 and 3.5, i.e. the url gets assigned s. – alexisdevarennes Nov 12 '17 at 20:07
  • >>> s = '324 asdasd aasd' >>> url = 'https://maker.ifttt.com/trigger/Custom Newsfeed/with/key/nYmoaoROeu6Mf2SrBOgUg?value1={value1}'.format(value1=s) >>> url 'https://maker.ifttt.com/trigger/Custom Newsfeed/with/key/nYmoaoROeu6Mf2SrBOgUg?value1=324 asdasd aasd' – alexisdevarennes Nov 12 '17 at 20:07
  • OK, updated my answer. Do a replace('\n', '%0A') on s. i.e. s.replace('\n', '%0A') - %0A is urlencoded. – alexisdevarennes Nov 12 '17 at 20:11
  • the result is being the same even after urlencoding and when u used 324 asdasd thing was that a space ib between or a newline because space is getting formatted but not newline – Achutha K Nov 12 '17 at 20:22
  • Can you please provide us with the output of print(url) very hard to see what is going on otherwise if we cant see the final url you are using when making the request. Also please do not down rank answers that actually do solve the question you asked. You are not dealing with formatting issues but with encoding issues. – alexisdevarennes Nov 13 '17 at 01:16
  • https://maker.ifttt.com/trigger/Custom Newsfeed/with/key/nYmoaoROeu6Mf2SrBOgUg?value1= Microsoft Kaizala launches in India%0AMicrosoft India today announced the launch of Microsoft Kaizala, a mobile-only product designed for large group communications and work management. Microsoft Kaizala makes it simple for organizations to seamlessly communicate, collaborate and complete tasks, bringing together de.................................................... it continues like everything is stuffed – Achutha K Nov 14 '17 at 07:26