PHPMailer '$mail->From = $_POST['email'] not working - php

I have set up a contact form on my website that people can use to email me. The form allows them to leave their email behind so I can get back to them somehow. However for some reason no matter what email is left, the system seems to always think that I am the one who sent the email to my own email address, I am guessing this is due to the $mail->Username line of code being me? Although the from should set this, it seems it won't even if I enter the from address directly into the code.
This is the form I am using on my site to submit the form itself.
<form id="contact-form" name="contact-form" method="post" action="contact-form.php" enctype="application/x-www-form-urlencoded">
<input type="text" placeholder="Name" name="name" required="true">
<i class="fa fa-envelope"></i><input type="email" placeholder="Email" name="email" required="true">
<input type="text" placeholder="Subject" name="subject" required="true">
<textarea placeholder="Message" name="message" rows="7" required="true"></textarea>
<button type="submit" name="submit" value="submit">Send</button>
<button type="reset" value="reset">Reset</button>
</form>
The code for the php to be executed when the form is submitted is this.
<?php
require 'assets/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.live.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myemail#hotmail.co.uk'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = $_POST['email'];
$mail->addAddress('myemail#hotmail.co.uk', $_POST['name']); // Add a recipient
$mail->addReplyTo('info#example.com', 'Information');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $_POST['subject'];
$mail->Body = $_POST['message'];
$mail->AltBody = $_POST['message'];
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
Any help would be much appreciated, thank you.

Some systems just don't allow you to use a from address that's anything other than your own as an anti-forgery measure - because make no mistake, what you're trying to do is forge their address! It's also likely to run into problems with anyone that uses SPF, so you really should not try to do this.
Gmail will allow you to set specific aliases, or use your own domain.
reply-to is probably the most practical solution to this, or just provide a mailto link in the message body.

I think you don't have to "pretend" $mail->From = $_POST['email']; that someone have send you an email because as you can see it wont work. If you are trying to send email from someone to yourself just to have ability to push "reply to" button and write to this person use:
$mail->AddReplyTo($_POST['email']);
Email will be send from your mailbox to your mailbox but you will respond to email posted by the user.

Related

Php form instead send mail download on computer

<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.

PHPMailer not sending e-mail through Gmail, returns blank page

