POST input to mail() - php

echo '<div id="email">
<form action="#" method="POST">
<label>E-mailadres:</label>
<p><input type="text" name="mail1" value="me#me.nl"> </p>
<input type="submit" name="submitemail">
</form>
</div>';
$to = 'MY#MAIL.COM';
$lala = $_POST['mail1'];
// subject
$subject = 'Subject';
// message
$message = $selected . $totaal .'';
// 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";
$headers .= 'From: ' . $lala . ' <' . $lala . '>';
// Mail it
mail($to, $subject, $message, $headers);
Sending the email is working fine, it just wont let catch the inserted email.
I can't get the value of <input .... name="mail1"> (me#me.nl) into the "FROM:" section.
What do i wrong OR what is the thing that i don't do in this case ?
Whenever using $headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n"; it works perfectly.

Try
echo '<div id="email">
<form action="a.php" method="POST">
<label>E-mailadres:</label>
<p><input type="text" name="mail1" value="me#me.nl"> </p>
<input type="submit" name="submitemail">
</form>
</div>';
if (isset($_POST['submitemail'])) {
$to = 'MY#MAIL.COM';
$lala = $_POST['mail1'];
// subject
$subject = 'Subject';
// message
$message = $selected . $totaal . '';
// 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";
$headers .= 'From: ' . $lala . ' <' . $lala . '>';
// Mail it
mail($to, $subject, $message, $headers);
}
the a.php is the name of ur php file

Related

How can I fix this PHP Mail Form issue?

I have a php contact form on my website. However when my customers input their e-mail address and submit their message I got a reply to my own e-mail. For example if they enter adam#yahoo.com I recieve the e-mail as myusername#myhostingprovider.com .... How can I fix this so the reply section shows the e-mail the customer input? Thank you.
Here is my code for my contact form
<form action="php/contact.php" method="post" class="contactForm" id="contactForm">
<fieldset>
<div class="formValidationError bg-red-dark color-white" id="contactNameFieldError">
<p class="center-text uppercase small-text">Name is required!</p>
</div>
<div class="formValidationError bg-red-dark color-white" id="contactEmailFieldError">
<p class="center-text uppercase small-text">Mail address required!</p>
</div>
<div class="formValidationError bg-red-dark color-white" id="contactEmailFieldError2">
<p class="center-text uppercase small-text">Mail address must be valid!</p>
</div>
<div class="formValidationError bg-red-dark color-white" id="contactMessageTextareaError">
<p class="center-text uppercase small-text">Message field is empty!</p>
</div>
<div class="formFieldWrap">
<label class="field-title contactNameField" for="contactNameField">Name:<span>(required)</span></label>
<input type="text" name="contactNameField" value="" class="contactField requiredField" id="contactNameField"/>
</div>
<div class="formFieldWrap">
<label class="field-title contactEmailField" for="contactEmailField">Email: <span>(required)</span></label>
<input type="text" name="contactEmailField" value="" class="contactField requiredField requiredEmailField" id="contactEmailField"/>
</div>
<div class="formTextareaWrap">
<label class="field-title contactMessageTextarea" for="contactMessageTextarea">Message: <span>(required)</span></label>
<textarea name="contactMessageTextarea" class="contactTextarea requiredField" id="contactMessageTextarea"></textarea>
</div>
<div class="formSubmitButtonErrorsWrap">
<input type="submit" class="buttonWrap button button-grey contactSubmitButton" id="contactSubmitButton" value="SUBMIT" data-formId="contactForm"/>
</div>
</fieldset>
</form>
Also here is the code for the PHP script
<?php
// variables start
$name = "";
$email = "";
$message = "";
$name = trim($_POST['contactNameField']);
$email = trim($_POST['contactEmailField']);
$message = trim($_POST['contactMessageTextarea']);
// variables end
// email address starts
$emailAddress = 'myemail#yahoo.com';
// email address ends
$subject = "Mywebsite.com | Mobile - Message From: $name";
$message = "<strong>From:</strong> $name <br/> <strong>E-Mail:</strong> $email </br><br/> <strong>Message:</strong> $message";
$headers = 'From: '. $name . '<' . $email . '>' . "\r\n";
$headers = 'Reply-To: ' . $email . "\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//send email function starts
mail($emailAddress, $subject, $message, $headers);
//send email function ends
?>
In your code, you haven't been adding to your header string, you've just been resetting it each time.
What you're finally sending to the mail function in the header variable is just;
'Content-type: text/html; charset=iso-8859-1' . "\r\n"
What you need to do is either where you set $headers = , change the equals sign so it looks like this $headers .=
As that means that it will add on to the String. Or, just format it as one massive string.
<?php
$headers = 'From: '. $name . '<' . $email . '>' . "\r\n";
$headers .= 'Reply-To: ' . $email . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//send email function starts
mail($emailAddress, $subject, $message, $headers);

How do you use PHP to send form data as a message?

