I have a PHP script to send emails with this code- - php

I have this PHP code to send emails.
<?php
if (isset($_POST['ajax'])) {
$to = $_POST['to'];
$subject = $_POST['sub'];
$msg = $_POST['msg'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: ".$_POST['name']."<".$_POST['from'].">";
$send = mail($to,$subject,$msg,$headers);
if ($send) {
echo "<p id='success'>✔️ $to</p>";
}else{
echo "<p id='error'>❌ $to</p>";
}
exit();
}
?>
I want to integrate it with the following form.
<form>
<input id="input-1" type="text" placeholder="John Doe" required autofocus />
<label for="input-1">
<span class="label-text">Full Name</span>
<span class="nav-dot"></span>
<div class="signup-button-trigger">Sign Up</div>
</label>
<input id="input-2" type="text" placeholder="john" required />
<label for="input-2">
<span class="label-text">Username</span>
<span class="nav-dot"></span>
</label>
<input id="input-3" type="email" placeholder="email#address.com" required />
<label for="input-3">
<span class="label-text">Email</span>
<span class="nav-dot"></span>
</label>
<input id="input-4" type="text" placeholder="●●●●●●" required />
<label for="input-4">
<span class="label-text">Password</span>
<span class="nav-dot"></span>
</label>
<input id="input-5" type="text" placeholder="●●●●●●" required />
<label for="input-5">
<span class="label-text">Confirm Password</span>
<span class="nav-dot"></span>
</label>
<button type="submit">Create Your Account</button>
<p class="tip">Press Tab</p>
<div class="signup-button">Sign Up</div>
</form>
Is there a way to make it work. I want to use the PHP GET method. Is this possible? How would I get around doing that?
I want to send a PHP GET request, which would then send the email. Do I have to change anything in the form? In the PHP?

Related

My form submission not working [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
HTML Codes
<h3>online consultation</h3>
<h5>We have provided contact information down below. You can contact us either by mobile, via email or via contact form. We will at once contact you to discuss your problems! If you have special requirments a consultation can be decided.</h5>
</div>
<div class="col-md-7 col-lg-offset-3 contact-grid-right">
<form method="post" action="mail.php">
<input type="text" placeholder="Your Name" name="name" id="name" required="required">
<input type="text" placeholder="Email" name="Email" id="Email" required="required">
<input type="text" placeholder="Contact No" name="contact" id="contact" required="required">
<input type="text" placeholder="Subject" name="subject" id="subject" required="required">
<textarea rows="2" cols="70" type="text" placeholder="MESSAGE" name="message" id="message" required="required">
</textarea>
<button type="submit" class="slide-btn"> Send your message <i class="fa fa-paper-plane" aria-hidden="true"></i> <br></button>
</form>
</div>
PHP Script
<?php
$n=$_POST['name'];
$mail=$_POST['email'];
$ph=$_POST['contact'];
$ln=$_POST['subject'];
$msg=$_POST['message'];
$to="email#yahoo.com";
$sub="message";
$body="
<html>
<body>
<p><b></b></p><h4></h4>
<table border='0'>
<tr>
<b>Name : </b> $n <br><br>
<b>Email : </b> $mail <br><br>
<b>Contact :</b> $ph <br><br>
<b>Subject :</b> $ln <br><br>
<hr>
<b>Contact Form Message:</b> <br><br>$msg
</tr>
</table>
</body>
</html>";
$headers = "MIME-Version: 1.0". "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8". "\r\n";
$headers .= 'From:'.$mail . "\r\n";
//$headers .= 'Cc: email#yahoo.com.au'; "\r\n";
//$headers .= 'Bcc: '; "\r\n";
mail($to,$sub,$body,$headers);
//echo "<script>alert('Message Send Sucessfully')</script>";
echo "<script>window.location.href='index.html'</script>";
?>
here the solution change the $mail=$_POST['email']; in your php script to $mail=$_POST['Email'];
For keeping your code standard replace the email html input for:
<input type="text" placeholder="Email" name="email" id="email" required="required">

Add Form Data on PHP mail script

i am using PHP form with File attachment. Due to low knowledge in PHP i use this script from a online tutorial.
My problem is:
I want to get all user submitted form info by mail but don't know how to add code on this script(check last code section).
I want user can only send only .txt, .doc, .docx file format on file attachment.
HTML Code:
<form name="sendmail" action="form.php" method="post" enctype="multipart/form-data">
<ul>
<li>
<span class="left">
<label for="name">First Name:</label>
<input type="text" size="40" id="f_name" />
</span>
<span class="left" style="position:relative;left:30px;">
<label for="name">Middle Name:</label>
<input type="text" size="40" id="m_name" />
</span>
<span class="right">
<label for="name">Last Name:</label>
<input type="text" size="40" id="l_name" />
</span>
</li>
<li>
<span class="left">
<label for="email">Email Address:</label>
<input type="email" size="40" id="email" style="width:250px;" />
</span>
<span class="right">
<label for="email" style="width:200px;">Confirm Email Address:</label>
<input type="email" size="40" id="c_email" style="width:250px;" />
</span>
</li>
<li>
<span class="left">
<label for="name">Primary Phone: </label>
<input type="text" size="40" id="p_phone" style="width:250px;" />
</span>
<span class="right">
<label for="name" style="width:200px;">Secondary Phone:</label>
<input type="text" size="40" id="s_phone" style="width:250px;" />
</span>
</li>
<li>
<span class="left">
<label for="name">Mailing Address:</label>
<input type="text" size="40" id="m_address" style="width:250px;" />
</span>
<span class="right">
<label for="name" style="width:200px;">Mailing City:</label>
<input type="text" size="40" id="m_city" style="width:250px;" />
</span>
</li>
<li>
<span class="left">
<label for="name">State/Province:</label>
<input type="text" size="30" id="state_province" />
</span>
<span class="left" style="position:relative;left:30px;">
<label for="name">Zip/Postal Code:</label>
<input type="text" size="30" id="zip_postal" />
</span>
<span class="right">
<label for="name">Country:</label>
<input type="text" size="30" id="country" />
</span>
</li>
<li>
<span class="left">
<label for="name">Job Information:</label>
<input type="text" size="40" id="job_info" style="width:250px;" />
</span>
<span class="right">
<label for="name" style="width:200px;">Desired Job Title: </label>
<input type="text" size="40" id="job_title" style="width:250px;" />
</span>
</li>
<li>
<span class="left">
<label for="name" style="width:200px;">Desired Geographic Region:</label>
<input type="text" size="40" id="job_info" style="width:200px;" />
</span>
<span class="right">
<label for="name" style="width:200px;">Years of Related Experience:</label>
<input type="text" size="40" id="job_title" style="width:250px;" />
</span>
</li>
<li>
<span class="left">
<label>Resume:</label>
<input type="file" name="resume" />
</span>
<span class="right">
<label style="width:180px;">Cover Letter:</label>
<input type="file" name="cover_letter" />
</span>
</li>
</ul>
<p>
<button type="submit" class="action" name="submit">Submit</button>
</p>
form.php code-
<?php
$from = "info#arif-khan.net";
$to = "arifkpi#gmail.com";
$subject ="JobSeeker Registration Request";
$message = $_POST['body'];
// Temporary paths of selected files
$file1 = $_FILES['resume']['tmp_name'];
$file2 = $_FILES['cover_letter']['tmp_name'];
// File names of selected files
$filename1 = $_FILES['resume']['name'];
$filename2 = $_FILES['cover_letter']['name'];
// array of filenames to be as attachments
$files = array($file1, $file2);
$filenames = array($filename1, $filename2);
// include the from email in the headers
$headers = "From: $from";
// boundary
$time = md5(time());
$boundary = "==Multipart_Boundary_x{$time}x";
// headers used for send attachment with email
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\"";
// multipart boundary
$message = "--{$boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$boundary}\n";
// attach the attachments to the message
for($x=0; $x<count($files); $x++){
$file = fopen($files[$x],"r");
$content = fread($file,filesize($files[$x]));
fclose($file);
$content = chunk_split(base64_encode($content));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$filenames[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $content . "\n\n";
$message .= "--{$boundary}\n";
}
// sending mail
$sendmail = mail($to, $subject, $message, $headers);
// verify if mail is sent or not
if ($sendmail) {
header("location:index.html");
}
?>
Field code that i want to add on email body-
First Name: $_POST[f_name]
Middle Name: $_POST[m_name]
Last Name: $_POST[l_name]
Email Address: $_POST[email]
Primary Phone: $_POST[p_phone]
Secondary Phone: $_POST[s_phone]
Mailing Address: $_POST[m_address]
Mailing City: $_POST[m_city]
State/Province: $_POST[state_province]
Zip/Postal Code: $_POST[zip_postal]
Country: $_POST[country]
Job Information: $_POST[job_info]
Desired Job Title: $_POST[job_title]
Desired Geographic Region: $_POST[desired_region]
Years of Related Experience: $_POST[year_of_experience]
To add the captured data to the sendmail script:
name your variable correctly (that is no space in between), for example,
First Name: $_POST[f_name]
Middle Name: $_POST[m_name]
Last Name: $_POST[l_name]
Should be (remember the '$' sign to tell php it is a variable:
$First_Name: $_POST['f_name'];
$Middle_Name: $_POST['m_name'];
$Last_Name: $_POST['l_name'];
Do the same for all, and place this code before this script:
// Temporary paths of selected files
$file1 = $_FILES['resume']['tmp_name'];
$file2 = $_FILES['cover_letter']['tmp_name'];
In your code where you have:
// multipart boundary
$message = "--{$boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
$message .= "--{$boundary}\n";
Add this:
$message .= "First name: ".clean_string($First_Name)."\r\n";
$message .= "Last name: ".clean_string($Last_Name)."\r\n";
Do this for all the variable you want to add from (1) above, and your script should now work fine.
Your inputs are missing the name attribute. For PHP to capture form values you need to add the name attribute to the inputs and then capture the input values by referring to these names. For example:
You are capturing the First Name from the from like so:
First Name: $_POST[f_name]
So get the name your input should have a name attribute equal to f_name like so:
<input type="text"name="f_name"size="40" id="job_title" style="width:250px;" />

Php mail not sending

I'm trying to send an email using php but it's not working..
This is my code:
<form method="post" name="contact" action="#contact">
<div class="left">
<label for="author">Name:</label> <input name="nom" type="text" class="input_field" id="author" maxlength="40" />
</div>
<div class="right">
<label for="email">Email:</label> <input name="email" type="text" class="input_field" id="email" maxlength="40" />
</div>
<div class="clear"></div>
<label for="text">Message:</label> <textarea id="text" name="text" rows="0" cols="0"></textarea>
<input type="submit" class="submit_btn float_l" name="submit" id="submit" value="Send" />
</form>
<?php
$name= ($_POST["nom"]);
$mail= ($_POST["email"]);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:<nelson-book#outlook.fr>' . "\r\n";
$suject="Book";
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .$_POST["text"];
if (isset($_POST['submit'])) {
mail("nelson-book#outlook.fr", $suject, $message, $headers);
echo" teste";
}
?>
I already used a code SUPER similar to this ant it totally worked.. Can the problem be from the server?
thanks in advance.
check out your SMTP server restriction when you send email, with another hosting domain like, "outlook.fr", somethime this is't allowed.

Simple Php Mailer.php and a contact.html, need html help might be missing few tags

Hi im totally new to php, i have a website where i have a contact.html,it has forms where in name, email, insurance type, subject and comments can be filled.
Up on clicking submit button, the specfied email will recieve the details. Cant seem to locate the problem.
here is the html code of contact.html
<form id="ajax-contact-form">
<div class="row">
<div class="span4">
<div class="form-padding">
<label for="f1">Your name(required):</label>
<input type="text" name="name" id="f1" class="form-text" value=""/>
<label for="f2">Insurance type:</label>
<input type="text" name="category" id="f2" class="form-text" value="" size="40"/>
</div>
</div>
<div class="span4">
<div class="form-padding">
<label for="f3">Your email(required):</label>
<input type="text" name="email" id="f3" class="form-text" value="" size="40"/>
<label for="f4">Subject:</label>
<input type="text" name="subject" id="f4" class="form-text" value="" size="40"/>
</div>
</div>
</div>
<div class="row">
<div class="span8">
<div class="form-padding">
<label for="f5">Message(required):</label>
<textarea name="message" id="f5" cols="40" rows="10"></textarea>
</div>
</div>
</div>
<!-- send mail configuration -->
<input type="hidden" value="myemail#gmail.com" name="to" id="to" />
<input type="hidden" value="myemail#gmail.com" name="from" id="from" />
<input type="hidden" value="From contact form" name="subject" id="subject" />
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
<!-- ENDS send mail configuration -->
<p><input type="button" class="button red small" value="Submit Form" name="submit" id="submit" /></p>
</form>
AND MY SEND-MAIL.PHP
<?php
//vars
$subject = $_POST['subject'];
$to = explode(',', $_POST['to'] );
$from = $_POST['email'];
//data
$msg = "NAME: " .$_POST['f1'] ."<br>\n";
$msg .= "INSURANCE: " .$_POST['f2'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['f3'] ."<br>\n";
$msg .= "SUBJECT: " .$_POST['f4'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['f5'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
//send for each mail
foreach($to as $mail){
mail($mail, $subject, $msg, $headers);
}
?>
My JS function
// contact form
$("#ajax-contact-form").submit(function() {
var str = $(this).serialize();
$.ajax({
type: "POST",
url: "send-mail.php",
data: str,
success: function(msg) {
if(msg == 1) {
result = '<div class="alert success fade in">Your message has been sent. Thank you!</div>';
$("#ajax-contact-form").hide();
} else {result = msg;}
$('#form-message').hide();
$('#form-message').html(result);
$('#form-message').fadeIn("slow");
}
});
return false;
});
My Questions
There seems to be an error, cant send mails. Im a naive. Any help would be appreciated.
How to do i show a success message after clicking submit button.
Thanks
maybe you have to complete your form open tag
<form id="ajax-contact-form" method="POST" action="send-mail.php">
unless you using Ajax, you have to provide the ajax code in your question.

Mail is not coming properly using php?

I am using below simple code to send mail using php. when i echo the line $headers = "From: . $email\r\n"; //echo $headers; exit; which results like From: . test#email.com.
I found dot is printing before mail address , so mail is coming but not properly.If i remove dot , mail is not even coming.Whats wrong with this code ?
<?php
if (isset($_REQUEST['user_email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['user_email'];
$user_name = $_REQUEST['user_name'];
$user_message = $_REQUEST['user_message'];
include 'contact_mail_form.php';
$to = "testtest#gmail.com";
$message = $contactText;
$subject = "Test Mail";
$headers = "From: . $email\r\n"; //echo $headers; exit;
$headers .= "Content-type: text/html\r\n";
mail($to,$subject,$message,$headers);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
?>
<form id="contact_form" name="ContactForm" action="" method="post" onsubmit="return checkContact(this);">
<div class="wrapper">
<div class="fright">
<div class="name">Message:</div>
<textarea cols="1" rows="1" name="user_message" id="user_message" ></textarea>
<span class="clear"></span> </div>
<div class="fleft">
<div>
<div class="name">Your Name:</div>
<input type="text" class="input" name="user_name" id="user_name" />
<span class="clear"></span> </div>
<div>
<div class="name">E-mail:</div>
<input type="text" class="input" name="user_email" id="user_email" />
<span class="clear"></span> </div>
</div>
</div>
<div class="buttons"><a class="more" href="javaScript:void(0);" onClick="document.getElementById('contact_form').reset()">clear</a> <input class="more1" type="SUBMIT" class="button2" value="Send" name="" /> </div>
</form>
<?php } ?>
Try it like this your dot is acting as string not as concatenation
$headers = "From:" . $email."\r\n";
Maybe because \r\n should be surrounded with " not '
And Also
$headers .= 'Content-type: text/html'. "\r\n";

Categories