I've created an contact us form using PHP Mailer, however the problem is when I received the email the name before you open it is me and I can't figured out how to fix it. Also I appreciate it if someone explain to me what is the different between Username and the add Address. Thank you in advance.
Here is the me I'm referring to
<?php
use 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 = "";
$mail->Password = '';
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
$mail->SetFrom("$email ", "$name");
//email settings
$mail->isHTML(true);
$mail->setFrom($_POST['email'], $_POST['email']);
$mail->addAddress("");
$mail->Subject = ("$email ($subject)");
$mail->Body = $body;
if($mail->send()){
$status = "success";
$response = "Email is sent!";
}
else
{
$status = "failed";
$response = "Something is wrong: <br>" . $mail->ErrorInfo;
}
exit(json_encode(array("status" => $status, "response" => $response)));
}
You are sending mail from the same email address to the same email address.
So, many email clients identify yourself as me.
You will have to send using another email address, and you will be idenfied with your email/name.
As Jesse said: You can also send mail to another email, and it will usually not show me.
Make a new Email address / use another Email address in script
Related
i am having some trouble with phpmailer and ajax. Im quite new to ajax and dont fully understand it yet so this might have a quite obvious fix but here we go.
So when i just put my php mailer script in a page and visit it with set values i can send the mail. but whenever i try to send my form data to the script something doesnt work.
This is my jquery
$(document).ready(function(){
$(".contact-form").on("submit",function(e){
e.preventDefault();
function validateEmail($email) {
var emailReg = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/;
return emailReg.test( $email );
}
var valid = true;
if(!$("#name").val()) {
$("#userEmail").css('border','2px solid #FFFFDF');
valid = false;
}
if( !validateEmail($(' #email').val())) {
valid = false
}
if(!$("#subject").val()) {
$("#subject").css('border','2px solid #FFFFDF');
valid = false;
}
if(!$("#message").val()) {
$("#message").css('border','2px solid #FFFFDF');
valid = false;
}
//send data to php file
var isValid;
isValid = valid
if(isValid) {
var name = $("#name").val();
var email = $("#email").val();
var subject = $("#subject").val();
var message = $("#message").val();
jQuery.ajax({
url: "includes/sendmail.inc.php",
type : "POST",
data:{name:name,email:email,subject:subject,message:message},
success:function(data){
$(".err_msg").text("Send!");
$(".err_msg").css('background-color', 'green')
$(".err_msg").show()
},
error:function (){}
});
} else {
$(".err_msg").text("Oops! check your input.");
$(".err_msg").css('background-color', 'red')
$(".err_msg").show()
}
});
});
This is my php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// 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;
// Load Composer's autoloader
require 'vendor/autoload.php';
$name = $_POST['name'];
$mail = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'mail.example#example.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example#example.com'; // SMTP username
$mail->Password = 'example123'; // 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
//Recipients
$mail->setFrom( $email , $name);
$mail->addAddress('example#example.com', 'John Doe'); // Add a recipient
$mail->addReplyTo($email , $name);
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
so this is what i have so far i hope you guys can figure out what i did wrong because i have been stuck on this for about two days now.
greetz Floris
yes i figured it out guys thanks ! so what went wrong is that for some reason it wouldnt send because the FROM email adress was a gmail account and i am using my own domain smtp that was causing it appearantly
I'm new to the world of programming. My code is shown below.
First question: how do I format the body text? For example, not having all requests in a row but one below the other, etc.
Second question: How can I insert the code to be able to load a file in my contact form, the html code is already present and it works but on the javascript side I can't understand where to insert it.
Last question: I have a news letters acceptance field how do I insert a field that tells me if the user has accepted the news letter?
Thanks a lot to everyone!
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$telefono = $_POST["telefono"];
$indirizzo = $_POST["indirizzo"];
$civico = $_POST["civico"];
$citta = $_POST["citta"];
$provincia = $_POST["provincia"];
$cap = $_POST["cap"];
$newsletters = $_POST["newsletters"];
$body = "Name:" .$name . "<br>Email:" . $email .
"TelefonoLocale:" . $telefono . "Indirizzo:" . $indirizzo . "Civico:" . $civico . "Città:" . $citta. "Provincia:" . $provincia . "Cap:" . $cap . "newsletters:" . $newsletters;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'xxxxxxx'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxxxxx'; // SMTP username
$mail->Password = 'xxxxxxxxxx'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = xxxxxx; // TCP port to connect to
//Recipients
$mail->setFrom('xxxxxx', $name);
$mail->addAddress('xxxxxxxx'); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = $body;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo '<script>
alert("Messaggio inviato correttamente");
window.history.go(-1);
</script>';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
I tried many site but I can't find the result what I want to do.so anybody can please help me to solve this ?
My problem is :
I want to send forgot password email to client.which is expire after 15 min.Here is my code
function send_email($to, $subject, $message)
{
global $from_email;
global $from_pwd;
global $host;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->ContentType = "text/html";
$mail->Host = $host;
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Username = $from_email;
$mail->Password = $from_pwd;
$mail->From = $from_email;
$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->Body = $message;
if(!$mail->Send()) {
die('Message was not sent.'.$mail->ErrorInfo);
}
}
And I also Added This code
//Send Welcome Email
require_once("classes/class.phpmailer.php");
send_email($_POST["email"],"Thanks For Register", "Php Link is here");
There are so many ways to achieve this like:
While sending the email make an entry in the datatabse table with the time on email send and put the check to compare the time with given time limit and show the message if time exceeds.
Send an extra pramater in the link that contains timestamp or encrypted time in it, compare it with the time of clicked time and show the message.
I'm trying to get my app run into my if statement but it went down to else statement which is not what I want. callback data is "Pass" which is correct for my if condition but it still ran down to else. Please take a look at my code.
js file
$http.post(path, postdata
).success(function (data, status, headers, config) {
//if (data) {
// $scope.PostDataResponse = data;
if (data === "Pass"){
setCookie("Username", $scope.formdata.Username);
setCookie("cafe_id", $scope.formdata.cafe_id);
console.log(getCookie("Username"));
alert("ลงทำเบียนสำเร็จ !");
$location.url('/viewSaveCafeDetail');
//console.log(data);
//alert("สมัครสมาชิกสำเร็จ");
//$scope.insertcafe();
//$scope.sendEmail();
// $scope.reset();
// $scope.getData();
}else{ <--- ran down to else statement
alert(data); <--- callback value is "Pass" which is match with my if condition.
}
PHP file
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Methods : GET,POST,PUT,DELETE,OPTIONS");
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Requested-With, X-YOUR-CUSTOM-HEADER');
header("Content-Type : application/json");
header("Accept : application/json");
$serverName = "localhost";
$userName = "root";
$userPassword = "";
$dbName = "middlework";
$conn = new mysqli($serverName,$userName,$userPassword,$dbName);
mysqli_set_charset($conn,"utf8");
session_unset();
session_start();
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$strSQL = "INSERT INTO users ";
$strSQL .="(Username,Password,Firstname,Lastname,Email,Tel,AccountStat,VerifyCode,Verifystat) ";
$strSQL .="VALUES ";
$strSQL .="('".$request->Username."','".$request->Password."','".$request->Firstname."' ";
$strSQL .=",'".$request->Lastname."','".$request->Email."','".$request->Tel."','User','".session_id()."','None' ) ";
mysqli_query($conn,$strSQL) or die(mysqli_error($conn));
$insertcafe = "INSERT INTO cafe (cafe_id,Username,CafeName) VALUES ('1' , '".$request->Username."', '".$request->CafeName."')";
require_once('PHPMailer/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->IsHTML(true);
$mail->CharSet = "utf-8";
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "kitsakorn.p55#rsu.ac.th"; // GMAIL username
$mail->Password = "1100501068349"; // GMAIL password
$mail->From = "kitsakorn.p55#rsu.ac.th"; // "name#yourdomain.com";
$mail->FromName = "ThaiCoffeeShopOnline"; // set from Name
$mail->Subject = "ยืนยันการสมัครสมาชิก ThaiCoffeeShopOnline";
$mail->Body = "ขอขอบคุณที่สมัครเป็นสมาชิกกับเรา กรุณาคลิก Url ด้านล่างเพื่อทำการ Activate บัญชีของคุณ</br>
http://localhost/activate.php?sid=".session_id()."&uid=".$request->Username."</br></br> ThaiCoffeeShop.com";
$mail->AddAddress($request->Email); // to Address
if($conn->query($insertcafe))
{
if (!$mail->send()) {
//echo "ไม่สามารถส่ง email: " . $mail->ErrorInfo;
echo "EmailFail";
} else {
echo "Pass ";
//json_decode();
}
//echo "Save Cafe Done.[".$insertcafe."]";
}
else
{
echo "";
//echo mysqli_error($conn);
//echo "ไม่สามาถบันทึกข้อมูล[".$insertcafe."]";
}
$conn->close();
?>
Your PHP file has echo "Pass "; which writes "Pass" followed by a space which you are comparing against the string "Pass" without a space.
In addition, you might also have a blank line at the end of your PHP file which will also be included in the output. This can be worked around by removing the ?> tag from the end.
When I am trying to send multiple emails using php mailer, it displays all recipients addresses. All the mail recipients can find others email addresses whom I sent. Is it possible to remove others recipient addresses? Need to show the recipient only its recipient address not others.
Here is my Coding
$to_array = explode(",", $_REQUEST['Recipient']);
$mail->From = 'automail#domain.com';
$mail->FromName = 'Test Admin';
foreach ($to_array as $address) {
$mail->AddAddress($address);
}
$mail->Subject = "$subject";
$mail->MsgHTML($body);
$mail->Send();
If you dont want other users to see your recipients you can simply do a bcc
$to_array = explode(",", $_REQUEST['Recipient']);
$mail->From = 'automail#domain.com';
$mail->FromName = 'Test Admin';
foreach ($to_array as $address) {
$mail->AddBCC($address);
}
$mail->Subject = "$subject";
$mail->MsgHTML($body);
$mail->Send();
If you just want recipients email to display and not others you need to send them individually this way
$to_array = explode(",", $_REQUEST['Recipient']);
for($i=0; $i<count($to_array); $i++){
$mail->From = 'automail#domain.com';
$mail->FromName = 'Test Admin';
$mail->AddAddress($to_array[$i]);
$mail->Subject = "$subject";
$mail->MsgHTML($body);
$mail->Send();
}
First case wont display any TO email. Second case it will display only the recipient email.