php mailer is not working - php

Here's my phpmailer code, its not working , dont know where i am getting error.
I have to embed this code in certain script and i think for gmail port no port no 465 .
<?php
if(isset($_POST['submit'])) {
require_once('phpmailer/class.phpmailer.php');
$email=$_POST['email'];
$subject1=$_POST['subject'];
$message=$_POST['message'];
smtpmailer($email,$subject1,$message);
function smtpmailer($to,$subject,$body) {
global $error;
$mail = new PHPMailer(); // create a new object
$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;
$mail->Username = "aman****589#gmail.com";
$mail->Password = "*****02589";
$mail->From = "aman***589#gmail.com";
$mail->FromName = "Cor****tions";
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
}
?><html><body>
<form method="post" action="index.php">
Email: <input name="email" id="email" type="text" /><br />
Subject:<br />
<textarea name="subject" id="subject" rows="2" cols="40"></textarea><br />
Message:<br/>
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" name="submit"/>
</form>
</body>
</html>
Can anybody help ?? where i m getting error?
when i run this script its giving error-
Fatal error: Call to undefined function smtpmailer() in C:\xampp\htdocs\phpm\index.php on line 8

You have to declare a function with its parameters, before you call it. At the moment, your program is searching for the function smtpmailer() - but it is not even defined yet.
<?php
if(isset($_POST['submit'])) {
require_once('phpmailer/class.phpmailer.php');
$email=$_POST['email'];
$subject1=$_POST['subject'];
$message=$_POST['message'];
function smtpmailer($to,$subject,$body) {
global $error;
$mail = new PHPMailer(); // create a new object
$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;
$mail->Username = "aman****589#gmail.com";
$mail->Password = "*****02589";
$mail->From = "aman***589#gmail.com";
$mail->FromName = "Cor****tions";
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
smtpmailer($email,$subject1,$message);
}
?><html><body>
<form method="post" action="index.php">
Email: <input name="email" id="email" type="text" /><br />
Subject:<br />
<textarea name="subject" id="subject" rows="2" cols="40"></textarea><br />
Message:<br/>
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" name="submit"/>
</form>
</body>
</html>

Declare the function before you call it.

Related

Echo = Message has been sent without errors; however, no message is sent. PHP/HTML/Gmail/PHPMailer [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I'm running locally from my mac, when the info is entered and I click send the email is not showing up in the outgoing email account in sent mail, and not showing up in the incoming email either; spam folder included. There is not an error echo. The echo states the message is sent without error. I'm at a loss here. By the way I am a newbie at coding so please be gentle and explain in depth. I appreciate any help that anyone might be able to provide. Here is my code.
<form method="post" enctype="multipart/form-data">
<p>
E-Mail:
</p>
<p>
<input type="text" name="receiver" />
</p>
<p>
Subject:
</p>
<p>
<input type="text" name"subject" />
</p>
<p>
Note:
</p>
<p>
<textarea name="message"></textarea>
</p>
<p>
Select Photo:
<input type="file" name="file" />
</p>
<input type="submit" name="submit" value="Send Email" />
</form>
<?php
if (isset($_POST['submit'])) {
require "php-mailer-master/PHPMailerAutoload.php";
$mail = new PHPMailer;
try {
$sender = "????????????#gmail.com";
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '$sender';
$mail->Password = '????????????';
$mail->SMTPSecure = 'tls';
$mail->Port = '587';
$mail->isHTML();
$mail->setFrom($sender, 'Mailer');
$mail->addAddress($_POST["receiver"]);
$mail->addAttachment ($file_name);
$mail->isHTML(true);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["message"];
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
?>
The code below will send a message for me. I replaced the ??? with my username and password (also had to set my gmail account to allow access).
Changes I made to your code:
I changed the require for my composer locally.
You used single quotes around the $sender - needs to be double quotes.
Added titles to the inputs (probably not needed).
Also used the namespaced values for instantiating $mail PHPMAILER();
Commented out the attachment feature.
I would also recommend checking into sanitizing your user input.
Something like:
$name = trim(filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL));
Code:
<form method="post" enctype="multipart/form-data">
<p>
E-Mail:
</p>
<p>
<input type="text" name="receiver" title="receiver1"/>
</p>
<p>
Subject:
</p>
<p>
<input type="text" name="subject" title="subj1"/>
</p>
<p>
Note:
</p>
<p>
<textarea name="message" title="message"></textarea>
</p>
<p>
Select Photo:
<input type="file" name="file"/>
</p>
<input type="submit" name="submit" value="Send Email"/>
</form>
<?php
require '../vendor/autoload.php';
if (isset($_POST['submit'])) {
$mail = new \PHPMailer\PHPMailer\PHPMailer();
try {
$sender = "?????????#gmail.com";
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = "$sender";
$mail->Password = '?????????????';
$mail->SMTPSecure = 'tls';
$mail->Port = '587';
$mail->isHTML();
$mail->setFrom($sender, 'Mailer');
$mail->addAddress($_POST["receiver"]);
// $mail->addAttachment ($file_name);
$mail->isHTML(true);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["message"];
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail-
>ErrorInfo;
}
}
?>

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!";
}
?>

PHP script with form not sending the email

I am having some problems with php. Well, the code, when uploaded to the server and run in browser is working and the email is arriving, using this code below:
<?php
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPDebug = 3;
$mail->IsSMTP();
//$mail->IsSMTP();
$mail->Debugoutput = 'html';
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPSecure = false;
$mail->SMTPAuth = false;
$mail->Username = "exemple#gmail.com";
$mail->Password = "password";
$name = "catarina";
$email = "myEmail#hotmail.com";
$message = "test";
$mail->setFrom($email, $name);
$mail->addAddress('me#exemple.io');
$mail->Subject = 'Contact Site';
$mail->Body = $message;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
The problem is, when I change this code a little to send with a form, the email never arrives:
js:
function submitForm() {
$.ajax({type:'POST', url:'email-action.php', data:$('#contact-form').serialize(), success: function(result){
$('.submit').html('send');
$('.send').removeClass('no-show');
$('.send').removeClass('no-show-mobile');
}});
};
html:
<form action="/" onsubmit="return submitForm();" method="post" name="contactform" class="contact-form wow zoomIn" data-wow-delay="0.6s" id="contact-form">
<div class="col-md-6">
<div class="row">
<div class="col-md-12">
<input placeholder="Name" class="input-field" name="name" required="" type="text">
</div>
<div class="col-md-12">
<input placeholder="E-mail" class="input-field" name="email" required="" type="email">
</div>
</div>
</div>
<div class="col-md-6">
<textarea placeholder="Message" class="input-field" name="message"></textarea>
<input value="Send Message" class="input-send submit" type="submit" name="submit">
</div>
<div class="col-md-12 send no-show hidden-xs"><button class=" botao btn btn-sucess"><h4 class="">Message sent sucessfully! We will get in touch shortly.</h4></button></div>
<div class="col-md-12 send no-show-mobile visible-xs"><button class=" botao btn btn-sucess"><h4 class="">Message sent sucessfully!</h4></button></div>
</form>
php:
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPDebug = 3;
$mail->IsSMTP();
//$mail->IsSMTP();
$mail->Debugoutput = 'html';
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPSecure = false;
$mail->SMTPAuth = false;
$mail->Username = "exemple#gmail.com";
$mail->Password = "password";
///this is the thing that change for one script to another
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
/////
$mail->setFrom($email, $name);
$mail->addAddress('me#exemple.io');
$mail->Subject = 'Contact Site';
$mail->Body = $message;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

Php script to sending email doesn't work

I created a simple form on my page and now I tried to add php script to sending email. Unfortunately it does not work. After clicking on the button, I want the user to remain on my side without redirection.
mail_sender.php
<?php
if(isset($_POST['submit'])){
$to = "someone#gmail.com";
$from = $_POST['email'];
$message = " You received the fallowing message:" . "\n\n" . $_POST['message'];
mail($to,$message,$from);
echo "Mail Sent. Thank you, we will contact you shortly.";
}
?>
HTML
<form action="mail_sender.php" method="post">
<textarea id="email" name="email" rows="1" cols="30" placeholder="Type your email"></textarea>
<textarea id="formContent" name="message" rows="6" cols="30" placeholder="Type your message"></textarea>
<input type="submit" id="submit" value="Send">
</form>
First of all name attribute is missing in your submit button. And php mail function is wrong.
It should be:
$subject = "Your subject";
$headers = "From: $from ";
mail($to,$subject,$message,$headers);
instead of:
mail($to,$message,$from);
PHP's default mail() function doesn't work most of the times, especially with GMail. This is because your e-mail needs to be formatted in a special way to be accpeted by Google's mail server. You'll be better off using a mail library like PHPMailer.
Here's how to send an e-mail using PHPMailer from a GMail Account.
$mail = new PHPMailer();
// ---------- adjust these lines ---------------------------------------
$mail->Username = "xxx#gmail.com"; // your GMail user name
$mail->Password = "passwd"; // your GMail Password
$mail->AddAddress("yyy#gmail.com"); // recipients email
$mail->FromName = "Your Name"; // readable name
$mail->Subject = "Subject";
$mail->Body = "Body";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->From = $mail->Username;
//----------------------------------------------------------------------
if(!$mail->Send())
{
echo "mail sent";
}
I tried everything and now i received message SMTP ERROR: Failed to connect to server and SMTP connect() failed
<form action="mail_sender.php" method="post">
<textarea id="email" name="email" rows="1" cols="30" placeholder="Type your email"></textarea>
<textarea id="formContent" name="message" rows="6" cols="30" placeholder="Type your message"></textarea>
<input type="submit" name="submit" id="submit" value="Send">
</form>
PHP
<?php
require "PHPMailer-master/PHPMailerAutoload.php";
$mail = new PHPMailer();
$to = "someone#gmail.com"; // required
$from = $_POST['email'];
$comment =
'Email: '.$from.' <br> />
Message: '.$_POST['message'].' <br> />'
;
$mail->Username = "someone#gmail.com"; // your GMail user name
$mail->Password = "Password"; // your GMail Password
$mail->AddAddress("someone#gmail.com"); // recipients email
$mail->setFrom($_POST['email']);
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->SMTPDebug = 1;
$mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Subject = 'Here is the subject';
//----------------------------------------------------------------------
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>

Sending multiple data with SMTP

Here is index.html codes;
<form name="contactform" method="POST" action="send.php">
Name: <input type="text" name="ad_soyad" size="25"><br />
Telephone: <input type="text" name="tel" size="25"><br />
E-Mail: <input type="text" name="email" size="25"><br />
Address: <textarea rows="5" name="adres" cols="25"></textarea><br />
Message: <textarea rows="5" name="mesaj" cols="25"></textarea><br />
<input type="submit" name="button" value="Gonder">
</form>
Here is send.php codes:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'blablabla.com';
$mail->SMTPAuth = true;
$mail->Username = 'blablabla.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
if(($ad_soyad=="") or ($tel =="") or ($email=="") or ($mesaj=="")){
echo "<center>Please fill the required fields.<br><a href=index.html>Go back</a></center>";
}
else
{
$mail->setFrom($_POST['email']);
$mail->addAddress('contact#blablabla.com', 'BlaBlaBla');
$mail->isHTML(true);
$mail->Subject='Contact Form Message';
$mail->Body = ($_POST['mesaj']);
$smtp = new SMTP;
//Enable connection-level debug output
$smtp->do_debug = SMTP::DEBUG_CONNECTION;
if(!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent.';
}
}
?>
It works.
But only sends name,email and message.
So, I want to add more field like telephone or address or etc.
but i can not put them into $mail->body when i tried to write telephone and message together
it gives me error:"not configured properly "
I have to use SMTP because my hosting only provides SMTP..
how can i send all datas which form gets from user ,with SMTP?
I don't know if this will solve the issue - I can't understand why it should fail if you add more content to the message body but as you have not shown that I can only guess it was done in such a way as to make invalid markup?
<?php
/* Are these variables defined before this point? */
if( ( $ad_soyad=="" ) or ( $tel =="" ) or ( $email=="" ) or ( $mesaj=="" ) ){
echo "<center>Please fill the required fields.<br><a href=index.html>Go back</a></center>";
} else {
/* only load the libraries if the above variables are not empty */
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'blablabla.com';
$mail->SMTPAuth = true;
$mail->Username = 'blablabla.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
/* some basic filtering of user supplied variables */
$email=filter_input( INPUT_POST, 'email', FILTER_VALIDATE_EMAIL );
$message=filter_input( INPUT_POST, 'mesaj', FILTER_SANITIZE_STRING );
$name=filter_input( INPUT_POST, 'ad_soyad', FILTER_SANITIZE_STRING );
$tel=filter_input( INPUT_POST, 'tel', FILTER_SANITIZE_STRING );
$address=filter_input( INPUT_POST, 'adres', FILTER_SANITIZE_STRING );
/* prepare message components */
$msg=array();
$msg[]="Name:".$name;
$msg[]="Phone:".$tel;
$msg[]="Address:".$address;
$msg[]="Message:".$message;
$mail->setFrom( $email );
$mail->addAddress('contact#blablabla.com', 'BlaBlaBla');
$mail->isHTML(true);
$mail->Subject='Contact Form Message';
$mail->Body = implode( "\r\n", $msg );
$smtp = new SMTP;
//Enable connection-level debug output
$smtp->do_debug = SMTP::DEBUG_CONNECTION;
if( !$mail->send() ) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent.';
}
$mail = $smtp = null;
}
?>

Categories