Change email headers on php send mail - php

I want to change my email headers on php mail send function , but i cant do this , i try use this into my php.ini
sendmail_path = /usr/sbin/sendmail -t -i -f'user#domain.com'
but its not working , i use plesk .
then i try to change my php script and add headers Parameter into my php code [ im new at php and using someone's script on internet ] but its not working , i means the mail not sending ..
php send mail script with out any change work correctly and send email
<?php
$emailTo = "user#domain.com";//change this to the email address which should receive the form data
/*
NOTE: do not change anything below this
*/
if( isset($_POST['maximus']) ) {
//honey pot detection
if( $_POST['maximus'] != '' ) {
die('Bad spam bot!!');
}
$message = "";
foreach( $_POST as $field => $val ) {
if( $field != 'maximus' ) {
$message .= $field . " : ". $val . "\n";
}
}
$subject = $_POST['theSubject'];
//send the email
if( mail($emailTo, $subject, $message) ) {
echo "Message sent!";
} else {
echo "Could not send message";
}
}
?>
but when i add headers parameter into my code the mail not sending
<?php
$emailTo = "user#domain.com";//change this to the email address which should receive the form data
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
/*
NOTE: do not change anything below this
*/
if( isset($_POST['maximus']) ) {
//honey pot detection
if( $_POST['maximus'] != '' ) {
die('Bad spam bot!!');
}
$message = "";
// Always set content-type when sending HTML email
foreach( $_POST as $field => $val ) {
if( $field != 'maximus' ) {
$message .= $field . " : ". $val . "\n";
}
}
$subject = $_POST['theSubject'];
//send the email
if( mail($emailTo, $subject, $message, $headers) ) {
echo "Message sent!";
} else {
echo "Could not send message";
}
}
?>
html form :
<form data-toggle="validator" method="post" action="form.php" id="quickQuoteForm" class="aSubmit">
<div style="display:none"><input type="text" name="maximus" value=""></div>
<input type="hidden" name="subject" value="Blue: Quick Quote Request">
<div class="contact-form-small">
<div class="row">
<div class="col-xs-12 col-md-6">
<div class="form-group">
<input type="text" placeholder="First and Last Name *" aria-invalid="false" aria-required="true" size="40" value="" name="name" required>
</div>
<div class="form-group">
<input type="email" placeholder="E-mail address" aria-invalid="false" aria-required="true" size="40" value="" name="email" required>
</div>
<select aria-invalid="false" name="service">
<option value="Cargo">Cargo</option>
<option value="Ground Transport">Ground Transport</option>
<option value="Logistic Service">Logistic Service</option>
<option value="Storage">Storage</option>
<option value="Trucking Service">Trucking Service</option>
<option value="Warehousing">Warehousing</option>
</select>
</div>
<div class="col-xs-12 col-md-6">
<div class="form-group">
<input type="text" placeholder="Subject *" aria-invalid="false" aria-required="true" size="40" value="" name="subject" required>
</div>
<div class="form-group">
<textarea placeholder="Message *" aria-invalid="false" rows="10" cols="40" name="message" required></textarea>
</div>
</div>
<div class="col-xs-12 col-md-12">
<input type="submit" class="btn btn-primary pull-right" value="SEND MESSAGE">
<img class="ajax-loader" id="loader" src="images/ajax-loader.gif" alt="Sending ..." style="display:none;">
</div>
</div><!-- /.row -->
<div class="response success">Your message was sent; we'll be in touch shortly!</div>
<div class="response error">Unfortunately, we could not sent your message right now. Please try again.</div>
</div><!-- /.contact-form-small -->
</form>
i see this part of code NOTE: do not change anything below this but i really need to change my headers name ..

Related

Some issue in sending email using php email function

I am trying to send an email from a enquiry form available on a catalogue website on which I am working and found a strange issue.
On the same domain I have a file to test the email function with following code in it:
$sender = 'someone#somedomain.tld';
$recipient = 'name#gmail.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
}
and its working fine. But the same code is not working on the index.php page where I have the enquiry form and also not showing any error at all (I have checked the error log. I have also tried by putting this code in a separate file and by including it on index.php but the same result).
<form name="frm_enquiry" id="frm_enquiry" method="post" autocomplete="off">
<div class="row">
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label for="fullname">Name:</label>
<input type="text" class="form-control" id="fullname" name="fullname" placeholder="Enter your name">
</div>
</div>
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter your email">
</div>
</div>
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label for="mobile">Mobile No:</label>
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Enter your mobile number">
</div>
</div>
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label style="width: 100%;" for="mobile"> </label>
<button type="submit" class="btn btn-block btn-primary" name="submit">Submit</button>
</div>
</div>
</div>
</form>
php:
if( isset( $_REQUEST['submit'] ) )
{
$name = $_REQUEST['fullname'];
$email = $_REQUEST['email'];
$mobile = $_REQUEST['mobile'];
echo $name . ' : ' . $email . ' : ' . $mobile . '<br>';
$sender = 'someone#somedomain.tld';
$recipient = 'name#gmail.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
}
Someone please guide me, what I am doing wrong here, I am stuck on this from last 2 days.
Thanks in advance.

