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
Related
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...");
?>
I'm trying to create a HTML form, which will in the end send an e-mail using the folling PHP Code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$destinationemail = "myemail#domain.com";
$emailcontent = "Name: {$name}\n\nE-Mail: {$email}\n\nMessage: {$subject}\n{$message}";
$subject = "Contact from Domain.com";
$from = $email;
mail($destinationemail, $subject, $emailcontent) or die("Error!");
echo "Thank you $name!";
?>
The problem is, everytime i receive an e-mail, i get is as if being sent from a what i guess is the Webhost general e-mail.
htgkaylg#server776.web-hosting.net
<htgkaylg#server776.web-hosting.net>
dom 08/10/2017, 18:40
Você;
I would like it to be received something like this:
myemail#domain.com
<myemail#domain.com>
dom 08/10/2017, 18:40
Você;
Is it possible?
Thank you,
Vítor
You have to use the Header as 4th parameter in mail() function.
Add this line and change mail as follow:
$headers = "From:" . $from . "\r\n";
mail($destinationemail, $subject, $emailcontent, $headers) or die("Error!");
Ref. https://www.w3schools.com/php/func_mail_mail.asp
This question already has an answer here:
php form sending email, but not in correct way [closed]
(1 answer)
Closed 5 years ago.
My contact form on my web page finally send emails, but it doesn't send it in the proper format. This is the email of what I am getting. I've hidden the email and organization for privacy reasons.
From-- norgun
Subject-- Test Email: ***#gmail.com Message: Sbsbdb
Message--
MIME-Version: 1.0
Content-type: text/plain; charset=iso-8859-1
From: Test <***#gmail.com>
Reply-To: <***#gmail.com>
X-Mailer: PHP/7.0.21
Anyway, as you can see from the above, the name, email, and message content that the person would've written in the contact form are in the subject line instead of the actual email box. Is there a way that I could format my code so that the message and the name that they wrote down are in the message box instead of the subject box, and the email gets sent from the person who put their email in the form, not "norgun" which is what I came up with for the website?
Here is my code so far:
<?php
$to = 'index#indexmarkets.biz';
$name = !empty($_POST['name']) ? filter_var(trim($_POST['name']), FILTER_SANITIZE_STRING) : '';
$from = !empty($_POST['email']) ? filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL) : $to;
$message = !empty($_POST['message']) ? filter_var(trim($_POST['message']), FILTER_SANITIZE_STRING) : '';
$body = "Name: {$name}\r\nEmail: {$from}\r\nMessage: {$message}";
$body = wordwrap($body, 70, "\r\n");
$headers = [
'MIME-Version: 1.0',
'Content-type: text/plain; charset=iso-8859-1',
"From: $name <$from>",
"Reply-To: <$from>",
'X-Mailer: PHP/' .phpversion()
];
$success = mail($to, $body, implode("\r\n", $headers));
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
die(json_encode(['success' => $success]));
}
echo $success ? 'Sent Successfully.' : 'An error occurred';
ini_set('display_errors', 1); error_reporting(E_ALL);
That's because you're passing your variable $body as the subject parameter to the mail() function. This line:
$success = mail($to, $body, implode("\r\n", $headers));
Should instead be something like this:
$success = mail($to, $subject, $body, implode("\r\n", $headers));
Make sure you set $subject to something.
I have contact form. I'd like to add CC to e-mail: abc#abc.de and change e-mail sender. Currently it shows my server as a sender, I'd like to have reply-to form users.
Hello.
I have contact form. I'd like to add CC to e-mail: abc#abc.de and change e-mail sender. Currently it shows my server as a sender, I'd like to have reply-to form users.
<?php
session_start();
//Ajax Questions Form
if(isset($_POST['email'])){
$name = $_POST['name'];
$email = $_POST['email'];
$arrival = $_POST['arrival'];
$departure = $_POST['departure'];
/// $adults = $_POST['adults'];
// $children = $_POST['children'];
// $room = $_POST['room'];
$requests = $_POST['requests'];
$to = 'contact#test.camp'; //Replace with recipient email address
$subject = 'Hotel Booking'; //Subject line for emails
$message = 'From: '.$name."\r\n".'Email: '.$email."\r\n".'Arrival: '.$arrival."\r\n".'People: '.$departure; //."\r\n".'Adults: '.$adults."\r\n".'Children: '.$children."\r\n".'Room: '.$room."\r\n".'Requests: '.$requests;
// Mail Functions
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message) or die('Error sending Mail'); //This method sends the mail.
echo "Your email was sent!"; // success message
}
}
//Contact Php Form
if(isset($_POST['contact_email'])){
$contact_name = $_POST['contact_name'];
$email = $_POST['contact_email'];
$contact_message = $_POST['message'];
$to = 'marek#gmail.com'; //Replace with recipient email address
$subject = 'Contact Form'; //Subject line for emails
$message = 'From: '.$contact_name."\r\n".'Email: '.$email."\r\n".'Message: '.$contact_message;
// Mail Functions
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // This line checks that we have a valid email address
mail($to, $subject, $message) or die('Error sending Mail'); //This method sends the mail.
}
}
?>
The php mail function does not have much functionality try using something like PHPMailer which allows you to send more complex emails
For add CC or BCC or ReplyTo add header to your email structure :
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
$headers[] = 'To: andreas<mail1#gmail.com>, thomas<mail2#gmail.com>';
$headers[] = 'From: from <from#gmail.com>\r\nReply-to: <ReplyTo#gmail.com>';
$headers[] = 'Cc: Cc#gmail.com';
$headers[] = 'Bcc: Bcc#gmail.com';
mail($to, $subject, $message, implode("\r\n", $headers));
I'm creating a 'forgot password' page where the user enters their email address and the script finds the password associated to that email, and sends it to the stored email address.
I believe the problem has to do with my SMTP Mailserver. I am using WAMP which doesn't have one so I downloaded one that was free.
This is the php script I'm using:
$id = checkEmail($email);
$record = readMemberRecord($id);
$password = #mysql_result($record,0,'cred');
if($password){
$email_subject = "Email Request";
$email_body = "Your password is ".$password.".";
$header = "NinjaMan";
mail($email, $email_subject, $email_body,$header);
$msg = "Your password has been sent to ".$email.".";
}else{
$msg = "Sorry, the email ".$email." wasn't found.";
}
The $msg outputs properly so I know the code is passing the mail function.
Try sending in a proper "From" in $header.
$emailFrom = "admin#yourdomain.com"; // match this to the domain you are sending email from
$email = "example#example.com";
$subject = "Email Request";
$headers = 'From:' . $emailFrom . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "Return-path: " . $email;
$message = "Your password is ".$password.".";
mail($email, $subject, $message, $headers);
See details of the mail() function.
If this doesn't work, try using PHPMailer. You configure it in code, no need to edit php.ini.
I've used it in some projects (v 2.0.4, I see the latest is 5.1) and had no problems.
Try using Google's server to send mails, you can see how to do that here
Try using this
//Email information
$to = "garggarima#gmail.com";
$subject = "Test mail";
$message = "Hello! This is a test email message.";
$from = "support#sltechsoft.com";
$headers = "From:" . $from;
$mail=mail($to,$subject,$message,$headers);
if($mail) {
echo "Thanks for mail";
} else {
echo "Mail not Sent";
}
//Email response
echo "Thank you for contacting us!";