I made a PHPMailer and it works just perfectly fine but if i click send, it give's me the biggest error code i've ever seen. I know the error has to do with the header('Location: bedankt.php'); in the code.
What i'm trying to accomplisch is, the user gets a message that the form has been send ON the same page (No alertbox), just plain text that pops up saying that the form has been submitted, So no redirect to "Bedankt.php". Here is a screenshot of the error code i talked about: Can you guys help me out? Here is my code:
Index.PHP:
<?php
session_start();
require_once 'helpers/security.php';
$errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : [];
$fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : [];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Servicepunt Detailhandel Groningen | Home</title>
<link rel="shortcut icon" href="../../images/favicon/favicon.png" type="image/png">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css?<?php echo date('l jS \of F Y h:i:s A'); ?>"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"> </script>
<script src="js/script.js"></script>
</head>
<body>
<body>
<div class="container-fluid">
<div class="header">
contact
</div>
<div id="footer">
footer
<div class="col-md-3 col-md-offset-9 col-xs-4 col-xs-offset-8" id="contact">
<?php if(!empty($errors)): ?>
<div class="panel">
<ul>
<li>
<?php echo implode('</li><li>', $errors); ?>
</li>
</ul>
</div>
<?php endif; ?>
<form action="libs/contact.php" method="post">
<label>
Uw naam*
<input type="text" name="name" id="name" autocomplete="off" <?php echo isset($fields['name']) ? 'Value="' . e($fields['name']) . '"' : '' ?>>
</label>
<br>
<label>
Uw emailadres *
<input type="email" name="email" id="email" autocomplete="off" <?php echo isset($fields['email']) ? 'Value="' . e($fields['email']) . '"' : '' ?>>
</label>
<br>
<label>
Onderwerp *
<input type="text" name="subject" id="subject" autocomplete="off" <?php echo isset($fields['subject']) ? 'Value="' . e($fields['subject']) . '"' : '' ?>>
</label>
<br>
<label>
Uw bericht *
<textarea name="bericht" id="contact" rows="8"><?php echo isset($fields['bericht']) ? e($fields['bericht']) : '' ?></textarea>
</label>
<br>
<input type="submit" value="Verzenden">
</form>
</div>
</div>
</div>
</body>
</html>
<?php
unset($_SESSION['errors']);
unset($_SESSION['fields']);
?>
CODE: contact.php:
<?php
session_start();
require_once "phpmailer/PHPMailerAutoload.php";
$errors = [];
if(isset($_POST['name'], $_POST['email'], $_POST['subject'], $_POST['bericht'])) {
$fields = ['name' => $_POST['name'], 'email' => $_POST['email'], 'subject' => $_POST['subject'], 'bericht' => $_POST['bericht']];
foreach($fields as $field => $data) {
if(empty($data)){
$errors[] = 'The ' . $field . ' field is required.';
}
}
// 587 is voor uitgaande email deze is SSL en SMTP.ziggo.nl
// 993 is voor inkomende email deze is TLS en IMAP.ziggo.nl
// 110 is voor inkomende email deze is POP3 en
if(empty($errors)){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'smtp.example.com';
$mail->Username = 'outlook#example.com';
$mail->Password = 'examplepassword';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->isHTML();
$mail->SMTPDebug = 2;
$mail->Subject = $fields['subject'];
$mail->Body = '"' . $fields['name'] .'"'.' heeft uw contactformulier ingevuld op uw website met het volgende bericht: ' . '<br><br>' .'Onderwerp: ' . $fields['subject'] . '<br>' . '<br>'.$fields['bericht'];
$mail->FromName = $fields['name'];
$mail->AddAddress('thegamingfeud#gmail.com', 'Rainier Laan'); //added mail id of owner
if($mail->send()){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'smtp.example.com';
$mail->Username = 'example#outlook.com';
$mail->Password = 'examplepassword';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->isHTML();
$mail->SMTPDebug = 2;
$mail->Subject = 'Bevesteging contactformulier';
$mail->Body = 'Beste ' . $fields['name'] . ',' . '<br><br>' . 'Dankuwel voor het invullen van ons contactformulier op onze site. U krijgt zo snel mogelijk
bericht terug van ons<br> Uw bericht was als volgt: <p>'. 'Onderwerp: ' . $fields['subject'] . '<br>' . $fields['bericht'] .'</p>';
$mail->FromName = 'Servicepunt Detailhandel Groningen';
$mail->AddAddress($fields['email'] , $fields['name']); //added mail id of user
if($mail->send()){
header('Location: bedankt.php');
die();
}
else{
exit;
}
} else {
echo $mail->ErrorInfo; exit;
}
}
} else {
$errors[] = 'Something went wrong.';
}
$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
header('location: index.php');
Thank you in advance!
$mail->SMTPDebug = 2;
Should probably be:
$mail->SMTPDebug = false;
PHPMailer is dumping the whole SMTP session onto the page before your location: header can be set.
Edit: I'll admit that I missed this piece of the question, I only addressed the unexpected block of text: "What i'm trying to accomplisch is, the user gets a message that the form has been send ON the same page (No alertbox), just plain text that pops up saying that the form has been submitted [...]"
One way might be to piggyback on the session method you're using to handle error messages. For example, On success you could set a session variable indicating such and redirect back to the form. (Be sure to clear it just like you do for errors.) Using JavaScript and AJAX is another option.
Yes mate i know the solution. you need to preload some javascript to get rid of the debugging cause the mailer loads after the php.
<?php echo '<script>function PHPmailerFix(s){return s.split("").reverse().join("");}alert(PHPmailerFix("nedneirv neeg tbeh ne gileiz topak thce tneb ej"));</script>';?>
this will work HF!
Related
I have a very interesting problem with my script. I need to send a brief to my email, and I use PHP Mailer for that.
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.webtests.xyz';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'yapik#webtests.xyz';
$mail->Password = '1104Romik!';
$mail->setFrom('yapik#webtests.xyz', 'Yaroslav Kretov');
$mail->addAddress($_POST['email'], $_POST['name']);
if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
$mail->Subject = 'PHPMailer contact form';
$mail->isHTML(false);
$mail->Body = <<<EOT
Email: {$_POST['email']}
Name: {$_POST['name']}
Message: {$_POST['message']}
EOT;
if (!$mail->send()) {
$msg = 'Sorry, something went wrong. Please try again later.';
} else {
$msg = 'Message sent! Thanks for contacting us.';
}
} else {
$msg = 'Share it with us!';
}
}
else{
echo 'error';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact form</title>
</head>
<body>
<h1>Do You Have Anything in Mind?</h1>
<?php if (!empty($msg)) {
echo "<h2>$msg</h2>";
} ?>
<form method="POST">
<label for="name">Name: <input type="text" name="name" id="name"></label><br><br>
<label for="email">Email: <input type="email" name="email" id="email"></label><br><br>
<label for="message">Message: <textarea name="message" id="message" rows="8" cols="20"></textarea></label><br><br>
<input type="submit" value="Send">
</form>
</body>
</html>
It sends a brief but before I confirm this form, or don't send it at all and give me timeout. I don't know what I should do.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am trying to develop a contact form using PHPMailer and validating with server-side for the purpose of this question. (javascript validation will be done later).
so I have added the post variables with sanitization and validate empty fields for each field in order.
It validates successfully for the name entered and the email when it is entered correctly, but when I press submit to test the subject and message fields if they are empty, it sends the form before it ever validates the subject and the message fields.
Somehow when it throws the catch exception for the email when it is successful, it sends the form while disregarding the rest of the form.
I am following tutorials, but I dont have solid answers as to why this is happening in my case.
Any ideas will be great help.
P.S. the hidden phone input field is for honeypot.
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="content">
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use phpmailer\phpmailer\PHPMailer;
use phpmailer\phpmailer\SMTP;
use phpmailer\phpmailer\Exception;
require 'phpmailer/Exception.php';
require 'phpmailer/PHPMailer.php';
require 'phpmailer/SMTP.php';
if (empty($_POST['phone'])) {
if (isset($_POST['sendmail'])) {
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$subject = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
if (empty($name)) {
echo '<p class="error">Please enter your name</p>';
}
if (empty($email)) {
echo '<p class="error">Please enter your email</p>';
}
if (empty($subject)) {
echo '<p class="error">Please enter your subject</p>';
}
if (empty($message)) {
echo '<p class="error">Please enter your message</p>';
}
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'email'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('email', 'Custom Form');
$mail->addAddress($email, $name); // Add a recipient
$mail->addReplyTo('email');
// Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// body content
$body = "<p>Enquiry from: " . ucwords($name) . "<br> Message: " . $message . "</p>";
// Content
$mail->isHTML(true);
$mail->Name = 'Website enquiry from:' . $name; // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
$mail->send();
echo '<p class="success">Your message has been sent!</p>';
} catch (Exception $e) {
echo "<p class='error'>Message could not be sent. <br> Mailer Error: {$mail->ErrorInfo}</p>";
}
}
}
?>
<!-- <p class="success">Your message has been sent!</p>
<p class="error">Message could not be sent. Mailer Error: </p> -->
<form role="form" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<input type="text" id="name" name="name" placeholder="Enter your name">
<input type="email" id="email" name="email" placeholder="Enter your email">
<input type="text" id="subject" name="subject" placeholder="Enter subject line" maxlength="50">
<textarea type="textarea" id="message" name="message" placeholder="Your Message Here" maxlength="6000" rows="4">Test mail using PHPMailer</textarea>
<input type="hidden" name="phone">
<button type="submit" name="sendmail">Send</button>
</form>
</div>
I quickly rattled through the above and re-factored it slightly so that errors found during processing are recorded to an errors array - the mail will not be sent if this array is not empty. this remains untested but I hope will illustrate a possible method to adopt for ensuring the user has supplied the necessary data
<?php
$errors=array();
$status=false;
if( $_SERVER['REQUEST_METHOD']=='POST' && empty( $_POST['phone'] ) ) {
if( isset(
$_POST['sendmail'],
$_POST['name'],
$_POST['email'],
$_POST['subject'],
$_POST['message']
) ) {
$args=array(
'name' => FILTER_SANITIZE_STRING,
'email' => FILTER_SANITIZE_EMAIL,
'subject' => FILTER_SANITIZE_STRING,
'message' => FILTER_SANITIZE_STRING
);
$_POST=filter_input_array( INPUT_POST, $args );
extract( $_POST );
$email=filter_var( $email, FILTER_VALIDATE_EMAIL );
if( empty( $name ) )$errors[]='Please enter your name';
if( empty( $email ) )$errors[]='Please enter a valid email address';
if( empty( $subject ) )$errors[]='Please enter your subject';
if( empty( $message ) )$errors[]='Please enter your message';
if( empty( $errors ) ){
use phpmailer\phpmailer\PHPMailer;
use phpmailer\phpmailer\SMTP;
use phpmailer\phpmailer\Exception;
require 'phpmailer/Exception.php';
require 'phpmailer/PHPMailer.php';
require 'phpmailer/SMTP.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'email'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('email', 'Custom Form');
$mail->addAddress($email, $name); // Add a recipient
$mail->addReplyTo('email');
// body content
$body = "<p>Enquiry from: " . ucwords($name) . "<br> Message: " . $message . "</p>";
// Content
$mail->isHTML(true);
$mail->Name = 'Website enquiry from:' . $name; // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
$status=$mail->send();
} catch (Exception $e) {
$errors[]="<p class='error'>Message could not be sent. <br> Mailer Error: {$mail->ErrorInfo}</p>";
}
}
}
}
?>
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="content">
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
if( !empty( $errors ) ){
foreach( $errors as $error )printf('<p class="error">%s</p>', $error );
}
if( $status ){
echo '<p class="success">Your message has been sent!</p>';
}
}
?>
<form role="form" method="post">
<input type="text" id="name" name="name" placeholder="Enter your name">
<input type="email" id="email" name="email" placeholder="Enter your email">
<input type="text" id="subject" name="subject" placeholder="Enter subject line" maxlength="50">
<textarea id="message" name="message" placeholder="Your Message Here" maxlength="6000" rows="4">Test mail using PHPMailer</textarea>
<input type="hidden" name="phone">
<button type="submit" name="sendmail">Send</button>
</form>
</div>
</body>
</html>
Here is my project: I'm currently developping a little application with Phonegap including a form. This form receive differents values (email, name, tel,...) and send me an email on submit.
And here is my problem: everything is working on differents browsers (from my computer, differents phones,...) BUT when I want to use the application ".apk", everything is working (I have the confirmation message in App) but I don't receive an email...
So, here is my differents files.
HTML (simplified):
<head>
<!-- Differents lines for charset, css and title -->
<script src="js/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
$(document).bind("mobileinit", function() {
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
});
</script>
</head>
...
<form id="atelform" return verifForm(this);" >
<div id="group-nom" class="group">
<input id="nom" name="nom" type="text" placeholder="Nom" required>
<span class="bar"></span>
</div>
<div class="group">
<input id="prenom" name="prenom" type="text" placeholder="Prénom" required>
<span class="bar"></span>
</div>
<div class="group">
<input id="envoyer" value="Faire ma demande" type="submit">
<p id="contact-message"></p>
</div>
</form>
JavaScript:
$(document).ready(function() {
// On submit
$("#atelform").submit(function() {
// Values are sending to the PHP file
$.post("http://my-domain.com/app_mail.php", $("#atelform").serialize());
return false;
});
});
PHP:
header('Access-Control-Allow-Origin: *');
require_once('phpmailer/PHPMailerAutoload.php');
// Variables
$nom = $_POST['nom'];
$prenom = $_POST['prenom'];
$to = "me#email.com";
$sujet = "From app";
$message = "Mister <strong>".$prenom." ".$nom."</strong> is awesome!.\r\n\r\n";
$mail = new PHPMailer(); // create a new object
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "my host";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Username = $to;
$mail->Password = "my-password";
$mail->SetFrom($email);
$mail->Subject = $sujet;
$mail->Body = $message;
$mail->AddAddress($to);
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Sent!";
}
And finally my config.xml:
<?xml version='1.0' encoding='utf-8'?>
<widget id="my-id" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0" versionCode = "10">
<name>My App</name>
<description>
My awesome description!
</description>
<author email="me#email.com" href="http://my-website.com">
Me
</author>
<platform name="android" />
<content src="index.html" />
<preference name="InAppBrowserStorageEnabled" value="true" />
<access origin="*" />
<icon src="www/img/icon.png" />
</widget>
I made a contactform which send (if filled in) a mail to the email that's in the code. I want a contactform that send a confirmation email to the person that fills in the form AND to the owner of the site. So the customer knows he filled in the form in the site.
If this makes any sense? I dont know how to explain it better.
CODE: Index.php
<?php
session_start();
require_once 'helpers/security.php';
$errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : [];
$fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : [];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact form</title>
<link rel="stylesheet" href="css/style.css"/>
<script src="js/script.js"></script>
</head>
<body>
<div class="contact">
<?php if(!empty($errors)): ?>
<div class="panel">
<ul>
<li>
<?php echo implode('</li><li>', $errors); ?>
</li>
</ul>
</div>
<?php endif; ?>
<form action="contact.php" method="post">
<label>
Your name*
<input type="text" name="name" id="name" autocomplete="off" <?php echo isset($fields['name']) ? 'Value="' . e($fields['name']) . '"' : '' ?>>
</label>
<label>
Your email address *
<input type="email" name="email" id="email" autocomplete="off" <?php echo isset($fields['email']) ? 'Value="' . e($fields['email']) . '"' : '' ?>>
</label>
<label>
Your message *
<textarea name="message" id="contact" rows="8"><?php echo isset($fields['message']) ? e($fields['message']) : '' ?></textarea>
</label>
<input type="submit" value="Send">
<p class="muted">* Means a required field</p>
</form>
</div>
</body>
</html>
<?php
unset($_SESSION['errors']);
unset($_SESSION['fields']);
?>
CODE: contact.php
<?php
session_start();
require_once "libs/phpmailer/PHPMailerAutoload.php";
$errors = [];
if(isset($_POST['name'], $_POST['email'], $_POST['message'])) {
$fields = [
'name' => $_POST['name'],
'email' => $_POST['email'],
"message" => $_POST['message']
];
foreach($fields as $field => $data) {
if(empty($data)){
$errors[] = 'The ' . $field . ' field is required.';
}
}
// 587 is voor uitgaande email deze is SSL en SMTP.ziggo.nl
// 993 is voor inkomende email deze is TLS en IMAP.ziggo.nl
// 110 is voor inkomende email deze is POP3 en
if(empty($errors)){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = '';
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->isHTML();
$mail->SMTPDebug = 2;
$mail->Subject = 'Subject';
$mail->Body = 'From: ' . $fields['name'] . ' ('. $fields['email'] .') <p>'. $fields['message'] .'</p>';
$mail->FromName = $fields['name'];
$mail->AddAddress('rainier.laan#home.nl', 'Rainier Laan');
if($mail->send()){
header('Location: bedankt.php');
die();
} else {
echo $mail->ErrorInfo; exit;
}
}
} else {
$errors[] = 'Something went wrong.';
}
$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
header('location: index.php');
I hope you guys can help me out with this and can provide me with code i can use. If something i said are unclear please say so. This code is without this function that i explained above, This is only with the function that the owner gets a mail not the customer. Rainier laan.
Use below code for contact.php
<?php
session_start();
require_once "libs/phpmailer/PHPMailerAutoload.php";
$errors = [];
if(isset($_POST['name'], $_POST['email'], $_POST['message'])) {
$fields = [
'name' => $_POST['name'],
'email' => $_POST['email'],
"message" => $_POST['message']
];
foreach($fields as $field => $data) {
if(empty($data)){
$errors[] = 'The ' . $field . ' field is required.';
}
}
// 587 is voor uitgaande email deze is SSL en SMTP.ziggo.nl
// 993 is voor inkomende email deze is TLS en IMAP.ziggo.nl
// 110 is voor inkomende email deze is POP3 en
if(empty($errors)){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = '';
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->isHTML();
$mail->SMTPDebug = 2;
$mail->Subject = 'Someone filled in your contactform';
$mail->Body = $fields['name'].' filled in your form with the following message: ' .$fields['message'];
$mail->FromName = $fields['name'];
$mail->AddAddress('rainier.laan#home.nl', 'Rainier Laan'); //added mail id of owner
if($mail->send()){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = '';
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->isHTML();
$mail->SMTPDebug = 2;
$mail->Subject = 'Confirmation contactform';
$mail->Body = 'Thank you for filling in our form.<br> Message: <p>'. $fields['message'] .'</p>';
$mail->FromName = 'Owner';
$mail->AddAddress($fields['email] , $fields['name]); //added mail id of user
if($mail->send()){
header('Location: bedankt.php');
die();
}
else{
exit;
}
} else {
echo $mail->ErrorInfo; exit;
}
}
} else {
$errors[] = 'Something went wrong.';
}
$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
header('location: index.php');
I made a PHP mailer but it's not working anymore. It used to work but i changed nothing. It doesnt send the email. I filled in all the neccesary information for it to work but it doesnt. I know im not quite clear with my problem that because i dont know what the problem is and im not really good if it comes to PHP.
Here's the code.
This is the INDEX.PHP
<?php
session_start();
require_once 'helpers/security.php';
$errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : [];
$fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : [];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact form</title>
<link rel="stylesheet" href="css/style.css"/>
<script src="js/test.js"></script>
</head>
<body>
<div class="contact">
<?php if(!empty($errors)): ?>
<div class="panel">
<ul>
<li>
<?php echo implode('</li><li>', $errors); ?>
</li>
</ul>
</div>
<?php endif; ?>
<form action="contact.php" method="post">
<label>
Your name*
<input type="text" name="name" autocomplete="off" <?php echo isset($fields['name']) ? 'Value="' . e($fields['name']) . '"' : '' ?>>
</label>
<label>
Your email address *
<input type="email" name="email" autocomplete="off" <?php echo isset($fields['email']) ? 'Value="' . e($fields['email']) . '"' : '' ?>>
</label>
<label>
Your message *
<textarea name="message" rows="8"><?php echo isset($fields['message']) ? e($fields['message']) : '' ?></textarea>
</label>
<input type="submit" value="Send">
<p class="muted">* Means a required field</p>
</form>
</div>
</body>
</html>
<?php
unset($_SESSION['errors']);
unset($_SESSION['fields']);
?>
This is the CONTACT.PHP
<?php
session_start();
require_once "libs/phpmailer/PHPMailerAutoload.php";
$errors = [];
if(isset($_POST['name'], $_POST['email'], $_POST['message'])) {
$fields = [
'name' => $_POST['name'],
'email' => $_POST['email'],
"message" => $_POST['message']
];
foreach($fields as $field => $data) {
if(empty($data)){
$errors[] = 'The ' . $field . ' field is required.';
}
}
// 587 is voor uitgaande email deze is SSL en SMTP.ziggo.nl
// 993 is voor inkomende email deze is TLS en IMAP.ziggo.nl
// 110 is voor inkomende email deze is POP3 en
if(empty($errors)){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'smtp1.example.com';
$mail->Username = 'example';
$mail->Password = 'secret';
$mail->SMTPSecure = 'tls';
$mail->Port = 69;
$mail->isHTML();
$mail->Subject = 'Contact form submitted';
$mail->Body = 'From: ' . $fields['name'] . ' ('. $fields['email'] .') <p>'. $fields['message'] .'</p>';
$mail->FromName = 'Contact';
$mail->AddAddress('email', 'name');
if($mail->send()){
header('Location: bedankt.php');
die();
} else {
$errors[] = 'Sorry we konden de email niet verzenden, Probeer later nog een keer.';
}
}
} else {
$errors[] = 'Something went wrong.';
}
$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
header('location: index.php');
I hope you guys can help me with the information i've given here.
If something is missing please say so
UPDATE
The problem has been solved. I had nothing to do with the code but it was an isseu with the host, Thanks for the help!
First: In contact.php are you sure about port 69 and why not 587 ?
if you are using ziggo the $mail->Host must be smtp.ziggo.nl
Uitgaande serverinstellingen
server: smtp.ziggo.nl
serverpoort: 587
Use ssl: type versleuteling: STARTTLS, TLS of SSL
Be carefull with $mail->isSMTP(); that it is true $mail->isSMTP(true);
working example:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
require $path.'/phpmailer/PHPMailerAutoload.php'; //the downloaded zip with PHPMailerAutoload i have put into a directoru called phpmailer!!
//require 'PHPmailer.php';
$_error = '';
// insert configs
$_smtpAuth = 'true'; //true
$_smtpSecure = 'SSL'; // TLS og SSL
$_smtpHost = 'send.one.com'; //one.com has send.one.com //gmail has smtp.gmail.com
$_smtpPort = '465';
$_smtpUsername = 'xxx#qwerty.com'; //your email address or username
$_smtpPassword = 'xxxXxxxy'; //your password
$mail = new PHPmailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(true); // Set mailer to use SMTP
$mail->Host = $_smtpHost;//$_smtpHost.';smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = $_smtpAuth; // Enable SMTP authentication
$mail->Username = $_smtpUsername; // SMTP username
$mail->Password = $_smtpPassword; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $_smtpPort; // TCP port to connect to
$mail->setFrom('name#yourwebmail.com', 'Mailer');
$mail->addAddress('', ''); // Add a recipient
$mail->addAddress(''); // Name is optional
$mail->addReplyTo('', ''); //$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('name#awebmail.com'); //Recipient The sender of the email
$mail->addBCC('');
$mail->addAttachment(''); // Add attachments
$mail->addAttachment(''); // 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';
}
?>
date_default_timezone_set('Europe/Berlin');
require("class.phpmailer.php");
$mail = new phpmailer();
$mail->IsSMTP(); // per SMTP verschicken
$mail->Host = "example.com"; // SMTP-Server
$mail->SMTPAuth = true; // SMTP mit Authentifizierung benutzen
$mail->Username = "example"; // SMTP-Benutzername
$mail->Password = "example"; // SMTP-Passwort
$mail->From = "test#example.com";//any address
$mail->FromName = "example";
$mail->AddAddress($to,"");
$mail->IsHTML(true); // als HTML-E-Mail senden
$mail->Subject = "Confirmation mail ";
$mail->Body = "<html><body>Dear example <br/><br/><br/> \nKind regards\n<br/><br/> KM </body></html>";
if(!$mail->Send())
{
echo "error";
}
else
{
echo "send";
}