PHP X-Mailer not sending all fields

I am by no means a developer. Self-taught, but can usually wing it enough to make things happen. Working on company website for boss, and having trouble with the form. Regardless of how I try to define the PHP variable, SOME are not sending the input value to the e-mail. I'm sure I'm missing something simple, thank you in advance for your help! This is what's not showing:
Website:
Average Monthly Volume:
Preferred Contact Method:
Here is what I have so far:
HTML FORM:
<div class="product-screens2">
<div style="padding:200px">
<div class="form">
<div id="sendmessage">Your message has been sent. Thank you!</div>
<div id="errormessage">Please retry.</div>
<form action="form-email" method="post" role="form" class="contactForm">
<div class="form-row">
<div class="form-group col-lg-6">
<input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="form-group col-lg-6">
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
<div class="validation"></div>
</div>
<div class="form-group col-lg-6">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Business Name" />
</div>
<div class="form-group col-lg-6">
<input type="text" name="name" class="form-control" id="business_site" placeholder="Business Website" />
</div>
<div class="form-group col-lg-6">
<input type="text" class="form-control" name="name" id="avgvolume" placeholder="Average Monthly Volume" />
</div>
<div class="form-group col-lg-6">
<input type="text" class="form-control" name="name" id="contactmethod" placeholder="Preferred Contact Method" />
</div>
</div>
<div class="form-group">
<textarea class="form-control" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Brief Description of Goods or Services Sold"></textarea>
<div class="validation"></div>
</div>
<div class="text-center"><button type="submit" title="Send Message">SUBMIT</button></div>
</form>
</div></div>
</div>
PHP MAILER SCRIPT
<?php
/***************** Configuration *****************/
// Enter your email, where you want to receive the messages.
$contact_email_to = "Support#XXXXX.com";
// Subject prefix
$contact_subject_prefix = "Message From XXXXX Website: ";
// Name too short error text
$contact_error_name = "Name is too short or empty!";
// Email invalid error text
$contact_error_email = "Please enter a valid email!";
// Subject too short error text
$contact_error_subject = "Subject is too short or empty!";
// Message too short error text
$contact_error_message = "Too short message! Please enter something.";
/********** Do not edit from the below line ***********/
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die('Sorry Request must be Ajax POST');
}
if(isset($_POST)) {
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$subject = filter_var($_POST["subject"], FILTER_SANITIZE_STRING);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
$business_site = $_POST["business_site"];
$avgvolume = $_POST["avgvolume"];
$contactmethod = $_POST["contactmethod"];
if(strlen($name)<4){
die($contact_error_name);
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
die($contact_error_email);
}
if(strlen($message)<3){
die($contact_error_subject);
}
if(strlen($message)<3){
die($contact_error_message);
}
if(!isset($contact_email_from)) {
$contact_email_from = "contactform#" . #preg_replace('/^www\./','', $_SERVER['SERVER_NAME']);
}
$sendemail = mail($contact_email_to, $contact_subject_prefix . $subject,
"Name: $name" . PHP_EOL .
"Reply-To: $email" . PHP_EOL .
"Business: $subject" . PHP_EOL .
"Website: $business_site" . PHP_EOL .
"Average Monthly Volume: $avgvolume" . PHP_EOL .
"Preferred Contact Method: $contactmethod" . PHP_EOL .
"Business Description: $message" . PHP_EOL .
"X-Mailer: PHP/" . phpversion()
);
if( $sendemail ) {
echo 'OK';
} else {
echo 'Could not send mail! Please check your PHP mail configuration.';
}
}
?>

Sending emails via PHP server

