Using Javascript in php

I’ve got a form and I want to send the data to an email which the user can then check before sending. I’ve got the results in $body but when I try and put that into an email link I just get $body in the email rather than what var_dump($body); shows it should be.

This is the code I’m using:

header("Location: mailto:email@address.com?Subject=New message from website&body=$body");

Wouldn’t you need to url-encode that anyway? I can’t see it’s going to like the spaces in your Subject unless you change them to %20.

The only reason I can think of for it not substituting the contents of $body is if you use single quotes around the header location string.

I think you might need to come out of the quotes for that bit. I have a bit of code on my site that works like this

header( 'Location: '.$link_go'' ) ;

so i think you probably need to do something like

header("Location: mailto:email@address.com?Subject=New message from website&body=".$body."");

You can probably lose the last ." but either way is worth a try.

Thanks that worked but only if the message didn’t have too many spaces, I think I need to do what droopsnoot suggested and use url-encode but how do I do that?

Thanks

simples…

header("Location: mailto:email@address.com?Subject=New message from website&body=".urlencode
    ($body)."");

Replace the spaces with %20
With double quotes like you had originally, you should not need to exit them to insert a variable, so I’m not sure why that did not work. Maybe because it’s on the end of the string it is best to close the quotes anyway, you should not need to open and close them again after the variable.

Not quite, you need to urlencode() the “subject” as well as the body text.

Thank you that worked but there was a ‘+’ symbol in-between every word, is it possible to get rid of that?

oh yeah :slight_smile: missed that. thanks

they get replaced by spaces when the field is decoded

It’s sent to an email instead which mean that there’s no decoding. The email message just appears as:

"Hi+Whoever

This+email+has+come+from+your+website"

A quick search suggests you should use rawurlencode() instead of urlencode(), as the latter will convert spaces to plus signs, and the former will not.

1 Like

thank you

side note: using mailto: is

  1. unreliable (it requires the user to have a mail client installed) and
  2. impolite (you’re forcing the user to use his/her own email address for sending)
1 Like

I appreciate that but didn’t know any other options as the script I was using has stopped working and the client doesn’t want to speak to her hosts about any updates they’ve done so asked me to do it like this.

I agree it’s not the best way though

Why don’t you just fix the script that stopped working?

for sending emails it’s recommended to use an email library (SwiftMailer, PHPMailer) or an email service (MailChimp, …)

I’ve checked through the script and there’s no problems with the actual script - everything is output properly and as it should do but nothing is ever received from it.

It’s a form for people to email the owner of the site - would those work for that?

I would think so, they’re just alternatives to using the PHP mail() function. If the old script is suddenly not working, you mentioned that the owner didn’t want to talk to the host about updates, do you have any idea of what the updates might be? Or you could post the script code here, anonymised of course, to see if anyone can spot what’s going wrong.