I refer to this page.
https://bootstrapious.com/p/how-to-build-a-working-bootstrap-contact-form
i want mailheader "name"
but this php code is
<?php
// configure
$from = 'Demo contact form <demo#domain.com>';
$sendTo = 'Demo contact form <demo#domain.com>';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in the email
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
// let's do the sending
try
{
$emailText = "You have new message from contact form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
so i tried
$sendTo = 'Demo contact form '; => my mail.
$from = 'Demo contact form '; => $name <'$email'>
but the result is "nobody"
So i tried again.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
=>
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from<$email>,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
But it failed...
what should i do?
i tried
From: $FromName and
$headers = "From: $from_user \r\n". and
$headers .= 'From: "'. $from . '" <' . $Email . '>' . "\r\n"; etc...
This worked for me
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$from = "$name<$email_address>";
Related
How can I configure the variables $from & $subject on top so that the email the server sends me uses those values instead of static text?
<?php
// configure
$from = 'Demo contact form <demo#domain.com>';
$sendTo = 'Demo contact form <demo#domain.com>'; // Add Your Email
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'subject' => 'Subject', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in the email
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
// let's do the sending
try
{
$emailText = "You have new message from contact form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
I want $from to use the Name value and $subject to use the Subject value.
Use the following:
# checks if the 'name' key has been $_POST'ed
$from = isset($_POST['name'])?$_POST['name']:$from;
# checks if the 'subject' key has been $_POST'ed
$subject = isset($_POST['subject'])?$_POST['subject']:$subject;
my contact form keeps getting spam everyday. Not sure why. Here is contact-process.php to process a submission:
<?php
// configure
$from = 'webmaster#example.com';
$sendTo = 'Message from <myemail#mydomain.com>';
$subject = "Contact Form: $name";
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in the email
$human = intval($_POST['humans']);
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// let's do the sending
try
{
$emailText = "You have new message from contact form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
header('Location: /thank-you.php');
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
//Check if simple anti-bot test is correct
if(!empty($_POST['humans'])) {
// it's spam
} else {
// it's human
}
I have no idea why this is happening, I also set up a honeypot as well. Also the emails are coming in as being sent from hostname, not from the individual email addresses themselves.
Thanks for the help guys.
I inspected different topics there, but all of them are not working with my code. I need to get the ability to answer direct to submitter email via my email. Here is my code:
<?php
// configure
$from = '<mymail#gmail.com>';
$sendTo = '<mymail#gmail.com>';
$subject = 'New message from PROMO form';
$fields = array('name' => 'Name', 'city' => 'City', 'tel' => 'Tel', 'email' => 'Email', 'message' => 'Message', 'age' => 'Age', 'info' => 'Info', 'checkboxtwo' => 'Checkboxtwo'); // array variable name
$okMessage = 'Спасибо.';
$errorMessage = 'Извините, ошибка.';
$headers = 'From: ' . $fields['email'] . "\r\n" . 'Reply-To: ' . $fields['email'];
try
{
$emailText = "You have new message from online form form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
mail($sendTo, $subject, $emailText, $headers);
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
I will highly appreciate any help. Thank you.
THE ANSWER
Re-write your foreach like this:
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
$fields[$key] = $value; //It set the values on the array
}
}
Then add the headers:
$headers = 'From: ' . $fields['email'] . "\r\n" . 'Reply-To: ' . $fields['email'];
So just send the mail.
mail($sendTo, $subject, $emailText, $headers);
Other answers (ignore them)
Just set the headers with the "Reply-To".
$headers = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from;
mail($sendTo, $subject, $emailText, $headers);
You will get any replies to the email that you used to submit the message.
EDIT
Get the desired email by using this:
$sendTo = $fields['email'];
Then you will be able to send the email using this var.
New at PHP and trying to integrate mailgun to send to multiple forms. Below is the code for my PHP script which is using mailgun.
When sending the email using a simple mailgun script i am able to receive email but when i attach it to the forms i don't get any email.
require 'vendor/autoload.php';
use Mailgun\Mailgun;
else if($_GET['method'] == 'send_form'){
$to = 'myemail#email.com';
$message = 'Name : '.$_POST['name'].'<br />';
$message .= 'Mobile : '.$_POST['mobile'].'<br />';
$message .= 'Email : '.$_POST['email'].'<br />';
$from = $name.' <'.$from.'>';
$name = $_POST['name'];
$subject = 'Email Subj for form 1';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: '.$name.' <'.$from.'>' . "\r\n";
send_mail($to,$from,$subject,$message);
}
else if($_GET['method'] == 'send_form_two'){
$to = 'myemail#email.com';
$message = 'Name : '.$_POST['name'].'<br />';
$message .= 'Mobile : '.$_POST['mobile'].'<br />';
$message .= 'Email : '.$_POST['email'].'<br />';
$from = $name.' <'.$from.'>';
$name = $_POST['name'];
$subject = 'Email subject for form 2';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: '.$name.' <'.$from.'>' . "\r\n";
send_mail($to,$from,$subject,$message);
}
function send_mail($to, $from, $subject,$content)
{
$mgClient = new Mailgun('mailgunkey-here');
$domain = "mg.domain.com";
$result = $mgClient->sendMessage($domain, array(
'from' => $from,
'to' => $to,
'subject' => $subject,
'text' => $content
));
}
Seems like my code does not want 'from' => $from,
changing the from to static email resulted to sent mails.
I've inherited an email form from a former vendor and after we've moved it to it's new server it started to malfunction. What it's supposed to do is send a verification email to the user who filled it out (does this), then send an email to warranty#mydomain.com and bcc: another address (this it no longer does). Tried everything but I'm not super savvy with PHP, mostly a front-end coder, can anyone look at my code and tell me why this isn't sending properly?
PHP code:
<?php
header("location: http://www.anatomicglobal.com/warranty/regthanks.html");
$posting = array(
'Name' => $_POST['Name'],
'Email' => $_POST['Email'],
'Phone' => $_POST['Phone'],
'Address' => $_POST['Address'],
'City' => $_POST['City'],
'State' => $_POST['State'],
'Province' => $_POST['Province'],
'Zip' => $_POST['Zip'],
'Product' => $_POST['check'][0],
'Size' => $_POST['size'][0],
'Mattress Model Name' => $_POST['MattressModeName'],
'Mattress Model Number' => $_POST['MattressModeNo'],
'Serial Number' => $_POST['SerialNumber'],
'Store Name' => $_POST['StoreName'],
'Purchase Month'=> $_POST['Month'],
'Purchase Day' => $_POST['Day'],
'Purchase Year' => $_POST['Year']
);
$decide = $_POST['decide'];
$subject = 'Eco Memory Foam - Warranty Registration';
$to = 'warranty#anatomicglobal.com';
$to = $posting['Email'];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'To: Anatomic Global <warranty#anatomicglobal.com>' . "\r\n";
$headers .= 'From: ecomemoryfoam.com <warranty#anatomicglobal.com>' . "\r\n";
$headers .= 'Reply-To: ' . $posting['Email'] . "\r\n";
$headers .= 'bcc: Hollyce Weber <hweber#anatomicglobal.com>' . "\r\n";
$message = '<html>';
$message .= '<head><title>Eco Memory Foam - Warranty Registration</title></head><body>';
$message .= '<h1>Eco Memory Foam - Warranty Registration</h1>';
$message .= '<p><strong>Warranty Registration</strong> submission successful, please keep for your records.</p>
';
$message .= '<p>Below is the submitted information at: <strong>' . strftime("%B %d %Y - %H:%M:%S", time()) . '</strong></p>';
$message .= '<dl>';
foreach ($posting as $field => $value) {
$message .= '<dt>';
$message .= '<dd><b>' . $field . '</b>: ' . $value . '</dd>';
$message .= '</dt>';
};
$message .= '<dt>Decide to Purchase This Product?</dt>';
$message .= '<dd>Customer Selected:<ul>';
foreach ($decide as $field => $value) {
$message .= '<li>' . $value . '</li>';
};
$message .= '</ul></dd>';
$message .= '</dl>';
/**$message .= '<p>You can reply to the submitter by replying to this email (if they gave you a valid email address).</p></body></html>';**/
mail($to, $subject, $message, $headers);
a few hours later....
Thanks for the help guys, but it's still not sending anything, the new code looks like this:
<?php
$posting = array(
'Name' => $_POST['Name'],
'Email' => $_POST['Email'],
'Phone' => $_POST['Phone'],
'Address' => $_POST['Address'],
'City' => $_POST['City'],
'State' => $_POST['State'],
'Province' => $_POST['Province'],
'Zip' => $_POST['Zip'],
'Product' => $_POST['check'][0],
'Size' => $_POST['size'][0],
'Mattress Model Name' => $_POST['MattressModeName'],
'Mattress Model Number' => $_POST['MattressModeNo'],
'Serial Number' => $_POST['SerialNumber'],
'Store Name' => $_POST['StoreName'],
'Purchase Month'=> $_POST['Month'],
'Purchase Day' => $_POST['Day'],
'Purchase Year' => $_POST['Year']
);
$decide = $_POST['decide'];
$subject = 'Eco Memory Foam - Warranty Registration';
$to = "warranty#anatomicglobal.com, {$posting['email']}";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'To: Anatomic Global <warranty#anatomicglobal.com>' . "\r\n";
$headers .= 'From: ecomemoryfoam.com <warranty#anatomicglobal.com>' . "\r\n";
$headers .= 'Reply-To: ' . $posting['Email'] . "\r\n";
$headers .= 'bcc: Hollyce Weber <hweber#anatomicglobal.com>' . "\r\n";
$message = '<html>';
$message .= '<head><title>Eco Memory Foam - Warranty Registration</title></head><body>';
$message .= '<h1>Eco Memory Foam - Warranty Registration</h1>';
$message .= '<p><strong>Warranty Registration</strong> submission successful, please keep for your records.</p>
';
$message .= '<p>Below is the submitted information at: <strong>' . strftime("%B %d %Y - %H:%M:%S", time()) . '</strong></p>';
$message .= '<dl>';
foreach ($posting as $field => $value) {
$message .= '<dt>';
$message .= '<dd><b>' . $field . '</b>: ' . $value . '</dd>';
$message .= '</dt>';
};
$message .= '<dt>Decide to Purchase This Product?</dt>';
$message .= '<dd>Customer Selected:<ul>';
foreach ($decide as $field => $value) {
$message .= '<li>' . $value . '</li>';
};
$message .= '</ul></dd>';
$message .= '</dl>';
/**$message .= '<p>You can reply to the submitter by replying to this email (if they gave you a valid email address).</p></body></html>';**/
mail($to, $subject, $message, $headers);
header("location: http://www.anatomicglobal.com/warranty/regthanks.html");
put
header("location: http://www.anatomicglobal.com/warranty/regthanks.html");
at the end...
In your code, you have it so it is sending the email to only one address. Here you're building your to adress:
$to = 'warranty#domain.com';
$to = $posting['Email'];
If you notice, you're setting the address to yours then overwriting it with the posted email. Then you go on to email it to only the one address
mail($to, $subject, $message, $headers);
You need to build the addresses as such:
$to = "warranty#domain.com, {$posting['email']}";
this will have the following value:
"warranty#domain.com, other#domain.com"