Straight to the point. I'm trying to set up a contact form on my website but struggling to make it work. Also have read through numerous of related topics here on SO. I followed tutorial thus I've got decent familiarity with this topic. I'm sure my php.ini file is configured just fine since sending emails worked until recently (although it always ended up in junk mail). Until I made changes to the code. Changes that I needed to make sooner or later. Strangely, sending emails worked when I ran it with hard-coded details with no variables. But when I implemented variables then it stopped working. Obviously I need PHP to process user's input through variables. How can get PHP to read user's input ? I think the most important part is using the attribute name="something" in input tags.
Sharing relevant code:
PHP:
<?php
$fullName = $errorMsg = $sucMsg = "";
$errMsg = $email = $subject = $emailErr = $message = "";
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if (isset($_POST['submit'])) {
$fullName = test_input($_POST['fullName']);
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$emailErr = "Your email address is not valid !";
} else {
$email = $_POST['email'];
}
$subject = test_input($_POST['subject']);
$message = test_input($_POST['message']);
if($emailErr == "") {
$emailTo = "mark.alexa.uk#gmail.com";
$headers = "From ".$fullName." ".$email." via contact form";
$headers .= "Reply-To: ".$email;
$headers .= "Return-Path: webmaster#ubuntuserver\r\n";
$headers .= "CC: alexa.mark#mail.com\r\n";
$headers .= "BCC: alexa.mark#mail.com\r\n";
if (mail($emailTo, $subject, $message, $headers)) {
$sucMsg = "<p>The email was sent successfully !</p>";
} else {
$errMsg = "<p>The email could not be sent !</p>";
}
}
}
?>
HTML:
<div class="container" id="form">
<div id="successMsg" class="alert alert-success" role="alert"><?php
echo $sucMsg; ?></div>
<div id="errMsg" class="alert alert-danger" role="alert"><?php echo
$errMsg; ?></div>
<form method="POST" action="contact.php">
<fieldset class="form-group">
<label for="fullName">Full name</label>
<input type="text" class="form-control" name="fullName"
id="fullName" placeholder="full name" required>
</fieldset>
<fieldset class="form-group">
<label for="email">Email</label>
<input type="email" id="email" placeholder="email" class="form-
control" required>
<small id="emailHelp" name="email" class="form-text text-muted">I
won't share your email with anyone</small>
</fieldset>
<fieldset class="form-group">
<label for="subject">Subject</label>
<input type="text" class="form-control" name="subject" id="subject" placeholder="subject" required>
</fieldset>
<fieldset class="form-group">
<label for="message">Your message</label>
<input type="text" class="form-control" name="message" id="message" placeholder="message" required>
<input type="submit" id="submit" class="btn btn-success" value="Submit">
</fieldset>
</form>
</div>
UPDATE:
How should I modify the PHP code so it won't end up in junk mail ?
Add name="submit" to your submit button. Forms pass on the name attribute to the post array. So your code test for:
if (isset(`$_POST['submit']`)) {
$fullName = test_input($_POST['fullName']);
..isn't valid because $_POST['submit'] isn't set.
<input type="submit" id="submit" name="submit" class="btn btn-success" value="Submit">

PHP contact form wont send message [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I can't seem to get the form to send a message to my email. Is it maybe due to the server? Here's my code:
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form id="contact-form" action:"" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="hidden" name="action" value="submit">
<label for="name">
Name</label>
<input type="text" class="form-control" id="name" placeholder="Enter name" required />
</div>
<div class="form-group">
<label for="email">
Email Address</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span>
</span>
<input type="email" class="form-control" id="email" placeholder="Enter email" required /></div>
</div>
<div class="form-group">
<label for="subject">
Subject</label>
<select id="subject" name="subject" class="form-control" required="required">
<option value="na" selected="">Choose One:</option>
<option value="General">General</option>
<option value="Hiring">Hiring</option>
<option value="My Work">My Work</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="message">
Message</label>
<textarea name="message" id="message" class="form-control" rows="9" cols="25" required
placeholder="Message"></textarea>
</div>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-skin pull-right" id="btnContactUs">
Send Message</button>
</div>
</div>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$subject=$_REQUEST['subject'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($subject=="")||($message==""))
{
echo "All fields are required.";
}
else{
$from="From: $name, $email";
$subject="Message sent using your contact form";
mail("nilsbittmannmyers#yahoo.co.uk", $subject, $message, $from);
echo "Message sent!";
}
}
?>
THANKS :)
Im using byet as a web host by the way, I think they have PHP enabled in the free service, excuse me, I'm a bit of a noob when it comes to coding, to say the least XD XD
You're using a colon action:"" instead of an equal sign which should be action="" which is the main issue in your code.
You're also missing a name attribute for the email and name input fields.
I.e.: name="email" and name="name"
Also, the From: should initially be an email, and not a name as you have in
$from="From: $name, $email";
It's best to use something like this:
$from = "From: ". $name . " <" . $email . ">\r\n";
as your header's 4th parameter.
That way you will have a proper From Email with the person's name appearing in the Email also.
Using $from="From: $name, $email"; will most likely end up in Spam or rejected altogether.
Consult the PHP.net Website for details on mail() and headers:
http://php.net/manual/en/function.mail.php
a few headers options:
$headers .= 'Reply-To: ' . $from . "\r\n";
$headers .= 'Return-Path: ' . $from . "\r\n";
in your case would be:
$from = "From: ". $name . " <" . $email . ">\r\n";
$from .= 'Reply-To: ' . $email . "\r\n";
$from .= 'Return-Path: ' . $email . "\r\n";
Sidenote:
I noticed you are using enctype="multipart/form-data"
Unless you're wanting to attach/upload a file, it isn't required for what you're using your form as, it's safe to remove it.
Footnotes:
Using error reporting would have signaled these errors, including Undefined index... warnings:
error_reporting(E_ALL);
ini_set('display_errors', 1);
placed just beneath your opening <?php tag.

