I am a designer trying to get a PHP to email script working.
The HTML form is the standard Skeleton CSS one from their website at
http://getskeleton.com/#forms:
<form>
<div class="row">
<div class="six columns">
<label for="exampleEmailInput">Your email</label>
<input class="u-full-width" placeholder="test#mailbox.com" id="exampleEmailInput" type="email">
</div>
<div class="six columns">
<label for="exampleRecipientInput">Reason for contacting</label>
<select class="u-full-width" id="exampleRecipientInput">
<option value="Option 1">Questions</option>
<option value="Option 2">Admiration</option>
<option value="Option 3">Can I get your number?</option>
</select>
</div>
</div>
<label for="exampleMessage">Message</label>
<textarea class="u-full-width" placeholder="Hi Dave …" id="exampleMessage"></textarea>
<label class="example-send-yourself-copy">
<input type="checkbox">
<span class="label-body">Send a copy to yourself</span>
</label>
<input class="button-primary" value="Submit" type="submit">
</form>
I am trying to amend the existing PHP script to match this and having an awful lot of trouble. This is my base plate for the PHP:
<?php
if($_REQUEST['name'] == '' || $_REQUEST['email'] == '' || $_REQUEST['message'] == ''):
return "error";
endif;
if (filter_var($_REQUEST['email'], FILTER_VALIDATE_EMAIL)):
$to = 'email#myemail.com';
$header = 'From: '. $_REQUEST['name'] . ' <'. $_REQUEST['email'] .'>'. "\r\n";
$header .= 'Reply-To: '. $_REQUEST['name'] . ' <'. $_REQUEST['email'] .'>'. "\r\n";
$header .= 'X-Mailer: PHP/' . phpversion();
$subject = "Hello";
$message .= 'Name: ' . $_REQUEST['name'] . "\n";
$message .= 'Email: ' . $_REQUEST['email'] . "\n";
$message .= 'Message: '. $_REQUEST['message'];
$event = $_GET['exampleRecipientInput']
$mail = mail( $to, $url , $message, $header );
return $mail ? "success" : "error";
else:
return "error";
endif;
?>
I cannot understand how to adapt the PHP which is probably easy for experienced devs but fairly impenetrable for designers. Can anyone help?
As ceejayoz said, you're going to need to add name attributes to the input fields, then you'll be able to access them via $_POST['whatever_name']
Your HTML code could look like the following (added name attributes):
<form>
<div class="row">
<div class="six columns">
<label for="exampleEmailInput">Your email</label>
<input class="u-full-width" placeholder="test#mailbox.com" id="exampleEmailInput" type="email" name="email">
</div>
<div class="six columns">
<label for="exampleRecipientInput">Reason for contacting</label>
<select class="u-full-width" id="exampleRecipientInput" name="reason">
<option value="Option 1">Questions</option>
<option value="Option 2">Admiration</option>
<option value="Option 3">Can I get your number?</option>
</select>
</div>
</div>
<label for="exampleMessage">Message</label>
<textarea class="u-full-width" placeholder="Hi Dave …" id="exampleMessage" name="message"></textarea>
<label class="example-send-yourself-copy">
<input type="checkbox">
<span class="label-body">Send a copy to yourself</span>
</label>
<input class="button-primary" value="Submit" type="submit">
</form>
You need to ensure you're checking if the submit button has been, in fact, submitted:
<?php
if (isset($_POST['submit']) {
if($_POST['name'] == '' || $_POST['email'] == '' || $_POST['message'] == ''):
return "error";
endif;
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)):
$to = 'email#myemail.com';
$header = 'From: '. $_POST['name'] . ' <'. $_POST['email'] .'>'. "\r\n";
$header .= 'Reply-To: '. $_POST['name'] . ' <'. $_POST['email'] .'>'. "\r\n";
$header .= 'X-Mailer: PHP/' . phpversion();
$subject = "Hello";
$message .= 'Name: ' . $_POST['name'] . "\n";
$message .= 'Email: ' . $_POST['email'] . "\n";
$message .= 'Message: '. $_POST['message'];
$event = $_POST['reason'];
$mail = mail( $to, $url , $message, $header );
return $mail ? "success" : "error";
else:
return "error";
endif;
}
?>
The if (isset($_POST['submit'])) portion will let the script know that the form has been submitted, and will run the code inside of the brackets.
Note: I changed the $_REQUEST[''] to $_POST['name']. These will be associated to the field names I've provided at the top of the HTML edit.
Related
My server receives emails but none of the fields are shown.
All fields are empty in the email? Do you have any suggestions? I have tried everything that I know but no results. Form fields does not pass to the php
<?php
// variables start
$name = "";
$email = "";
$message = "";
$name = trim($_POST['contactNameField']);
$email = trim($_POST['contactEmailField']);
$message = trim($_POST['contactMessageTextarea']);
// variables end
// email address starts
$emailAddress = 'mail#domain.com';
// email address ends
$subject = "Message From: $name";
$message = "<strong>From:</strong> $name <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
?>
for this form
<form action="php/contact.php" method="post" class="contactForm" id="contactForm">
<div class="form-field form-name">
<label class="contactNameField color-theme" >Name:<span>(required)</span></label>
<input type="text" name="contactNameField" id="contactNameField" />
</div>
<div class="form-field form-email">
<label class="contactEmailField color-theme" >Email:<span>(required)</span></label>
<input type="text" name="contactEmailField" id="contactEmailField" />
</div>
<div class="form-field form-text">
<label class="contactMessageTextarea color-theme" >Message:<span>(required)</span></label>
<textarea name="contactMessageTextarea" id="contactMessageTextarea"></textarea>
</div>
<div class="form-button">
<input type="submit" class="btn bg-highlight text-uppercase font-900 btn-m btn-full rounded-sm shadow-xl contactSubmitButton" value="Gönder" />
</div>
Thank you
I have tried too many different ways to get reply-to from the $_request[email] but it keeps sending the mails with the $from CGI- mailer, although all the body on my mail work´s fine..
I have tried too many ways but i can't find where is my problem.. i have looked at several answers to this question here but not any one fixes my problem.. this is my code.
<?php
$subject = 'Contacact from website';
$to = 'contact#myhosting.com';
$emailTo = $_REQUEST['email'];
// an email address that will be in the From field of the email.
$name = $_REQUEST['name'];
$email = $_REQUEST['email']; // i can't get this going to the reply-to section on the mail
$phone = $_REQUEST['phone'];
$msg = $_REQUEST['message'];
$email_from = $name.'<'.$email.'>';
$headers = "MIME-Version: 1.1";
$headers .= "Content-type: text/html; charset=utf-8";
$headers .= 'From: ' . $fromName . ' <' . $fromEmail .'>' . " \r\n" .
'Reply-To: '. $fromEmail . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$message .= 'Name : ' . $name . "\n";
$message .= 'Email : ' . $email . "\n";
$message .= 'phone : ' . $phone . "\n";
$message .= 'Message : ' . $msg;
if (#mail($to, $subject, $message, $email_from)) {
// Transfer the value 'sent' to ajax function for showing success message.
echo 'sent';
} else {
// Transfer the value 'failed' to ajax function for showing error message.
echo 'failed';
}
?>
and this is my form:
<form name="contactForm" id='contact_form' method="post" action='email.php'>
<div class="row">
<div class="col-md-4">
<div id='name_error' class='error'>write your name</div>
<div>
<input type='text' name='name' id='name' class="form-control" placeholder="Name">
</div>
<div id='email_error' class='error'>Write a valid email</div>
<div>
<input type='email' name='email' id='email' class="form-control" placeholder="
email">
</div>
<div id='phone_error' class='error'>Write a phone number.</div>
<div>
<input type='tel' name='phone' id='phone' class="form-control" placeholder="Your name">
</div>
</div>
<div class="col-md-8">
<div id='message_error' class='error'>Please write your message here</div>
<div>
<textarea name='message' id='message' class="form-control"
placeholder="Message or quotation"></textarea>
</div>
</div>
<div class="col-md-12">
<p id='submit'>
<input type='submit' id='send_message' value='Enviar' class="btn btn-line">
</p>
<div id='mail_success' class='success'>We received your message :)</div>
<div id='mail_fail' class='error'>Please try again :/</div>
</div>
</div>
</form>
There are a few things wrong with what you posted.
Firstly, the first line for this block of code:
$message .= 'Name : ' . $name . "\n";
$message .= 'Email : ' . $email . "\n";
$message .= 'phone : ' . $phone . "\n";
$message .= 'Message : ' . $msg;
should not have a leading dot for $message .=, it should read as:
$message = 'Name : ' . $name . "\n";
Then this line:
if (#mail($to, $subject, $message, $email_from))
Since you're using $email_from as the last argument, mail() as you did for the other instance where you are sending mail, is using a valid From: with an E-mail address as it's coming "from", when the 2nd one does not contain that, you only declared it as $email_from = $name.'<'.$email.'>';.
What you will need to do is add the From: as you did for the first mailing instance.
Side note: The # symbol is an error suppressor. You might want to remove that during testing/development.
Consult the manual on the mail() function for more detail:
https://www.php.net/manual/en/function.mail.php
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
So everything works except that I don't receive an email after sending and wonder why?
keeps getting this error "Could not execute mail delivery program '/usr/local/bin/sendmail -oi -t' in"
Html
<form action="process.php" method="post">
<ul>
<li>
<label for="name">Name:</label>
<input type="text" name="name" id="name" />
</li>
<li>
<label for="email">Email:</label>
<input type="text" name="email" id="email" />
</li>
<li>
<label for="topic">Topic:</label>
<select>
<option value="optiona">optiona</option>
<option value="optionb">optionb</option>
<option value="optionc">optionc</option>
</select>
</li>
<li>
<label for="message">your message:</label>
<textarea id="message" name="message" cols="42" rows="9"></textarea>
</li>
<li><input type="submit" value="Submit"></li>
</form>
PHP
<?php
$to = 'robin.kahrle#gmail.com';
$subject='hi there you';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$message = <<<EMAIL
Hi! My name is $name.
$message
From $name
my email is $email
EMAIL;
$header ='$email';
if($_POST){
mail($to, $subjects, $message, $header);
$feedback = 'Thankyou for your email';
echo $feedback;
}
?>
Maybe your $header and $message definition is not ok. Try this:
<?php
// Constants
$to = 'robin.kahrle#gmail.com';
$subject='hi there you';
// Variable contents from the form
$name = (!empty($_POST['email'])) ? $_POST['name'] : '????';
$email = (!empty($_POST['email'])) ? $_POST['email'] : $to;
$message = 'Hi? my name is ' . $name;
if (!empty($_POST['message'])) $message .= . "\r\n" . $_POST['message'];
// Header
$header = 'From: ' . $email . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send mail and give feedback
$accepted = mail($to, $subjects, $message, $header);
if ($accepted) {
$feedback = 'Thankyou for your email';
} else {
$feedback = 'Email not accepted';
}
echo $feedback;
I have a PHP mail form from a Template that sends the email to me, but it appears all the variables are blank.
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = #trim(stripslashes($_POST['yourname']));
$email = #trim(stripslashes($_POST['youremail']));
$subject = #trim(stripslashes($_POST['yoursubject']));
$message = #trim(stripslashes($_POST['yourmessage']));
$email_from = $email;
$email_to = 'kylef33#gmail.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
?>
And here, the HTML:
<div class="col-sm-6">
<h1>
Contact Form
</h1>
<p>
Fill out the form to enquire directly and we will get back to you as soon as possible.
</p>
<div class="status alert alert-success" style="display: none">
</div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<input name="yourname" type="text" class="form-control" required="required" placeholder="Name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input name="youremail" type="text" class="form-control" required="required" placeholder="Email address">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea name="yourmessage" id="message" required class="form-control" rows="8" placeholder="Message"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger btn-lg">Send Message</button>
<input type="hidden" name="yoursubject" value="Enquiry">
</div>
</div>
</div>
</form>
</div>
But the resulting email is:
Name:
Email:
Subject:
Message:
With none of the responses. Where am I going wrong?
Edit:
If I set the variables manually in the php file the message comes through fine:
$name = 'John';
$email = 'email#email.com;
$subject = 'Enquiry Form';
$message = 'Message here.';
I think the php file isn't getting the variables from the form correctly. How do I fix this?
I don't see the header of the email. try to add this:
$headers = "MIME-Version: 1.0\r\nFrom: $noReplay\r\nReply-To: $noReplay\r\nContent-Type: text/html; charset=utf-8";
where $noReplay is the email address to show to the receiver and the header is sent as a last parameter in mail() function.
Hope this helps!
Keep on coding,
Ares.
Try By use simple code and Set Content-type: text/html in headers it may useful:
$name = $_POST['yourname'];
$email = $_POST['youremail'];
$subject = $_POST['yoursubject'];
$message = $_POST['yourmessage'];
$email_from = $email;
$email_to = 'kylef33#gmail.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
//set the headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: <'.$email_from.'>' . "\r\n";
// Mail it
mail($email_to, $subject, $body, $headers);
Basically I have a page which displays to a user only when they're logged in, it has a custom contact form on it. When the form is submitted, their user role should automatically change to "Author".
I literally have no idea where to start with this, if anyone has any ideas or can help me out a little that would be amazing because I'm really struggling with this.
EDIT
This is what I currently have, all it does at the minute is send emails.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = trim(stripslashes($_POST['name']));
$email = trim(stripslashes($_POST['email']));
$subject = trim(stripslashes($_POST['subject']));
$message = trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'email#email.com';
$body = '<html><head></head><body>' .
'<b>Name:</b> ' . $name . '<br><br>' .
'<b>Email:</b> ' . $email . '<br><br>' .
'<b>Nature of Enquiry:</b> ' . $subject . '<br><br>' .
'<b>Message:</b> ' . nl2br($message) .
'</body></html>';
$headers = "From: " . strip_tags($email_from) . "\r\n";
$headers .= "Reply-To: ". strip_tags($email_from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if( isset($_POST['checkbox']) ) {
mail($email_to, $subject, $body, $headers);
// Change user role to "author
echo '<p>You are now an author.</p>';
} else {
echo '<p>It appears you are a spambot, if this is a mistake please try again and check the "I am not a spambot" checkbox.</p>';
}
}
?>
<form method="post" action="#">
<label for="name">Name</label>
<input type="text" id="name" name="name" value="<?php echo $name; ?>" required>
<label for="email">Email Address</label>
<input type="email" id="email" name="email" value="<?php echo $email; ?>" required>
<label for="subject">Nature of Enquiry</label>
<input type="text" id="subject" name="subject" value="<?php echo $subject; ?>" required>
<label for="checkbox">I am not a spambot</label>
<input type="checkbox" id="checkbox" name="checkbox">
<label for="message">Message</label>
<textarea id="message" name="message" required><?php echo $message; ?></textarea>
<button type="submit">Submit</button>
</form>
This would be a matter of using the class WP_User and its set_role method.
$user_id = 2;
$role_name = 'author';
$user = new WP_User( $user_id );
$user->set_role( $role_name );