Help with PHP mailer script - php

I'm clueless when it comes to PHP and have a script that emails the contents of a form. The trouble is it only sends me the comment when I want it to also send the name and email address that is captured.
Anyone know how I can adjust this script to do this?
A million thanks in advance!
<?php
error_reporting(E_NOTICE);
function valid_email($str)
{
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*#([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
if($_POST['name']!='' && $_POST['email']!='' && valid_email($_POST['email'])==TRUE && strlen($_POST['comment'])>1)
{
$to = "me#me.com";
$headers = 'From: '.$_POST['email'].''. "\r\n" .
'Reply-To: '.$_POST['email'].'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$subject = "Contact Form";
$message = htmlspecialchars($_POST['comment']);
if(mail($to, $subject, $message, $headers))
{
echo 1; //SUCCESS
}
else {
echo 2; //FAILURE - server failure
}
}
else {
echo 3; //FAILURE - not valid email
}
?>

You could do
$extra_fields = 'Name: '.$_POST['name'].'<br>Email: '.$_POST['email'].'<br><br>Message:<br>';
$message = $extra_fields.$_POST['comment'];
Not exactly clean, but you get the point. Just concatenate the data in with the $message.

Change this line:
$message = htmlspecialchars($_POST['comment']);
to
$message = htmlspecialchars($_POST['name'] . $_POST['email'] . "\r\n" . $_POST['comment']);
Or something to that effect

The problem is your $message = ... line, its only including the $_POST['comment']) variable. You need to add the $_POST['name'] and $_POST['email'] like something below:
$message = '';
$message .= htmlspecialchars($_POST['name']) . "\n";
$message .= htmlspecialchars($_POST['email']) . "\n";
$message .= htmlspecialchars($_POST['comment']) . "\n";

Related

Why tags in contact form are not working?

On my webpage, I have a contact form, which sends messages directly to my mail. Unfortunately, I receive everything in one line, tags are not working and also when I want to reply to that mail, instead of mail there is:
To:my#mail.comrnReply-TornContent-Typetext/html
To: PHP/7.2.7 <>
I am totally lost, where should be a problem. Does anyone know what's going on, please?
session_start();
if (!empty($_POST)) {
if ( (isset($_POST['email'])) && (strlen($_POST['message']) > 5) ) {
$receiver = "my#mail.com";
$to = $receiver;
$subject = 'New message - mypage.com';
$message = 'New e-mail sent by contact form, from mypage.com.<br> Contact: ' . $_POST['email'] . '<br>Text :<br> ' . $_POST['message'] . '';
$headers = 'From: ' .$receiver."rn" .
'Reply-To: ' .$email."rn" .
"Content-Type: text/html; charset=UTF-8" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) {
$warning = '<div class="sent"><p class="success">Your message was successfully sent. Thanks for your time.</p></div>';
$_SESSION['hlaska'] = $warning;
} else {
$warning = '<p class="error">Unexpected error.</p>';
}
} else {
$warning = '<p class="error">You did not fill all fields.</p>';
}
}

Mail Function is giving false return in PHP