PHP files open while clicking on send

this is my code in HTML5
whenever i am clicking on send button ...
phpfiles open rather mailing on that mail id which I mentioned can anybody help me out please ???
<div class="col-md-6">
<div class="alert alert-success hidden" id="contactSuccess">
<strong>Success!</strong> Your message has been sent to us.</div>
<div class="alert alert-danger hidden" id="contactError">
<strong>Error!</strong> There was an error sending your message.</div>
<h2 class="short">
<strong>Contact</strong> Us</h2>
<form action="php/contact-form.php" id="contactForm" type="post">
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Your name *</label>
<input type="text" value="" data-msg-required="Please enter your name." maxlength="100" class="form-control"
name="name" id="name" /></div>
<div class="col-md-6">
<label>Your email address *</label>
<input type="email" value="" data-msg-required="Please enter your email address."
data-msg-email="Please enter a valid email address." maxlength="100" class="form-control" name="email"
id="email" /></div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Subject</label>
<input type="text" value="" data-msg-required="Please enter the subject." maxlength="100" class="form-control"
name="subject" id="subject" /></div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Message *</label>
<textarea maxlength="5000" data-msg-required="Please enter your message." rows="10" class="form-control" name="message"
id="message"></textarea></div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" value="Send Message" class="btn btn-primary btn-lg" data-loading-text="Loading..." />
</div>
</div>
</form>
</div>
======================================================
and dis are my codes in php file...
<?php
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');
// Enter your email address below.
$to = 'info#webppulse.com';
$subject = $_POST['subject'];
if($to) {
$name = $_POST['name'];
$email = $_POST['email'];
$fields = array(
0 => array(
'text' => 'Name',
'val' => $_POST['name']
),
1 => array(
'text' => 'Email address',
'val' => $_POST['email']
),
2 => array(
'text' => 'Message',
'val' => $_POST['message']
)
);
$message = "";
foreach($fields as $field) {
$message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}
$headers = '';
$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=UTF-8\r\n";
if (mail($to, $subject, $message, $headers)){
$arrResult = array ('response'=>'success');
} else{
$arrResult = array ('response'=>'error');
}
echo json_encode($arrResult);
} else {
$arrResult = array ('response'=>'error');
echo json_encode($arrResult);
}
?>
whenever i am clicking on send button ...
phpfiles open rather mailing on that mail id can someone help me out please ?
Change the following code
<form action="php/contact-form.php" id="contactForm" type="post">
with
<form action="php/contact-form.php" id="contactForm" method="post">
You Must have install localhost server Like xampp or wampp.
Then put files in
For Xampp : htdocx/ Put your file in htdocx folder and run.
Foe wampp: www/ put your file in www folder and run.
Turn your type="post" into a method="post". Also, you need to have your project placed into the htdocs folder of your WAMP directory and ensure that the WAMP server has been turned on.
You should always try a "Hello World" test first to ensure that everything is running correctly. Check out tutorials on the Web for this.

Categories