I am creating a website as a project for college and I want to include a contact page, of course. In this contact page, I created a form in which the user of the website can contact me (the admin of the website) and send me emails. I will display the code that I wrote immediately.
Can you also please tell me where exactly I should write the code? Maybe this is what I am doing wrong. So, the first time, I wrote it in a function and I called that function in the contact form. It didn't work. Then, I wrote the code for PHPMailer in a separate php file named send_mail.php and in the contact form (which is another file) i wrote the attribute action="send_mail.php" in the form tag. Is this right? Please tell me if I do this wrong.
Also, I want to receive those emails on my actual gmail account
I can't make it work and I have the following errors:
Warning: Creating default object from empty value in D:\Xampp\htdocs\licenta_ecomm\public\send_mail.php on line 24
Fatal error: Uncaught Error: Call to undefined method stdClass::isSMTP() in D:\Xampp\htdocs\licenta_ecomm\public\send_mail.php:25 Stack trace: #0 {main} thrown in D:\Xampp\htdocs\licenta_ecomm\public\send_mail.php on line 25
Now, I am pretty new to this and I haven't used PHPMailer before and I am a bit confused.
This is the contact form (after comes the phpmailer code)
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2 class="section-heading">Contacteaza-ne!</h2>
<h3 class="section-subheading text-muted"><?php display_message(); ?></h3>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<form name="sentMessage" id="contactForm" action="send_mail.php" method="post" >
<!-- EVENTUAL FUNCTIE??-->
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" name="nume_contact" class="form-control" placeholder="Numele *" id="name" required data-validation-required-message="Introduceți numele dvs.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="email" name="email_contact" class="form-control" placeholder="Adresă e-mail *" id="email" required data-validation-required-message="Introduceți adresa e-mail">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="text" name="subiect" class="form-control" placeholder="Subiect *" id="phone" required data-validation-required-message="Introduceți subiectul">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<textarea name="mesaj" class="form-control" placeholder="Mesajul dvs. *" id="message" required data-validation-required-message="Introduceți mesajul dorit"></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
<button name="submit" type="submit" class="btn btn-xl">Trimiteți mesaj</button>
</div>
</div>
</form>
</div>
</div>
</div>
And this is the code that I wrote for sending the emails (send_mail.php)
<!DOCTYPE html>
<html>
<?php
require_once("../resources/config.php");
//clase PHPMailer importante din namespace-ul global
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'D:\Xampp\composer\vendor\autoload.php';
//variabilele din formularul de contact
$nume_contact = escape_string($_POST['nume_contact']);
$subiect = escape_string($_POST['subiect']);
$email_from = escape_string($_POST['email_contact']);
$mesaj = escape_string($_POST['mesaj']);
try {
//Server settings
$mail->SMTPDebug = 1; // 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 = 'myemailaddress#gmail.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//sender
$mail->setFrom('myemailaddress#gmail.com', 'H.P.');
//recipient
$mail->addAddress($email_from, $nume_contact); // Add a recipient
//body content
$body = "<p>You received a message from: ".$nume_contact.". The message is: ".$mesaj."</p>";
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subiect;
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
</html>
Related
I am facing issue in my contact form, i want to send a mail using php on click on send message button i am getting error form submission failed no errors
<div class="col-lg-6 mt-5 mt-lg-0" data-aos="fade-left" data-aos-delay="100">
<form action="forms/contact.php" method="post" role="form" class="php-email-form">
<div class="row">
<div class="col-md-6 form-group">
<input type="text" name="name" class="form-control" id="name" placeholder="Your Name" required>
</div>
<div class="col-md-6 form-group mt-3 mt-md-0">
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email" required>
</div>
</div>
<div class="form-group mt-3">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" required>
</div>
<div class="form-group mt-3">
<textarea class="form-control" name="message" rows="5" placeholder="Message" required></textarea>
</div>
<div class="my-3">
<div class="loading">Loading</div>
<div class="error-message"></div>
<div class="sent-message">Your message has been sent. Thank you!</div>
</div>
<div class="text-center"><button type="submit" name="submit" id="submit">Send Message</button></div>
</form>
</div>
Here is my php code which i save with contact.php under forms folder.
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
$result = "";
$error = "";
if(isset($_POST['submit']))
{
$name = $_POST['name']; // Get Name value from HTML Form
$email_id = $_POST['email']; // Get Email Value
$to = "support#globalexpresssolutions.net"; // You can change here your Email
$subject = $_POST['subject']; // This is your subject
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//smtp settings
$mail->isSMTP(); // send as HTML
$mail->Host = "smtpout.secureserver.net"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "************"; // Your mail
$mail->Password = '************'; // Your password mail
$mail->Port = 465; //specify SMTP Port
$mail->SMTPSecure = 'ssl\tls';
$mail->setFrom($email_id,$name);
$mail->addAddress('**********');
$mail->addReplyTo($email_id,$name);
$mail->isHTML(true);
$mail->Subject=$subject;
$mail->Body="
<html>
<body>
<table style='width:600px;'>
<tbody>
<tr>
<td style='width:150px'><strong>Name: </strong></td>
<td style='width:400px'>$name</td>
</tr>
<tr>
<td style='width:150px'><strong>Email ID: </strong></td>
<td style='width:400px'>$email_id</td>
</tr>
<tr>
<td style='width:150px'><strong>Message: </strong></td>
<td style='width:400px'>$msg</td>
</tr>
</tbody>
</table>
</body>
</html>
";;
if(!$mail->send())
{
$error = "Something went worng. Please try again.";
}
else
{
$result="Thanks\t" .$name. " for contacting us.";
}
}
?>
i have added the phpmailer files to
Please help me in resolving and understanding the issue
Your error message isn't a php error. Enable php errors on your contact.php page and show us the exact error. You can do this by going to your php.ini file and enabling all errors. Also, comment out your mail function for now just to make sure php is getting your form data.
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
PHP Email form with attachment not working by PHPMailer help me what I'm missing. Without form and PHP code it's working in localhost but when i merge with the form it's totally not working..
<?php
require 'vendor/autoload.php';
require 'classes/config.php';
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$mat = $_POST['mat'];
$res = $_POST['res'];
$tech = $_POST['tech'];
$city = $_POST['city'];
$message = $_POST['message'];
$content='<br><br>Name: '.$name.'<br>'.'<br>Mobile: '.$mobile .'<br>Material: '.$mat.'<br>Res: '.$res.'<br>tech: '.$tech.'<br>City: '.$city .'<br>Message: '.$message.'<br>Email: '.$email;
$file_name = ($_FILES['image']['name']);
$file_tmp = ($_FILES['image']['tmp_name']);
$file_path = "upload";
if(move_uploaded_file($file_tmp, "uploads" . $file_name)){
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = Config::SMTP_HOST;
$mail->Username = Config::SMTP_USER;
$mail->Password = Config::SMTP_PASS;
$mail->Port = Config::SMTP_PORT;
$mail->SMTPSecure = 'tls';
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->isHTML(true);
$mail->CharSet = 'UTF-8';
$mail->setFrom('mail');
$mail->addAddress('mail');
$mail->Subject = 'Got a Enquiry';
$mail->Body = $content;
$mail->addAttachment($file_path, $file_name);
if($mail->send()){
echo 'Super';
} else {
echo 'nothing:' .$mail->ErrorInfo;
}
}
}
?>
<form class="montform" id="reused_form" enctype="multipart/form-data" >
<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 bzd-upload-1-1">
<div class="col-lg-12 frm-bg">
<img src="img/upload-icon.png" class="img-responsive">
<p>Drag and Drop Files<br/>
or Upload Here</p>
<p class="file">
<label for="file_attach">
<h6>Browse</h6>
</label>
<input name="image" type="file" id="file" class="feedback-input">
</p>
</div>
</div>
<div class="col-lg-8 col-md-8 col-sm-12 col-xs-12 bzd-form-1-1">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 form-group">
<label class="form-1-1">Name</label>
<p class="name">
<input name="name" type="text" class="feedback-input" required placeholder="Name" id="name" />
</p>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 form-group">
<label class="form-1-1">Email</label>
<p class="email">
<input name="email" type="email" required class="feedback-input" id="email" placeholder="Email" />
</p>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 form-group">
<label class="form-1-1">Mobile No</label>
<p class="name">
<input name="phone" type="tel" class="feedback-input" required placeholder="Phone" id="phone" />
</p>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 form-group">
<label class="form-1-1">Material</label>
<p class="name">
<input name="mat" type="tel" class="feedback-input" required placeholder="Material" id="mat" />
</p>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 form-group">
<label class="form-1-1">Resolution</label>
<p class="name">
<input name="res" type="tel" class="feedback-input" required placeholder="Resolution" id="res" />
</p>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 form-group">
<label class="form-1-1">Technology</label>
<p class="name">
<input name="tech" type="tel" class="feedback-input" required placeholder="Technology" id="tech" />
</p>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 form-group">
<label class="form-1-1">City</label>
<p class="name">
<input name="city" type="text" class="feedback-input" required placeholder="City" id="city" />
</p>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 form-group">
<label class="form-1-1">Message</label>
<p class="text">
<textarea name="message" class="feedback-input" id="comment" placeholder="Message"></textarea>
</p>
</div>
<div class="submit">
<button type="submit" class="button-blue">SUBMIT</button>
<div class="ease"></div>
</div>
</div>
</form>
When I click the submit button it's loading... No error displayed and also i'm not getting mail. I am also search some more articles but still it is not resolved.. kindly help me in this..
You are checking this:
if(isset($_POST['submit'])){
but your form does not contain an input named submit, so this condition will never be true and your sending code will not be run.
You have this submit button:
<button type="submit" class="button-blue">SUBMIT</button>
but it has no name or value attribute. Change it to this:
<button type="submit" name="submit" class="button-blue">SUBMIT</button>
You should check your error_log file it would show you whats going wrong.
Actualy $mail->SMTPDebug = 2; would create troubleshoots of errors too, so maybe your emails going to spam box.
Here is the code working on my localhost :
require_once "vendor/autoload.php";
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
//Or $mail = new PHPMailer;
//Enable SMTP debugging.
$mail->SMTPDebug = 3;
//Set $mail->SMTPDebug = 0; on live sites.
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "useremail#gmail.com";
$mail->Password = "password";
//If SMTP requires TLS encryption then set it
//$mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
//Set TCP port to connect to
$mail->Port = 587;
$mail->From = "name#gmail.com";
$mail->FromName = "Full Name";
$mail->smtpConnect(
array(
"ssl" => array(
"verify_peer" => true,
"verify_peer_name" => true,
"allow_self_signed" => false
)
)
);
//This won't require any server settings on your localhost.
$mail->addAddress("Recepient#email.com", "Recepient Name");
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
Change credentials to yours, and see if your emails not going to spam box.
Change your form line to this : <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="montform" id="reused_form" enctype="multipart/form-data"> you dont have post method in your form, it doesnt post your values to php page.
NOTE : if your php codes are not in same page! change this <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> to your page.php in action.
Change Your upload codes to :
$file_name = ($_FILES['image']['name']);
$file_tmp = ($_FILES['image']['tmp_name']);
$file_path = "upload/";
if(move_uploaded_file($file_tmp, $file_path.$file_name)){
This question already has answers here:
Debugging PHP Mail() and/or PHPMailer
(1 answer)
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
i'm trying to send that form via gmail :
<!-- form -->
<form name="contactForm" id="contactForm" method="post" action="inc/sendEmail.php">
<fieldset>
<div class="form-field">
<input name="contactName" type="text" id="contactName" placeholder="Nom" value="" minlength="2" required="">
</div>
<div class="form-field">
<input name="contactEmail" type="email" id="contactEmail" placeholder="Email" value="" required="">
</div>
<div class="form-field">
<input name="contactSubject" type="text" id="contactSubject" placeholder="Sujet" value="">
</div>
<div class="form-field">
<textarea name="contactMessage" id="contactMessage" placeholder="message" rows="10" cols="50" required=""></textarea>
</div>
<div class="form-field">
<button class="submitform">Submit</button>
<div id="submit-loader">
<div class="text-loader">Sending...</div>
<div class="s-loader">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>
</div>
</fieldset>
</form> <!-- Form End -->
I've checked with the developer tools and everything is send correctly
My php file is in the inc folder :
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
if(isset($_POST["submit"])){
$username = 'myadress#gmail.com';
$password = 'mypass';
$to = $username;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = $username;
$mail->Password = $password;
$mail->SetFrom($_POST["contactName"." "."contactEmail"]);
$mail->Subject = $_POST["contactSubject"];
$mail->Body = $_POST["contactMessage"];
$mail->AddAddress($to);
if(!$mail->Send())
{
echo "Mailer error : " . $mail->ErrorInfo . "<br>";
}}
Actually i can't see any error but i receive no mail so something is wrong
(indentation here isn't correct but it's good in my code)
Actually i googled for some solutions but no one has worked for it, what am i missing ?
Once the form is submitted with correct information a message will appear informing the user that the email was sent. however for some reason the email is not being received by the user.
I have already checked online but to no avail. Can someone kindly help me solve this issue?
The following is the code for both the HTML form and PHP.
<form method="POST" action="contact.php">
<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 inputStyle" id="comments" name="comments" placeholder="Comment" rows="6" required></textarea>
<br>
<div class="row">
<div class="col-sm-12 form-group">
<button class="btn btn-default pull-right" name="submit" type="submit">Send</button>
</div>
</div>
<!-- Contacting Support Team -->
<?php
if(isset($_POST['submit'])){
$email = $_POST['email'];
$name = $_POST['name'];
$comment = $_POST['comments'];
if(!empty($email) && !empty($name) && !empty($comment)){
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->Host = 'ssl://smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->Port = 465;
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '*****'; // SMTP username
$mail->Password = '*****'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = '*****';;
$mail->addAddress($email); // Name is optional
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Test';
$mail->Body = '$comment';
$mail->AltBody = '$comment';
if(!$mail->send()) {
echo '<div class="alert alert-danger alert-dismissible" id="myAlert">
×
<strong> Email not sent </strong> Something went wrong.
</div>';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo '<div class="alert alert-success alert-dismissible" id="myAlert">
×
<strong> Email sent!</strong> We will contact you as soon as possible.
</div>';
}
}
}
?>
</form>
Thanks in advance!
You're setting
$mail->SMTPSecure = 'tls';
but port 465 is the Gmail SSL port. 587 is the TLS port. So either switch the port, or change to using
$mail->SMTPSecure = 'ssl';
i am trying to send mail from one to other using gmail smtp.
the mail sent successfully but without attachment. how can i solve this?
html form:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendresume.PHP" enctype="multipart/form-data">
<div class="row">
<div class="col-sm-5">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="text" name="mobile" class="form-control" required="required" placeholder="Mobile number">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" required="required" placeholder="Email address">
</div>
<p class="lead" style="margin-bottom: 10px;">Resume :</p>
<div class="form-group">
<input type="file" name="file" class="form-control" required="required" >
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg">Send Message</button>
</div>
</div>
<div class="col-sm-7">
<textarea name="messege" id="message" class="form-control" rows="8" placeholder="Note"></textarea>
</div>
</div>
</form>
and php code is:
<?php
include "classes/class.phpmailer.php"; // include the class name
$email = $_POST["email"];
$messege=$_POST['messege'];
$name=$_POST['name'];
$mobile=$_POST['mobile'];
$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; // or 587
$mail->IsHTML(true);
$mail->Username = "abc#gmail.com";
$mail->Password = "mypassword";
$mail->SetFrom("feedback#abc.com");
$mail->Subject = "this is subject";
$mail->Body = "this is body <br/><br/><br/><br/>
name:$name <br/><br/>
email:$email<br/><br/>
mobile:$mobile<br/><br/>
messege:$messege</b>";
$mail -> Addattachment('here is problem');
$mail->AddAddress("mymail#gmail.com");
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
echo "mail sent";
}
?>
this mail is sent successfully but I want it with attachment. file store in directory is not important. hope this problem solve immediately. thanks in advance because I never learned an used php before.
Here is an example to send attachment using PHPMailer class:
http://tournasdimitrios1.wordpress.com/2011/11/08/a-simple-example-sending-email-with-attachment-using-phpmailer/