<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer-master/src/Exception.php';
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = "smtp";
$mail->SMTPDebug = 1;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Host = "smtp.gmail.com";
$mail->Username = "myemail";
$mail->Password = "myemailpassword";
if(isset($_POST['submit'])){
$from = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mail->IsHTML(true);
$mail->AddAddress("myemail", "name");
$mail->SetFrom($from);
$mail->AddReplyTo($from);
$mail->Subject = $subject;
$content = $message;
$mail->MsgHTML($content);
if(!$mail->Send()) {
echo "Error while sending Email.";
var_dump($mail);
}
else {
echo "Email sent successfully";
}
}
?>
the code works if I enter the details myself rather than getting them from my html form. for example for $mail->SetFrom("myemail", "my name")
tried defining the variable when I call it in the mail function that didn't work either
Related
I tried many sites but I can't find the result of what I want to do. so anybody can please help me to solve this? My problem is this code belongs PHPMailer library. how I redirect to the homepage after the email submission.
My home page is index.html
<?php
use PHPMailer\PHPMailer\PHPMailer;
if(isset($_POST['name']) && isset($_POST['email'])){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$body = $_POST['body'];
require_once "PHPMailer/PHPMailer.php";
require_once "PHPMailer/SMTP.php";
require_once "PHPMailer/Exception.php";
$mail = new PHPMailer();
//smtp settings
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "website#gmail.com";
$mail->Password = '123';
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
//email settings
$mail->isHTML(true);
$mail->setFrom($email, $name);
$mail->addAddress("website#gmail.com");
$mail->Subject = ("$email ($subject)");
$mail->Body = $body;
$mail->send();
}
?>
After your $mail->send(); function, you could try using the PHP in-built function header();
So for instance in your case:
header('Location: /');
This should redirect to home page after submission, where / is the main root of home page.
Some more information on header(): https://www.php.net/manual/en/function.header.php
You should handle your $mail->send() to handle the success and failed response.
Would write the code like this:
<?php
use PHPMailer\PHPMailer\PHPMailer;
if(isset($_POST['name']) && isset($_POST['email'])){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$body = $_POST['body'];
require_once "PHPMailer/PHPMailer.php";
require_once "PHPMailer/SMTP.php";
require_once "PHPMailer/Exception.php";
$mail = new PHPMailer();
//smtp settings
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "website#gmail.com";
$mail->Password = '123';
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
//email settings
$mail->isHTML(true);
$mail->setFrom($email, $name);
$mail->addAddress("website#gmail.com");
$mail->Subject = ("$email ($subject)");
$mail->Body = $body;
if($mail->send()){
header('Location: your_url_here.php');
}else{
//do something when email delivery fails.
}
}
?>```
I have developed a site in PHP and added a PHPMailer function for my contact us page but the mailer functionality is not working. Contacted with support team but they didn't help me tried a lot with that.
<?php
if(isset($_POST['submit_contact']))
{
require 'PHPMailer/PHPMailerAutoload.php';
$to = "gmail#gmail.com";
$name = $_POST['uname'];
$email = $_POST['email'];
$phone = $_POST['phonenumber'];
$companyname = $_POST['companyname'];
$country = $_POST['country2'];
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "relay-hosting.secureserver.net";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "xxxx#gmail.com";
$mail->Password = "PASSword1#3";
$message = array();
$message[] = 'Name : '.trim($name).' ';
$message[]='Phone : '.trim($phone).' ';
$message[]='Email : '.trim($email).' ';
$message[]='Company Name : '.trim($companyname).' ';
$message[]='Country : '.trim($country).' ';
$message = implode('<br/>', $message);
$mail->SetFrom($email);
$mail->Subject = 'Here is the subject';
$mail->Body = $message;
$mail->AddAddress($to);
if(!$mail->send()) {
$msg = "Error while sending email";
$msgclass = 'bg-danger';
header("Location: /");
die();
}
else {
$msg = 'A mail with recovery instruction has sent to your email.';
$msgclass = 'bg-success';
}
}
?>
u used gmail,u should set host to smtp.gmail.com, SMTPSecure to ssl,SMTPAuth to true,port to 465,u alse should use the app password,that u can see https://support.google.com/mail/answer/7126229
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = false;
$mail->SMTPSecure = 'none';
$mail->Host = "localhost";
$mail->Port = 25;
$mail->IsHTML(true);
Solved the issue with this update in SMTP
I am trying to send emails using SMTP Server in Core HP. I am getting a blank page not displaying any errors. Can any one help me out? what i am doing wrong? Unable to check the issue as well!
<?php ob_start();
if(isset($_POST['submit_contact'])) {
require 'PHPMailer/PHPMailerAutoload.php';
$to = "abc#gmail.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$txt = $_POST['comment'];
$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 = "abc#gmail.com";
$mail->Password = "password1#3";
$mail->SetFrom('$email');
$mail->Subject = $subject;
$mail->Body = $txt;
$mail->AddAddress($to);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
}
?>
is your source code located at public_html folder?
In that case you have to include like below
require 'aaa/PHPMailer/PHPMailerAutoload.php';
<?php
require 'PHPMailerAutoload.php';
//echo !extension_loaded('openssl')?"Not Available":"Available <br/>";
$name = $_POST['username'];
$email = $_POST['email'];
$number = $_POST['phone'];
$profession = $_POST['profession'];
$to = 'example#gmail.com';
$subject = 'user registration';
$phone = "phone number:".$number;
$message = "client details:"."\n"."Name:".$name."\n"."email:".$email."\n"."phone number:".$number."\n"."profession:".$profession;
$headers = "From:".$email;
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'ssl://smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'example#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom($email, $name);
$mail->Subject = $subject;
$mail->Body = $message;
if($mail->send()) {
header("Location: ../../thankyouNew.html");
}
else {
header("Location: ../../somethingWrong.html");
}
?>
code is executing else block, i want to send mail to example#gmail.com and return user to the thankyou.html page after the mail function is executed.I am new to this php and i would highly appreciate the help thank you in advance.
forget the below lines........
You don't actually specify where you want to send the email. You need to use the addAddress() method, as shown below. This method requires one parameter, but you may supply two - in the same way your setFrom() method has; first the target address, then an optional display name.
$mail = new PHPMailer;
// ...
$mail->setFrom($email, $name);
$mail->addAddress($to); // Add this method to specify a recipient
$mail->Subject = $subject;
$mail->Body = $message;
if($mail->send()) {
// ...
}
// ...
This is my code:
<?php
require_once "PHPMailer/PHPMailerAutoload.php";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com"; //gethostbyname('smtp.gmail.com');
$mail->SMTPAuth = true;
$mail->SMTPDebug = 3;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->Username = "ku***wa***ar***#gmail.com";
$mail->Password = "t***r9***5******7";
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress($mail->Username);
$mail->Subject = 'Contact form query / feedback';
$mail->Body = "
<div><span>Name : </span><span>{$name}</span></div>
<div><span>Email : </span><span>{$email}</span></div>
<div><p>{$message}</p></div>
";
$mail->isHTML(true);
if ($mail->send()){
echo "Your feedback/query is sent!";
}else{
echo "Error! Unable to forward your request.<br> Pleas try again later!";
}
Note: I have used " GMail " as my SMTP server and SMTPSecure is " ssl " and port is "465" and username & passwords are my GMail username & password
The Error Message is:
Screen Shot : https://s16.postimg.org/4hgsh9d51/smtp.png
Here is your code modified
<?php
include "PHPMailer_5.2.4/class.phpmailer.php";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com"; //gethostbyname('smtp.gmail.com');
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->isHTML(true);
$mail->Username = "hari.andoidsaiss#gmail.com";
$mail->Password = "supreme#12#";
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress($mail->Username);
$mail->Subject = 'Contact form query / feedback';
$mail->Body = "
<div><span>Name : </span><span>{$name}</span></div>
<div><span>Email : </span><span>{$email}</span></div>
<div><span>Message : </span><p>{$message}</p></div>
";
if ($mail->send()){
echo "Your feedback/query is sent!";
}else{
echo "Error! Unable to forward your request.<br> Pleas try again later!";
}