I have tried sending email from a basic html form by php mail() function and It has given me false return
Code:
<?php
if(isset($_POST['submit'])){
$title = $_POST['title'];
$name = $_POST['first_name'];
$name1 = $_POST['last_name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$company=$_POST['lead_object'];
$skype= $_POST['skype_id'];
$to = 'himanshu#webkidukan.com';
$subject = 'Advertiser Form Details';
$from = 'ssing648#gmail.com';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Sir</h1>';
$message .= '<p style="color:#080;font-size:18px;">Details of Advertiser Form</p>';
$message .= '<p style="color:#080;font-size:18px;"><?php $title . " " . $name . " " . "?>wrote the following:"<?php . "\n\n" . $name . "\n\n" . $name1 . "\n\n" .$email . "\n\n" .$phone . "\n\n" .$company. "\n\n" .$skype. "\n\n"?> </p>';
$message .= '</body></html>';
$t = mail($to, $from, $message, $headers,$subject);
var_dump($t);exit;
if ($t) {
echo "Mail Sent. Thank you " . $name . " .We will contact you shortly.";
}else{
echo "Failed to send email. Please try again later";
}
echo "<script type='text/javascript'>alert('Thanks for filling out our form! We will look over your message and get back to you soon.')</script>";
echo "<script> window.location.href = 'advertiser.php';</script>";
}
?>
I have this issue in three forms together , hence posting one for the understanding the mistake, that I am doing. Can anyone of you help me with the same.Or should I go for the SMTP mail option.Also I am sending the form details in mail to the user.So also check that the way to send the flyer is right or not.
There are some unwanted PHP tags while appending message. try like this
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Sir</h1>';
$message .= '<p style="color:#080;font-size:18px;">Details of Advertiser Form</p>';
$message .= '<p style="color:#080;font-size:18px;">'.$title." ".$name." wrote the following:"."\n\n".$name."\n\n".$name1."\n\n".$email."\n\n".$phone."\n\n".$company."\n\n".$skype."\n\n".'</p>';
$message .= '</body></html>';

Multiple field PHP form submission

The problem I'm having is that I want to pass through data from a number of fields in a form into one email send via PHP, the concerned code is as follows:
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
// This still needs to be debugged online as soon as possible
$from = $_POST['inputemail'];
$name = $_POST['inputname'];
$phone = $_POST['inputphone'];
$date = $_POST['inputdate'];
$time = $_POST['inputtime'];
$data = $_POST['inputprotection'];
$subject = $_POST['inputdropdown'];
$message = $_POST['inputmessage'];
$message = wordwrap($message, 70);
mail("UP498701#myport.ac.uk",$subject,$message,"From: $from\n" . "Name: $name\n" . "Phone: $phone\n" . "Date: $date\n" . "Time: $time\n" . "Keep Data? $data\n");
echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
}
}
This is where I'm declaring all the information to be passed as a header for the email. The current PHP passes syntax check as works to the extent that an email is sent containing the $to, $subject, $message all fine, but the $header only passes through the final part (Keep Data? $data\n). When I remove all the other fields and simply keep the $data part, the email stops sending any $header. I also have an issue with the redirect, which has been removed from the below code and will be inserted as soon the current issue is resolved. The current full PHP is:
<html>
<body>
<?php
function spamcheck($field)
{
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
<?php
if (isset($_POST['inputemail']))
{
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
// This still needs to be debugged online as soon as possible
$from = $_POST['inputemail'];
$name = $_POST['inputname'];
$phone = $_POST['inputphone'];
$date = $_POST['inputdate'];
$time = $_POST['inputtime'];
$data = $_POST['inputprotection'];
$subject = $_POST['inputdropdown'];
$message = $_POST['inputmessage'];
$message = wordwrap($message, 70);
mail("UP498701#myport.ac.uk",$subject,$message,"From: $from\n" . "Name: $name\n" . "Phone: $phone\n" . "Date: $date\n" . "Time: $time\n" . "Keep Data? $data\n");
echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
}
}
?>
</body>
and is temporarily hosted at http://luke-hale.com/temp/contact.html. I'm sure this is just a case of being misinformed on my part, but any help would be great, cheers!
EDIT:
okay so that issue is sorted with:
$subject = $_POST['inputdropdown'];
$message = $_POST['inputmessage'] . "\r\n\r\n" . $_POST['inputname'];
$message = wordwrap($message, 70);
$headers = "From: " . $_POST['inputemail'];
mail("UP498701#myport.ac.uk",$subject,$message,$headers);
echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
Which works though doesn't display the $headers anywhere, but I'm not too fussed about this, the next thing is the redirect, which I would usually run through via:
header("refresh:5;url=http://www.domain.com")
Though this was not working correctly earlier, I will apply an edit when tested for anyones future reference.
EDIT
So the form works but the re-direct does not. The site is still hosted on the same domain, but now the full PHP looks like this:
<html>
<body>
<?php
function spamcheck($field)
{
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
<?php
if (isset($_POST['inputemail']))
{
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
$subject = $_POST['inputdropdown'];
$message = "Name: " . $_POST['inputname'] . "\r\n\r\n" . "Email: " . $_POST['inputemail'] . "\r\n\r\n" . "Phone: " . $_POST['inputphone'] . "\r\n\r\n" . "Date: " . $_POST['inputdate'] . "\r\n\r\n" . "Time: " . $_POST['inputtime'] . "\r\n\r\n" . "Retain Data? " . $_POST['inputprotection'] . "\r\n\r\n" . "Message: " . $_POST['inputmessage'];
$message = wordwrap($message, 70);
$headers = "From: " . $_POST['inputemail'];
mail("UP498701#myport.ac.uk",$subject,$message,$headers);
echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
}
}
header("refresh:5;url=http://www.domain.com")
?>
</body>
The form still sends fine but the return states that the line beginning "header" cannot be passed because the browser data has already been modified. I'm not sure sure what I've done or where so if anyone could lend a hand, that'd be great!
FINAL EDIT
My final, fully working, code:
<?php
function spamcheck($field)
{
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
<?php
ob_start();
if (isset($_REQUEST['inputemail'])&&($_REQUEST['inputname'])&&($_REQUEST['inputmessage']))
{
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid email, you'll be redirected ";
header("refresh:5;url=http://www.luke-hale.com/");
}
else
{
$subject = $_POST['inputdropdown'];
$message = "Name: " . $_POST['inputname'] . "\r\n\r\n" . "Email: " . $_POST['inputemail'] . "\r\n\r\n" . "Phone: " . $_POST['inputphone'] . "\r\n\r\n" . "Date: " . $_POST['inputdate'] . "\r\n\r\n" . "Time: " . $_POST['inputtime'] . "\r\n\r\n" . "Retain Data? " . $_POST['inputprotection'] . "\r\n\r\n" . "Message: " . $_POST['inputmessage'];
$message = wordwrap($message, 70);
$headers = "From: " . $_POST['inputemail'];
mail("UP498701#myport.ac.uk",$subject,$message,$headers);
echo "Thank you for your messgae, I'll get back to you as soon as possible! You'll be redirected in 5 seconds.";
}
header("refresh:5;url=http://www.luke-hale.com/");
}
else
{
echo "You did not fill all the required fields, please try again.";
header("refresh:5;url=http://www.luke-hale.com/");
}
?>
Try putting your form data into the $message variable, insert \n after each attribute to give a new line. Do not use the header.
Location and Refresh both require an absolute URI
header('Location: http://www.domain.com');
Examples for mail:
https://stackoverflow.com/a/22833890/3489793
https://stackoverflow.com/a/22831825/3489793
mail("Your Email Address Here",$subject,$therest,"subject: $subject\n");
U said this:
"The mail function is also not the issue, and correct syntax is" mail($to,$subject,$message,$headers,$parameters)
Explanation:
your email adress = $to
$subject = $subject
$therest = $message (rename it if you want)
"subject: $subject\n" = the header (change it to $headers if you want that)
Header example (as in the documentation)
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
You could echo a Javascript at the bottom of your PHP script which redirects instead, like this:
$url = "url";
$time = 5000 // time in milliseconds
print "<script>window.setTimeout(function(){window.location='".$url."'},".$time.") </script>"

