PHPMailer still sending emails infinitely - php

I am using PHPMailer to send emails.
When I execute the code that sends an email to X#Y.Z, I still receive this email, not only onetime, but each time I refresh my inbox I receive many copies of it!
Note: I execute the code one time only!!
So what's wrong?
this is my code:
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$body = "bla blas";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "xxx.xxx.xxx.xxx";
$mail->Port = 25;
$mail->Username = "****";
$mail->Password = "****";
$mail->SetFrom('z#z.z');
$mail->AddReplyTo("z#z.z");
$mail->Subject = "test";
$mail->MsgHTML($body);
$mail->AddAddress('X#Y.Z');
if(!$mail->Send()) {
//echo "Mailer Error: " . $mail->ErrorInfo;
} else {
//echo "Message sent!";
}
Thanks!

Related

PHPMailer does not execute code after $mail = new PHPMailer

I have made a website on my localhost. After everything was ready to go I hosted the website. However, now PHPMailer seems to cause the trouble. Mails are not sending and it seems like PHP code does not execute after $mail = new PHPMailer command. Here is part of the code:
if($conn->query($sql)){
require_once '/home/ziptie/public_html/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = true;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mygmail#gmail.com'; // your email id
$mail->Password = 'mypassword'; // your password
$mail->SMTPSecure = 'tls';
$mail->Port = 587; //587 is used for Outgoing Mail (SMTP) Server.
$mail->setFrom('mygmail#gmail.com', 'ZIPTIE');
$mail->addAddress($email); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$v="http://ziptie.rs/verification-page.php?sifra=$token";
$body = file_get_contents('/home/ziptie/public_html/verificationemail.html');
$body = str_replace('promenljiva', $v, $body);
$mail->Subject = 'Verifikacija kupca, ZIPTIE';
$mail->Body = $body;
$mail->addAttachment("naruzbine ".date("d-m-Y").".zip");
$mail->send();
header("Refresh:0; url=https://www.ziptie.rs/verification-page.php");
//echo '<script>alert("Verifikacioni link je poslat, proverite Vašu mejl adresu.")</script>';
/*if(!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}*/
}
else{
echo $conn->error;
}
Does anybody know the solution? Thanks!
found solution in upgrading to PHPMailer 6.3

Not able to send email via PHP using office 365 smtp. Works on localhost but fails online

$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$message = $_POST["message"];
$mailbody="";
$mailbody.="Hello,<br/><br/>You have a new contact on your website:<br/><br/>";
$mailbody.="Name: ".$name."<br />\r\n";
$mailbody.="Email: ".$email."<br />\r\n";
$mailbody.="Phone: ".$phone."<br />\r\n";
$mailbody.="Message: ".$message."<br />\r\n";
require_once('./bat/phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
// SMTP Configuration
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.office365.com"; // SMTP server
$mail->Username = "email#domain.com";
$mail->Password = "****";
$mail->Port = 587; // optional if you don't want to use the default
$mail->IsHTML(true);
$mail->From = "email#domain.com";
$mail->FromName = "Website Name";
$mail->Subject = "Subject";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($mailbody);
// Add as many as you want
$mail->AddAddress("email#domain.com", "Name");
if(!$mail->Send()) {
//header("Location:contact.php?sent=0#sent");
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
header("Location:contact.php?sent=1#sent");
}
When I test on localhost the email sends successfully and gets delivered.
When I test online it throws the following error: Mailer Error: SMTP connect() failed.
I tried to remove $mail->IsSMTP(); as mentioned in one of the suggestions. The scripts runs online and returns true but the email never gets delivered.

PHPMailer in localhost showing an error Called Mail() without being connected. how can I fix it?

<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
// ---------- adjust these lines ---------------------------------------
$mail->Username = "mymail#gmail.com"; // your GMail user name
$mail->Password = "xxxxxxxxxx";
$mail->AddAddress("yourmail#yahoo.com"); // recipients email
$mail->FromName = "abc"; // readable name
$mail->Subject = "Sample Email";
$mail->Body = "Here is the message you want to send to your friend.";
//-----------------------------------------------------------------------
$mail->Host = "ssl://smtp.gmail.com"; // GMail
$mail->Port = 25;
$mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->From = $mail->Username;
if(!$mail->Send())
echo "Mailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
?>
Mailer Error: The following From address failed: mymail#gmail.com : Called Mail() without being connected.
How can I fix it??

php mailer sender form user

i have phpmailer and i can send email via php page without any problem
but the sender automatically by username it i am use in smtp server
i want take sender email from user who write message not from default sender
and this is form code
<?php
require '../../PHPMailer/PHPMailer-master/PHPMailerAutoload.php';
$name = $_POST['name'];
$Institute = $_POST['Institute'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Debugoutput = 'html';
//$mail->SMTPDebug = true;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "MyGmail";
$mail->Password = "MyGmailPass";
$mail->setFrom('Mygmail', $name);
$mail->addReplyTo('MyGmail', 'First Last');
$mail->addAddress('MyEmail', 'Nasser');
$mail->Subject = 'Database Site Reminder';
$mail->Body = ($message);
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
i am put `$mail->setFrom('Mygmail', $name); this like
$mail->setFrom($email, $name);
because take sender email from user , and i get Message sent
but message not arrive to my email
i cant find any solution... please assist me
thanks ...
$mail = new PHPMailer();
$body = "Body text goes here";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "Gmail Id"; // GMAIL username
$mail->Password = "PaSsWoRd"; // GMAIL password
$mail->SetFrom('fromemail#gmail.com', 'User');
$mail->AddReplyTo("fromemail#gmail.com","User");
$mail->Subject = "Subject Goes Here";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress('toemail#gmail.com', 'User');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo("Success");
}
try this code... its working....
If you are using PHP Mailer from Github,
then you can set the Sender name by,
$mail->SetFrom("info#mibckerala.org", "MIBC");

PHP Mailer SMTP ERROR

Hi I am using phpMailer For sending email. Its working fine on server, but when i am trying to send mail from my local machine(localhost) it giving me error. I am using GMAIL smtp
<?
require("lib/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "XXX#XXX.com"; // SMTP username
$mail->Password = "XXXXXXX"; // SMTP password
$mail->From = $email;
$mail->AddAddress("XXXXXX#XXX.com", "XX XX XX");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
}
?>
and the error is
SMTP Error: Could not connect to SMTP host. Message could not be sent.
Mailer Error: SMTP Error: Could not connect to SMTP host.
It's working fine in my server.
Okay I got it in local here is the code that I used
<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username#doamin.com"; //Reply to this email ID
$email="username#domain.com"; // Recipients email ID
$name="name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

Categories