I am trying to use phpmailer to send a user's input from an html form
Form is like this
<form action="senditpm.php" role="form">
<input type="text" class="form-control" id="name" name="name"><br>
<input type="email" class="form-control" id="email" name="email">>br>
<input type="text" class="form-control" id="phone" name="phone">>br>
<input type="submit" value="Submit" class="submit-button btn btn-default">
</form>
senditpm.php is like this
<?php
$name = $_POST['name'];
$phone = $_POST['phone'];
$email= $_POST['email'] ;
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'xxxxxxxxx';
$mail->SMTPAuth = false;
$mail->Username = 'xxxxxxxx';
$mail->Password = 'xxxxxxxx';
$mail->Port = 25;
$mail->setFrom('xxx#xxxxs.co.uk', 'xxxxx');
$mail->addAddress('xxx#xxx', 'xxx xxxx');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body="
Name: $name <br>
Email: $email <br>
Phone: $phone <br>";
;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
When I complete the form and click Submit, I receive an email like this
Name:
Email:
Phone:
As you can see, the php isn't sending the variables
Please could someone let me know where I am going wrong. Thanks in advance
<form> defaults to a GET method if a POST method is not implied and you are using POST arrays.
So change
<form action="senditpm.php" role="form">
to
<form action="senditpm.php" role="form" method="post">
Related
How can I send an email to multiple email addresses? Right now this index.php page is being hosted online, and it connects to a send.php page, and I'd like to add another email text field that would be used to also send an email to the address entered in it.
Currently it sends an email to one email address that is entered in the email field in the form.
Index.php
<form id="contactus" method="post" action="send.php">
<input type="name" name="name" class="form-control" placeholder="Name" required>. <br/><br/>
<input type="email" name="email" class="form-control" placeholder="Email" required>. <br/><br/>
<input type="name" name="name2" class="form-control" placeholder="Player 2's Email Address" required><br/><br/>
<textarea name="message" class="form-control" placeholder="What Is Your Question?" required></textarea><br/><br/>
<input type="hidden" name="phone2">
<button type="submit" class="btn btn-success">Send Email</button>
<br><br>
</form>
Send.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING); //sanitize data
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
/*added line of code*/
if(empty($name)){
header("location: index.php?nouser");
exit();
}
if(empty($email)){
header("location: index.php?noemail");
exit();
}
if(empty($message)){
header("location: index.php?noemail");
exit();
}
/*if(empty($message)){
header("location: index.php?nomessage");
exit();
}//end validation*/
//added line; 'honeypot'
if(empty($_POST['phone2'])){
//Information that needs to be filled out to be able to send an email through phpmailer
//Will use an SMTP service. SMTP is basically like a mail server to send mail
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 1;
//$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = false;
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = "25";
$mail->Username = "#Email_Placeholder";
$mail->Password = "Password";
$mail->SetFrom("#Email_Placeholder");
$mail->setFrom('#Email_Placeholder', 'Greeting');
// Add a recipient : used to also send to name
$mail->addAddress($email);
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('email#gmail.com');
/*$mail->addBCC('bcc#example.com');*/
//Body content
$body = "<p><strong>Hello, </strong>" . $email . " How are you?
<br>
<b> Please Notice: </b><br>
<br> <br> Your message was Delivered: " . $message . ". Hello" . $name2 .
"</p>";
$mail->isHTML(true);
//subject line of email
$mail->Subject = $name;
//for html email
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
//the email gets sent
header("Location: http://www.test-domain.com");
$mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
?>
This is pretty simple - if you follow the code, you just need to duplicate some lines.
Duplicate your form field
<input type="email" name="email" class="form-control" placeholder="Email" required>
<input type="email" name="email_2" class="form-control" placeholder="Email" required>
Duplicate your validation
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$email_2 = filter_var($_POST['email_2'], FILTER_SANITIZE_STRING);
Duplicate the 'Add Address'
$mail->addAddress($email);
$mail->addAddress($email_2);
Fore readability sake in the code use an array and implode it to a comma separated string:-
$recipients = array(
"youremailaddress#yourdomain.com",
// more emails
);
$email_to = implode(',', $recipients); // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page
<div class="contact_form clearfix" id="Contact">
<h2>Hello... You can send me message to my universe here.</h2>
<img src="img/planeta1.png" alt="">
<form class="clearfix spaceForm" action="contactform.php" metod="post" >
<label for="name">Your name:</label>
<input type="text" name="name" id="name" placeholder="Jon Doe" required>
<label for="email">Your email:</label>
<input type="text" name="email" id="email" placeholder="something#mama.com" required>
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject" required>
<label for="message">Your message:</label>
<textarea name="message" id="message" required></textarea>
<button type="submit" name="submit">Send mail</button>
</div>
and php code here...
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mailTo = "pisitenam#sammostalnisindikatstark.org.rs";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.html");
}
?>
My contact form instead to send message download on computer as php file. I uploaded my site to netfly but stil doesnt work.
Can anybody help and give me a hint where is problem?
On XAMPP im getting blank page and mail is not sent. When I uploaded site on netfly site works fine but contact from when click submit start download php file where is code writen for controling contact form.5 day im trying to find solution for this problem and im geting tired :D So if anybody can help...
you are having some spell mistake in your form tag, first of all correct the method spell in your code as it is not correct so it can't redirect and post your data to contact form.
mail library contains various function.
for example:
<?php require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'your_smtp_domain.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('john#example.net', 'John doe'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
this can works for you if you use library.
i am trying to send a random number to an email but unable to this is the code that i have been able to come up with so far
please help if possible
sendmail.php
$to = $_POST['email'];
$header ='From:admin#domain.com';
$subject ='Verification Code';
if(empty($to))
{
echo "Email is Empty";
}
else
{
if (mail($to,$subject,$message,$header)){
echo 'Check Your Email FOr verfication code';
}
else{
echo 'Failed';
}
}
index.php
<form action = "register.php" method="POST">
<p>Username</p>
<input type="text" name="username" maxlength="40" value='<?php if(isset($username)) echo $username ?>'"><br>
<p>New Password</p>
<input type="password" name="password">
<p>email</p>
<input type="text" name="email" maxlength="40" value='<?php if(isset($email)) echo $email ?>'"> <br><br>
<input type="submit" value="Register"> <br><br>
</form>
In form(index.php)
<form action="sendmail.php" method="post">
<lable>email</lable>>
<input type="text" name="email" maxlength="40">
<br>
<input type="submit" value="Register">
</form>
In sendmail.php
$rand= rand(10, 20)// random number generator
$to = $_POST['email'];
$header ='From:admin#domain.com';
$subject ='Verification Code';
$message = "Your Random number is";
$message .= $rand;
$message .= "Thank you-Admin";
if(empty($to))
{
echo "Email is Empty";
}
else
{
if (mail($to,$subject,$message,$header)){
echo 'Check Your Email FOr verfication code';
}
else{
echo 'Failed';
}
}
You forgot to add semicolon at the end of first line
$rand= mt_rand(100000, 999999)
It should be
$rand= mt_rand(100000, 999999);
I think the hosting service that You're using does not accept sending using mail() functions. Maybe for security reasons to prevent virus-like php scripts to send spams from Your account.
So create admin#domain.com email account and using PHPMailer send Your email:
require __DIR__.'/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.hostname.com';
$mail->SMTPAuth = true;
$mail->Username = 'no-reply#domain.com';
$mail->Password = 'secret';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('admin#domain.com');
$mail->addAddress($to);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
download link and examples here: https://github.com/PHPMailer/PHPMailer
i have this form:
<form action="/sendemail.php" id="main-contact-form" method="post" name="contact-form" role="form">
<div class="form-group">
<input class="form-control" id="name" name="name" placeholder="put your name" required="" type="text" />
</div>
<div class="form-group">
<input class="form-control" id="email" name="email" placeholder="Email" required="" type="email" />
</div>
<div class="form-group">
<input class="form-control" id="subject" name="subject" placeholder="Subject..." required="" type="text" />
</div>
<div class="form-group">
<textarea class="form-control" name="message" placeholder="Mensaje" required="" rows="8"></textarea>
</div>
<input class="btn btn-primary" id="submit" name="submit" type="submit" value="Enviar" />
and this is sendemail.php:
<?php
$pos = $_POST;
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#gmail.com ';
$subject = 'contact message ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message\n $pos";
mail ($to, $subject, $body, $from)
?>
the email is arrive empty, i try a lot of stuffs but always comes in the same way, is like the form canĀ“t pass the data from inputs to php variables. im new in php so, any help is welcome.
thanks.
The use of mail() function is so insecure and is a vector attack to send massive spam, don't use anymore, for this reason is better to use PHP Mailer libray.
Create a EMAIL account (example: no-reply#domain.com) in your hosting and after that install this library PHP Mailer, now you can do this:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
UPDATE
After some research I've found that:
You need to ask godaddy technical support to enable "sendmail".
PHP mail() not working on GoDaddy
----------
I've tested your code and it works. I've received the email with ALL the $_POST contents without any problems.
$pos = $_POST;
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#gmail.com ';
$subject = 'contact message ';
NOTE:
1 - $pos = $_POST; is an array and it will be displayed as Array.
2 - Emails sent via mail() will most likely end-up on the spam folder (read the comments below).
I am brand new to phpmailer.. i have made some progress by creating a simple form that allows users to send emails to our inbox:
<form id="contact-form" class="contact-form" method="post" action="send-mail.php">
<input type="text" name="name" id="name" placeholder="Name" tabindex="1">
<input type="text" name="subject" id="trade" placeholder="Your Trade" tabindex="3">
<input type="text" name="email" id="email" placeholder="Your Email" tabindex="2">
<input type="text" name="number" id="number" placeholder="Contact Number" tabindex="4">
<textarea id="message" rows="8" name="message" placeholder="Message" tabindex="5"></textarea>
<div class="grid-col one-whole">
<button type="submit">Send Your Message</button>
</div>
</form>
.php:
<?php
require('PHPFiles/PHPMailerAutoload.php');
require('PHPFiles/class.smtp.php');
require('PHPFiles/class.phpmailer.php');
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;
$mail->IsSMTP();
$mail->Host = '74.208.5.2';
$mail->SMTPAuth = true;
$mail->Username = 'info#mydomain.com';
$mail->Password = '*******';
$mail->SMTPSecure = 'tls';
$mail->Port = 25; connect to
$mail->From = isset($_POST["email"]) ? $_POST["email"] : "";
$mail->FromName = isset($_POST["email"]) ? $_POST["email"] : "";
$mail->addAddress('info#mydomain.com');
$mail->Subject = isset($_POST["subject"]) ? $_POST["subject"] : "";
$mail->Body = str_replace("\n",'<br>', $_POST['message']);
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->isHTML(true);
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
At the moment, the Body of the email contains the contents of the textare field. How can the body contain this, but then also a line break, then, the contents of the number field?
You might want to try this. Make a separate variable for all the data that needs to be in the body of the message.
$message = "Name :- " . $_POST['name'] . "<br>" . " Number :- " . $_POST['number'];
$mail->Body = $message;
so doing it like:
$mail->Body = str_replace('\n','<br />', $_POST['message']);
or:
$mail->Body = str_replace("\\n","<br />", $_POST["message"]);
should fix the problem of string getting the "line breaks".
btw. it`s always good to:
var_dump($data);
die();
to see what are you getting at each step of a code