PHP - need help in php form element (Header part)

I am a beginner in php.
I am trying php to send mail.
My mail goes to mail client but it does not show the header part well as I wanted.
I am using the following codes ----
<?php
//validation expected data exists
if(!isset($_POST["name"]) ||
!isset($_POST["city"]) ||
!isset($_POST["email"]) ||
!isset($_POST["phone"]) ||
!isset($_POST["message"])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
//Subject
$name = $_POST["name"];
$subject = "NO REPLY";
$email = $_POST["city"];
$phone = $_POST["email"];
$website = $_POST["phone"];
$message = $_POST["message"];
$header = "from: $name <$email>";
$to = 'info#mishmihillsadventure.in';
$send_contact=mail($to,$subject,$message,$header);
//Check, if message sent to your email
// Display message "We've recived your information"
if($send_contact){
echo "We've received your contact information";
}
else{
echo "ERROR";
}
?>
$email = $_POST["city"];
$phone = $_POST["email"];
Is this really what you want? Shouldn't it be:
$email = $_POST["email"];
And try the following headers:
$header = 'From: ' . $name . '<' . $email . '>' . "\r\n";
Use (atmost) following headers while sending mail via PHP -
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$header .= "From: $name <$email>" . "\r\n";
//If want to `CC` someone add
$header .= 'Cc: abc#email.com' . "\r\n";
Using variables in Double quotes is fine.
You can try something like in code mentioned below,
<?php
$to = 'test#to.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: test#from.com' . "\r\n" .
'Reply-To: test#from.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)) {
echo 'Msg send..';
} else {
echo 'Msg Not send..';
}
?>

How to add custom message in to my form mailer

I want to add "This message came from etcetc.com" in the e-mail form in the e-mail body itself. Hope this make sense..
my sent_email.php
<?php
$email_to = 'test#test.org';
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n" . 'Reply-To: ' . $email;
if(mail($email_to, $subject, $message, $headers)) {
echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..
} else {
echo 'failed'; // ... or this one to tell it that it wasn't sent
}
?>
Like this:
<?php
$email_to = 'test#test.org';
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = "This message came from etcetc.com \r\n".$_POST['message'];
$headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n" . 'Reply-To: ' . $email;
if(mail($email_to, $subject, $message, $headers)) {
echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..
} else {
echo 'failed'; // ... or this one to tell it that it wasn't sent
}
?>
All you need to do is add your text to the end of $message.
Change:
$message = $_POST['message'];
to
$message = $_POST['message'] . "\n\nThis message came from etcetc.com";
Add message before and after as you wish by
$message = "This is before posted message\r\n";
$message .= $_POST["message"];
$message .= "\r\nthis is after the posted message";

Categories