I have the following code saved as send.php:
<?php
$to_email = "person#gmail.com";
$subject = $_POST['subject'];
$body = "Hi, This is test email send by PHP Script";
$headers = "From: Me#myWebsite.com";
if ( mail($to_email, $subject, $body, $headers)) {
echo("Email successfully sent to $to_email...");
} else {
echo("Email sending failed...");
}
?>
The email sends if I navigate to https://myWebsite.com/Stuff/send.php
and make $subject = "something"
But if I set $subject to be a POST variable. It does not send the subject when I write this URL in the browser (as in, the email still sends but the subject is now blank):
https://myWebsite.com/Stuff/send.php?subject=notworking
To get both $_POST and $_GET you can use $_REQUEST which will work for both.
In your code replace
$subject = $_GET['subject'];
With
$subject = $_REQUEST['subject'];
If someone direct hit the URL in browser without paramter subject you can prevent them using
if(!empty($_REQUEST['subject']))
{
$to_email = "person#gmail.com";
$subject = $_REQUEST['subject'];
$body = "Hi, This is test email send by PHP Script";
$headers = "From: Me#myWebsite.com";
if ( mail($to_email, $subject, $body, $headers))
echo("Email successfully sent to $to_email...");
else
echo("Email sending failed...");
}
If you want to extract the values from the URL parameters then you need to use the $_GET
global variable to get the data like this,
<?php
$to_email = "person#gmail.com";
$subject = $_GET['subject'];
$body = "Hi, This is test email send by PHP Script";
$headers = "From: Me#myWebsite.com";
if ( mail($to_email, $subject, $body, $headers)) {
echo("Email successfully sent to $to_email...");
} else {
echo("Email sending failed...");
}
?>
Try it.
<?php
$to_email = "person#gmail.com";
$subject = (isset($_GET['subject']) && (!empty($_GET['subject']))?$_GET['subject']:"No Subject";
$body = "Hi, This is test email send by PHP Script";
$headers = "From: Me#myWebsite.com";
if ( mail($to_email, $subject, $body, $headers))
echo("Email successfully sent to $to_email...");
else
echo("Email sending failed...");
?>
Related
PHP CODE BELOW: Already done configuration in php.ini & sendmail.ini
<?php
$to_email = "receipient#gmail.com";
$subject = "Simple Email Test via PHP";
$body = "Hi,nn This is test email send by PHP Script";
$headers = "From: sender\'s email";
if (mail($to_email, $subject, $body, $headers)) {
echo "Email successfully sent to $to_email...";
} else {
echo "Email sending failed...";
}
Getting the following error:
To insert a PHP variable in a string you have to use curly brackets.
Like this:
<?php
$to_email = "receipient#gmail.com";
$subject = "Simple Email Test via PHP";
$body = "Hi,nn This is test email send by PHP Script";
$headers = "From: sender's email";
if (mail($to_email, $subject, $body, $headers)) {
echo "Email successfully sent to {$to_email}..."; // <-- here the brackets
} else {
echo "Email sending failed...";
}
The first code without $_Post works, the second one with $_POST doesnt. It is on the server in cpanel. Anything helps :)
I tried various things like changing the code in HTML, placing slashes, also using $_request over $_post.
This works -
$to_email = "example#example.hr";
$subject = "Simple Email Test via PHP";
$body = "Hi,nn This is test email send by PHP Script";
$headers = "From: me#example.hr";
if ( mail($to_email, $subject, $body, $headers)){
echo("Email successfully sent to $to_email...");
} else {
echo("Email sending failed...");
}
$to_email = "example#example.hr";
$subject = "Simple Email Test via PHP";
$body = "Poruka:$_POST[poruka]";
$headers = "From: $_POST[email]\n";
if ( mail($to_email, $subject, $body, $headers)){
echo("Email successfully sent to $to_email...");
} else {
echo("Email sending failed...");
}
</code></pre>
The first code sends the email, while the second one goes into echo ("Email sending failed...")
I am trying to adjust my code to able to reply to sender email from PHP contact form. please check my code below to give advise. Thank you
<?php
$marke = $_POST['marke'];
$modell = $_POST['modell'];
$name = $_POST['name'];
$adresse = $_POST['adresse'];
$telefon = $_POST['telefon'];
$email = $_POST['email'];
$to = 'myemail#gmail.com';
$from = 'myemail#gmail.com';
$subject = 'Contact Form';
$body = "marke: $marke\n modell: $modell\n name: $name\n adresse: $adresse\n
email: $email\n telefon: $telefon\n";
?>
<?php
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
header("Location: http://www.website.com/sent.php");
} else {
echo '<p>Oops! An error occurred. Try sending your message again.</p>';
}
}
?>
First make headers
$headers = "From: $from\r\nReply-to: $email";
Than fix calling of mail function to be
mail ($to, $subject, $body, $headers)
Didn't tried it from times when it was PHP 4 but it will probably work as you expected...
Addition:
I just checked on php.net... go to this url http://php.net/manual/en/function.mail.php and check "Example #2 Sending mail with extra headers."
I am trying to create a simple PHP script that will send email to my gmail account, the script is working and sending the email, but everything appears in the subject line as such:
from: me
to: me#gmail.com
date: Wed, Mar 28, 2018 at 10:20 AM
subject: Here is what was sent: Name : me<br>Emailme#gmail.com<br>Message: Testing this form 1<br>
mailed-by: my.server.cloud
And nothing in the body of the email! I am definitely doing something wrong, so here is my code:
if (isset($_REQUEST['email']))
{
//send email
$to = "me#gmail.com";
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$from = $_REQUEST['email'] ;
$subject = "Email request from " ;
$message = $_REQUEST['message'] ;
$header = "From: $from";
mail( $to, $from, "$subject: $name", $message, $header );
echo "Thank you $name for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<p>Your message was not sent, please click the back button on your
browser and correct the mistakes</p>;
}
Change your
mail( $to, $from, "$subject: $name", $message, $header );
to
mail( $to, "$subject: $name", $message, $header );
Remember to only put the from-address into the header, not as an additional parameter
I have a simple form. Basically trying to replicate share by email thought this would be sufficient. I'd like to send a copy of this email to the $email variable (Yeah that stripslashes might not be necessary), any ideas on how to do so? Came across bunch of posts through google but couldn't figure it out;
<?php
$EmailFrom = "admin#test.com";
$EmailTo = "admin#test.com";
$Subject = "Check out this video.";
$email = !empty($_POST['email']) ? Trim(stripslashes($_POST['email'])) : false;
$Body = "Take a look at this; youtubelink...";
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
header('Location: /#');
?>
Just edit your script like this, the copy will be sent only if the original will:
<?php
$EmailFrom = "admin#test.com";
$EmailTo = "admin#test.com";
$Subject = "Check out this video.";
$email = !empty($_POST['email']) ? Trim(stripslashes($_POST['email'])) : false;
$Body = "Take a look at this; youtubelink...";
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
if ($success)
mail($EmailFrom, $Subject, $Body, "From: <$EmailFrom>");
header('Location: /#');
?>
Just add another mail(); function
<?php
$EmailFrom = "admin#test.com";
$EmailTo = "admin#test.com";
$Subject = "Check out this video.";
$email = !empty($_POST['email']) ? Trim(stripslashes($_POST['email'])) : false;
$BodyReceiver = "Take a look at this; youtubelink...";
$BodySender = "You sent the following message " . $BodyReceiver . " to " . $EmailTo . ".";
$successReceiver = mail($EmailTo, $Subject, $BodyReceiver, "From: <$EmailFrom>");
$successSender = mail($EmailFrom, $Subject, $BodySender, "From: <no-reply#text.com");
header('Location: /#');
?>
or something like that...
As bozdoz suggested you might do it with Bcc, but then it will be a total copy of the original. You will not be able to change the sender email or the massage (for instance to "You sent the following massage ... to ..." and whatnot.