PHP Form Displays Web Server instead of Sender - php

I'm fairly new to this but I've done all the digging I can do. When I submit this form rather than displaying the sender's email (which is what I want) it displays the webserver I'm using in the From part. I'm not sure what I need to put to make display anyone's email.
<?php
$email = $_POST['email'];
$recipient = "my#email.com";
$subject = "Email Notice";
$headers = 'From: '.$email."\r\n".
if(mail($recipient, $subject, $headers)){
header("Location: thankyou.html");
}else{
header("Location: sorry.html");
}
exit;
?>

Related

PHP E-mail form - Define sender

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

Contact form sends everything to spam

My contact form works fine but sends everything to spam when I set the recipient as Gmail account and sends nothing if I set it as my domain email client (eg info#mydomain.com). Is there something wrong in the code? What do I need to do?
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "info#mydomain.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: message-sent.html#contact'); exit();
?>
Second question. I set up the location to redirect a user to a thank-you page. How can I set it to open in a new tab instead?

Emailing when submit button is pressed

<?php
echo $this->Form->end(__('Submit'))
$to = "someone#hotmail.com"; $subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
Hey guys need help here! I want when someone pressed the submit button, it saves and sends the details entered to the emails
You have to use like this.Make sure your button type is submit
<?php
if($_POST){
$to = "someone#hotmail.com"; $subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
}
?>
If you are still running WAMPServer then you are on a Windows PC.
Window does not come with a mail server, and the mail() function does little else other than pass a mail onto a mail server.
You will either need to install a mail server, they do exist for windows, but this is not a simple task for a beginner.
Alternatively, look at phpMailer which can be used to send mail and basically piggy backs an existing email account like one of your yahoo or gmail accounts
Try the below code,
<?php
if(isset($_POST['submit'])){ // check if submit button is pressed
$to = "someone#hotmail.com"; $subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
if(mail($to,$subject,$message,$headers))
echo "Mail Sent.";
else
echo 'Error while sending mail.';
}
?>

Php form: email copy to sender

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.

Sending Email Twice from PHP

I'm doing this simple testing on creating form where I want the form to send email twice:
to my email address, to notify me if somebody has reached me
to the sender's email address to notify him/her that the contact has been sent to me.
problem is, I could send the first email, but I couldn't send the second one. I thought this should be easy. I may miss a line or two here. Here's the code:
<?php
$field_email = $_POST['email'];
$mail_to = 'myemail#mydomain.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message .= 'E-mail: '.$field_email."\n";
$headers = 'From: '.$field_email."\r\n";
$headersTo = 'From: '.$mail_to."\r\n";
$body_messageTo = 'Thank you for your interest';
$subjectTo = 'Thank You from Gaban';
$mail_status = mail($mail_to, $subject, $body_message, $headers);
$autoreply = mail($field_email, $subjectTo, $body_messageTo, $headersTo);
?>
the $field_email should take data directly from the "email" form from the HTML code.
Unless $_POST['email'] contains an invalid email address, I can't see anything wrong with it. I'd say the problem is most likely to be found in the mailserver processing the mailbox where the second email is sent to.

Categories