I need to have the visitor of my website send form data (such as a contact form to my email via PHP. How will I be able to do this?
With GET/POST query and using tag
html forms / w3schools
for example
.html page:
<form method="GET" action="send.php">
<input name="fieldname" type="text" placeholder="You text here...">
<input type="submit" value="Submit">
</form>
send.php
<?php
if (isset($_GET['fieldname']) {
// you code here..
}
example send email by function mail()
about mail() function on php.net
$from = 'fromemailsend#mail';
$to = 'emailtosend#mail';
$subject = 'your subject';
$message = 'your<br>message<br>in html code';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8 \r\n";
$headers .= 'To: Author <' .$to . ' >' . "\r\n";
$headers .= 'From: '.$author.' <'.$from.'>' . "\r\n";
mail($to, $subject, $message, $headers);

Complicated Contact Form Submit (ajax help)

How can I run this contact form without reloading or changing the page on submit? I was told to use ajax but I don't know where to start with that.
I would like to be able to display the output ('Successfully sent! Thank you.') on my index.html inside a div for example.
<?php if (isset($_POST["contact"])){
$to = 'contact#example.com'; //Contact Email
$from = $_POST["email"];
$name = $_POST["name"];
$message = $_POST["message"];
// subject
$subject = 'New message from '.$name;
// 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";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: '.$from . "\r\n";
// Mail it
if(mail($to, $subject, $message, $headers)){
echo "Successfully sent! Thank you.";
} else {
echo "Oops! Failed to send your message";
}
}
?>
Try the below codes =)
html
<form id="contactform" name="contactform" method="post" action="email.php">
<div>
<input type="text" name="name">
</div>
<div>
<input type="email" name="email">
</div>
<div>
<input type="text" name="subject">
</div>
<div>
<textarea name="message"></textarea>
</div>
<button type="submit">
Send Message
</button>
</form>
ajax
var form = $('#contactform');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div></div>');
$.ajax({
type:"post",
url: $(this).attr('action'),
data: $(this).serialize(),
beforeSend: function(){
form.prepend( form_status.html('<p>Sending</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p>Done</p>').delay(3000).fadeOut();
});
});
email.php
<?php
$to = 'contact#example.com'; //Contact Email
$from = $_POST["email"];
$name = $_POST["name"];
$message = $_POST["message"];
// subject
$subject = 'New message from '.$name;
// 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";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: '.$from . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>

echo form value doesn't pass to mail function

I have tried to search online for similar problem, but i just couldn't get it work, it's driving me nuts. haiz. your_email just couldnt pass.
The form
echo '<form class=location_submit name="togo" action="" method="post" >';
echo '<input class="user_email" type="text" name="your_email" placeholder="Enter your email here" required size="55"> <br />';
echo '<input class="location_submit_button" type="image" name="button" src="images/send_location_button.png" />';
echo '<input type="hidden" name="button_pressed" value="1" />';
echo '</form>';
The mail function
if(isset($_POST['button_pressed']))
{
$to = $your_email;
$subject = "Sent from Utourpia!";
$message = "Message body";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Terence Leong # Utourpia <terence#utourpia.me>" . "\r\n";
$headers .= "Reply-To: terence#utourpia.me" . "\r\n";
$headers .= 'Bcc: terence#popby.me' . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers, '-f terence#utourpia.me');
echo '<p class=email_sent>Email Sent.</p>';
}
Things worth to note is email sent does execute when i click the submit button, but $your_email simply returns NULL :((
you need to extract the values from the $_POST using their names;
$to = $_POST['your_email'];

send html email and receive it as text

I have this php code to send an HTML email :
function sendEmail($to,$from,$subject,$body) {
$To = $to;
$Subject = $subject;
$Message = $body;
$from = "Do Not Reply";
$Headers = "From: sender#taskmanager.domain.ca"." \r\n" .
"Reply-To: ".$from." \r\n" .
'MIME-Version: 1.0' . "\r\n".
'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($To, $Subject, $Message, $Headers);
}
function sendEmailUser($fromUserObj, $userInfo){
$html="
<html>
<body>
<p>
Dear ".$userInfo["firstName"] ." ".$userInfo["lastName"]." ,</p>
<p>
Welcome to IT .</p>
<p>
Account Information:</p>
<p>
Username:".$userInfo["username"]."</p>
<p>
Password: ".$userInfo["password"]."</p>
<p>
You can login <a href = 'http://domain.com'>here</a> .</p></body>
</html>";
$subject = "subject";
sendEmail($userInfo["email"], $subject, $html);
}
I receive the email normal text, with not html interpreted.
<html> <body> <p> Dear test lastname ,</p> <p> Welcome to IT.</p> ... </html>
what's wrong with my code ??
please any help I'll appreciate it.
try decalring your header variable like this
$Headers = "From: sender#taskmanager.domain.ca"." \r\n";
$Headers .="Reply-To: ".$from." \r\n" ;
$Headers .= 'MIME-Version: 1.0' . "\r\n";
$Headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
function sendEmail parameters -> ($to,$from,$subject,$body) (4 parameters)
you are calling -> sendEmail($userInfo["email"], $subject, $html); (3 parameters)
it should be:
sendEmail($userInfo["email"], $email_from, $subject, $html);

Categories