How to disable PHPMailer redirect - php

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.

Related

PHPMailer not connecting HTML/PHP

I'm trying to get my HTML contact form to work with the PHPMailer, but when I try to send the message, I'm unable to connect to the server:
2017-06-15 10:39:15 SMTP ERROR: Failed to connect to server: (0) 2017-06-15 10:39:15 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I'm trying to connect to the SiteGround SMTP server.
Form code:
<form id="contact" action="mailer.php" method="post">
<div class="left">
<input type="text" placeholder="Name" required="required" name="name"/>
<input type="email" placeholder="Email" required="required" name="email"/>
<input type="text" placeholder="Subject" required="required" name="subject"/>
</div>
<div class="right">
<textarea placeholder="Message" required="required" name="message"></textarea>
</div>
<div class="send-button cl">
<button type="submit" name="submit">Send</button>
</div>
</form>
PHP Code:
//Creating the message
$message =
'Name: '.$_POST['name'].'<br />
Subject: '.$_POST['subject'].'<br />
Email: '.$_POST['email'].'<br />
Message: '.$_POST['message'].'
';
require 'phpmailer/PHPMailerAutoload.php';
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "mail.frankkreutzer.com";
$mail->Port = 465; // 587 | 465
// $mail->IsHTML(true);
// Authentication
$mail->Username = "email#domain.com";
$mail->Password = "password";
// Compose
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = $_POST['subject'];
$mail->MsgHTML($message);
// Send to
$mail->AddAddress("recipient#domain.com"); // Where to send it
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
Just fixed the PHP code:
<?php
require 'phpmailer/PHPMailerAutoload.php';
//Creating the message
$message =
'Name: '.$_POST['name'].'<br />
Email: '.$_POST['email'].'<br /><br />
Subject: '.$_POST['subject'].'<br />
Message: '.$_POST['message'].'
';
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // enable SMTP
// $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages
only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // 587 | 465
// $mail->IsHTML(true);
// Authentication
$mail->Username = "example#email.com";
$mail->Password = "password";
// Compose
$mail->setFrom($_POST['email']);
$mail->Subject = $_POST['subject'];
$mail->MsgHTML($message);
// Send to
$mail->AddAddress("example#email.com"); // Where to send it
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent. I'll get back to you as soon as possible!";
}
?>

SMTP Error: Could not connect to SMTP host. Phpmailer error