I am trying to set up a contact form at the bottom of my page. I am using PHPMailer and trying to receive emails at my gmail account. Every time I submit the form, my url changes to url/email.php and I get a blank page. What am I doing wrong? Can anyone see the glaring error that I am obviously missing?
HTML:
<div class="container-fluid">
<form action="email.php" id="contact-me" method="post" name="contact-me">
<div class="row-fluid">
<div class="form-group">
<div class="col-md-3 col-md-offset-3">
<input class="input" name="name" id="name" placeholder="Name" type="text">
</div>
<div class="col-md-3">
<input class="input" id="email" name="email"
placeholder="Email" type="text">
</div>
</div>
</div>
<div class="form-group">
<textarea class="input input-textarea" id="message" name="message" placeholder="Type your message here" rows=
"5"></textarea>
</div>
<input name="send" class="send" type="submit" value="Get In Touch">
</form>
</div>
</section>
PHP:
<?php
if (isset($_POST['submit']))
{
require('PHPMailer-master/PHPMailerAutoload.php');
require_once('PHPMailer-master/class.smtp.php');
include_once('PHPMailer-master/class.phpmail.php');
$name = strip_tags($_Post['name']);
$email = strip_tags($_POST['email']);
$msg = strip_tags($_POST['message']);
$subject = "Message from Portfolio";
$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'me#gmail.com';
$mail->Password = '***********';
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress("me.com", "Katia Sittmann");
$mail->AddReplyTo($email, $name);
$mail->isHTML(true);
$mail->WordWrap = 50;
$mail->Subject =$subject;
$mail->Body = $msg;
$mail->AltBody ="No message entered";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}else{
header("Location: thank-you.html");
}
}
?>
You've got some very confused code here. You should start out with an up-to-date example, not an old one, and make sure you're using the latest PHPMailer (at least 5.2.10).
Don't do this:
require('PHPMailer-master/PHPMailerAutoload.php');
require_once('PHPMailer-master/class.smtp.php');
include_once('PHPMailer-master/class.phpmail.php');
All you need is this, which will auto-load the other classes if and when they are needed:
require 'PHPMailer-master/PHPMailerAutoload.php';
This won't work:
$name = strip_tags($_Post['name']);
PHP variables are case-sensitive, so do this:
$name = strip_tags($_POST['name']);
This will cause problems:
$mail->From = $email;
When you do this, you're forging the From address, and it will get rejected by SPF checks. Put your own address in here, and let the reply-to carry the submitter's address, as you're already doing.
Submit may not be included in the submission if the form is not submitted via that control, e.g. if it's submitted by hitting return in a text input. Check for a field that you know will always be in a submission:
if (array_key_exists('email', $_POST)) {...
Adding a try/catch will not achieve anything - PHPMailer does not throw exceptions unless you ask it to, and you are not.
You are applying strip_tags on the body, but then calling $mail->isHTML(true); - do you want HTML or not?
All that said, none of those things will cause a blank page. It's fairly likely you are running into gmail auth problems which are popular lately, and to see that you need to enable debug output:
$mail->SMTPDebug = 2;
and then read the troubleshooting guide on what you see.

How to disable PHPMailer redirect

I have mail form:
<div class="contact">
<form id="mailer-form" action="./plugins/mailer/gmail.php" method="post" name="message">
<input type="text" name="name" /> <span>Jméno <strong>*</strong></span>
<div class="clear"></div>
<input type="text" name="email" /> <span>Email <strong>*</strong></span>
<div class="clear"></div>
<input type="text" name="subject" /> <span>Předmět</span>
<input type="hidden" name="frompage" value="yes" />
<div class="clear"></div>
<span>Zpráva <strong> * </strong></span>
<div class="clear"></div>
<textarea name="message" id="message"></textarea><br />
<input type="submit" name="submit" value="Odeslat zprávu!" />
</form>
</div>
And PHPMailer script:
<?php
if ($_POST['frompage'] == "yes")
{
date_default_timezone_set('Etc/UTC');
require './PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
$mail->CharSet = 'UTF-8';
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "xxx";
//Password to use for SMTP authentication
$mail->Password = "xxx";
//Set who the message is to be sent from
$mail->setFrom($_POST['email'], $_POST['name']);
//Set an alternative reply-to address
$mail->addReplyTo('xxx', 'xxx');
//Set who the message is to be sent to
$mail->addAddress('xxx', 'xxx');
//Set the subject line
$mail->Subject = $_POST['subject'];
$mail->msgHTML($_POST['message']);
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Sending email works fine but after sending email the page redirects me to the gmail.php with Message sent echo.
How can I disable it beacuse of staying on the index.php with response in value of the submit button?
Thanks.
It's not redirecting you, you set the form action to "./plugins/mailer/gmail.php", that's where your form data gets sent.
Replace:
echo "Message sent!";
by
header("Location:YourFormPage.php");
It will send you back. It's possible to do what your looking for, you need to research about AJAX so using this will not be necessary to leave the first page and complete the form submition.

PHP Mail Sending Blank Message On Page-Load

I am having an issue with PHP Mailer. It is sending a blank email every time the page is loaded.
I'm sure it's something simple (maybe missing a condition if submit button is hit) to fix this.
Their documentation doesn't seem to have one though and the script first worked when I first used it but then started sending emails on page load after the first few times. Thanks!
<form method="post" action="">
<div class="form-group">
<input name="name" type="text" class="form-control" placeholder="Enter Name">
</div>
<div class="form-group">
<input name="email" type="email" class="form-control" placeholder="Enter Email">
</div>
<div class="form-group">
<input name="subject" type="text" class="form-control" placeholder="Enter Subject">
</div>
<div class="form-group">
<textarea name="message" class="form-control" rows="5" placeholder="Enter Message"></textarea>
</div>
<button name="submit" type="submit" value="submit" class="btn btn-submit">Submit</button>
</form>
<?php
require '/phpmailer/PHPMailerAutoload.php';
require_once('/phpmailer/class.phpmailer.php');
include("/phpmailer/class.smtp.php");
$emailaddress = 'info#newpointdigital.com';
$message=
'Name: '.$_POST['name'].'<br />
Email: '.$_POST['email'].'<br />
Subject: '.$_POST['subject'].'<br />
IP: '.$_SERVER['REMOTE_ADDR'].'<br /><br />
Message:<br /><br />
'.nl2br($_POST['message']).'
';
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
//$mail->SMTPDebug = 2; // 1 = errors and messages,2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "smtp.gmail.com"; // sets the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "info#newpointdigital.com"; // SMTP account username (the email account your created)
$mail->Password = "newpoint!##$"; // SMTP account password (the password for the above email account)
$mail->SMTPSecure = 'ssl'; // Enable encryption, 'ssl' also accepted
$mail->CharSet = 'UTF-8'; // so it interprets foreign characters
$mail->SetFrom($_POST['email']);
$mail->AddReplyTo($_POST['email']);
$mail->Subject = "Contact form from ".$_POST['name']." ";
$mail->MsgHTML($message);
$mail->AddAddress($emailaddress);
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
You don't check to see if a form submission occurred. You just call your email code when the page loads. There are several ways to decide if a form submission occurred. One way is to check to see if the form action is POST:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// email code goes here
}
You can also check to see if the submit button was pressed:
if (isset($_POST['submit'])) {
// email code goes here
}
Other things to know:
You don't do any data validation so it is still possible for someone to submit nothing resulting in a blank email being sent
You don't protect against header injections which makes your form vulnerable to sending spam
This happen cause you didn't apply any condition for this to run so code runs line by line on page load .
so keep your mail sending code in submit check condition so it will run after submit click
if(isset($_POST['submit'])) {
// your mail sending code
}
or using any of your post data like for email check
if(!empty($_POST['email'])) {
// your mail sending code
}

How do I send an email from gmail within my app?

I have the following code. I would like to send a confirmation email to the user as soon as they click the 'register' button. When I click on the register button my database gets updated with the correct information but it doesn't send the email.
Email.php:
<?php
include('class.phpmailer.php');
include('class.smtp.php');
$mail = new PHPMailer();
$mail->IsSMTP();
//GMAIL config
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'ssl'; // sets the prefix to the server
$mail->Host = 'smtp.gmail.com'; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = 'xxx#gmail.com'; // GMAIL username
$mail->Password = 'yyy'; // GMAIL password
//End Gmail
$mail->From = 'xxx#gmail.com';
$mail->FromName = 'SYS';
$mail->Subject = 'Registration Successful';
$mail->MsgHTML('You have successfully been added to the System!');
$mail->AddAddress($_POST['EmailAddress']);
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) {//to see if we return a message or a value bolean
echo "Mailer Error: " . $mail->ErrorInfo;
} else echo "Message sent!";
?>
This is the part of the code where I get the email address from. I have it so that the user inputs his email address into the textbox. it gets saved as EmailAddress
AddUser.php:
<div class="">
<label style=""> Email Address </label>
<input id="Email Address" type="email" name="EmailAddress" value="">
</div>
My sql query goes here. I include the Email.php file after it.
<?php
.
.
.
include('./content/Email.php');
?>
This is my button. When the user clicks it, the information gets stored and is supposed to send the email.
</select></div>
</div>
</div>
<br><br>
<input type="submit" class="btn btn-submit" value="Add User">
</div>

Categories