Here is my file, contact-form.php (code)
<?php
if(isset($_POST['submit']))
{
$message=
'Full Name: '.$_POST['fullname'].'<br />
Subject: '.$_POST['subject'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['emailid'].'<br />
Comments: '.$_POST['comments'].'
';
require "phpmailer/class.phpmailer.php"; //include phpmailer class
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "ssl"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 465; //Gmail SMTP port
$mail->Encoding = '7bit';
// Authentication
$mail->Username = "youremail#gmail.com"; // Your full Gmail address
$mail->Password = "yourpassword"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['emailid'], $_POST['fullname']);
$mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
$mail->Subject = "New Contact Form Enquiry"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("recipientemail#gmail.com", "Recipient Name"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
?>
<html>
<head>
<title>Contact Form</title>
</head>
<body>
<div style="margin: 100px auto 0;width: 300px;">
<h3>Contact Form</h3>
<form name="form1" id="form1" action="" method="post">
<fieldset>
<input type="text" name="fullname" placeholder="Full Name" />
<br />
<input type="text" name="subject" placeholder="Subject" />
<br />
<input type="text" name="phone" placeholder="Phone" />
<br />
<input type="text" name="emailid" placeholder="Email" />
<br />
<textarea rows="4" cols="20" name="comments" placeholder="Comments"></textarea>
<br />
<input type="submit" name="submit" value="Send" />
</fieldset>
</form>
<p><?php if(!empty($message)) echo $message; ?></p>
</div>
</body>
</html>
It Shows this error,
SMTP Error: Could not connect to SMTP host.
I know there are several times this question is asked here but nothing is worked for me,
I tried every previous tricks given here, Please anyone there can help me out.
Exact where is the error n my coding...
Please use TLS and port 587 for gmail
Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "tls"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 587; //Gmail SMTP port
$mail->Encoding = '7bit';
Read the below points and implemet according to this
Port 25, 465, or 587
SSL/TLS optional.
One or more static IP addresses are required.
Port 465 (SSL required)
Port 587 (TLS required)
Dynamic IPs allowed
Port 25
TLS not required
Dynamic IPs allowed
Mail can only be sent to Gmail or G Suite users
ref :- here
just paste this line your code and see what is the exact error . If it is again the SMTP error just follow above .
Ref :- here

Why isn't my contact form using PHPMailer working?

I made a contact form to send information to my email, and whenever I submit the form, I get redirected to my php file (mywebsite.com/contact.php) and am shown a Internal Server Error (the 500 kind). What did I do wrong? Could it be because I have my html and php code in different files? I included my contact form as well just in case it is relevant.
<?php
if(isset($_POST['submit']))
{
$message=
'Full Name: '.$_POST['name'].'<br/>
Comments: '.$_POST['comments'].'<br/>
Email: '.$_POST['email'].'
';
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mail.yahoo.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'aroncea#yahoo.com'; // SMTP username
$mail->Password = 'letmejusteditthisout'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom($_POST['email'], $_POST['name']);
$mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->addAddress('aroncea#yahoo.com'); // Add a recipient
$result = $mail->Send();
$message = $result ? 'Successfully sent!' : 'Sending Failed!';
unset($mail);
$mail->Subject = "New Form Submission";
$mail->MsgHTML($message);
?>
<!-- Container (Contact Section) -->
<form action="newcontact.php" method="POST" name='contactform' id='contactform' enctype="text/plain">
<div id="contact" class="container-fluid bg-grey">
<h2 class="text-center">CONTACT</h2>
<div class="row">
<div class="col-sm-5">
<p>Contact us and we'll get back to you within 24 hours. </p>
<p><span class="glyphicon glyphicon-map-marker"></span> Burlington, ON</p>
<p><span class="glyphicon glyphicon-phone"></span> 289-230-4510</p>
<p><span class="glyphicon glyphicon-envelope"></span> aroncea#yahoo.com</p>
</div>
<div class="col-sm-7 slideanim">
<div class="row">
<div class="col-sm-6 form-group">
<input class="form-control" id="name" name="name" placeholder="Name" type="text" required>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
</div>
</div>
<textarea class="form-control" id="comments" name="comments" placeholder="Comment (not required)" rows="3"></textarea><br>
<div class="row">
<div class="col-sm-12 form-group">
<input type="submit" name="submit" value="Submit" />
</div>
</div>
</div>
</div>
You are missing a } in your PHP-File. (You open a { after the if-statement but never close it.)
With 500 Errors, it usually helps to check the servers error-log.
assign your message and Subject before $mail->Send(); and close }.
try this code.
if(isset($_POST['submit']))
{
$message=
'Full Name: '.$_POST['name'].'<br/>
Comments: '.$_POST['comments'].'<br/>
Email: '.$_POST['email'].'
';
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mail.yahoo.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'aroncea#yahoo.com'; // SMTP username
$mail->Password = 'letmejusteditthisout'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom($_POST['email'], $_POST['name']);
$mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->addAddress('aroncea#yahoo.com'); // Add a recipient
$mail->Subject = "New Form Submission";
$mail->MsgHTML($message);
$result = $mail->Send();
$message = $result ? 'Successfully sent!' : 'Sending Failed!';
unset($mail);
}
i hope it will be helpful

php cant send email

i have code for send email to my email
but if i press ok i get can not send email to **#hotmail.com
i work on linux server (fedora), and i don not change any settings
my mail.html file is
<html>
<head><title>Mail sender</title></head>
<body>
<form action="mail.php" method="POST">
<b>Email</b><br>
<input type="text" name="email" size=40>
<p><b>Subject</b><br>
<input type="text" name="subject" size=40>
<p><b>Message</b><br>
<textarea cols=40 rows=10 name="message"></textarea>
<p><input type="submit" value=" Send ">
</form>
</body>
</html>
and my email.php file is :
<html>
<head><title>PHP Mail Sender</title></head>
<body>
<?php
# Retrieve the form data
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
# Sends mail and report success or failure
if (mail($email,$subject,$message)) {
echo "<h4>Thank you for sending email</h4>";
} else {
echo "<h4>Can't send email to $email</h4>";
}
?>
</body>
</html>
please try help me
Best if you use a dedicated script for sending emails. Such as PHPMailer which gives you various config options including SMTP:
// Send Mail
$mail = new PHPMailer(); // initiate
$mail->IsSMTP(); // use SMTP
// SMTP Configuration
$mail->SMTPAuth = true; // set SMTP
$mail->Host = "myhost"; // SMTP server
$mail->Username = "email#email.com";
$mail->Password = "password";
$mail->Port = 465; // change port is required

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