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
Related
I am new at PHP. I am using PHPMailer to send emails but unable to do so. I tried configuring it for gmail and it worked But not working when tried to send msg from remote host and for the given domain.
Here is the code.
<?php
require("PHPMailerAutoload.php"); // path to the PHPMailerAutoload.php file.
$mail = new PHPMailer();
//$mail->IsSMTP();
$mail->Mailer = "smtp";
$mail->Host = "175.000.000.000"; //Sample Host
$mail->SMTPDebug = 1;
$mail->Port = "25"; // 8025, 587 and 25 can also be used. Use Port 465 for SSL.
$mail->SMTPAuth = true;
//$mail->SMTPSecure = 'ssl';
$mail->Username = "maaz#mcbah.com";
$mail->Password = "password";
$mail->From = "maaz#mcbah.com";
$mail->FromName = "Maaz Khan";
$mail->AddAddress("mcbah.all#mcbah.com", "all");
$mail->AddReplyTo("fundprices#mcbah.com", "Fund");
$mail->Subject = "Hi!";
$mail->Body = "Hi! How are you?";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
echo 'Not sent: <pre>'.print_r(error_get_last(), true).'</pre>';
exit;
} else {
echo 'Message has been sent.';
}
This code may help you
$mail->Host = "smtpout.secureserver.net";
Your host name should be like mydomain.com
$mail->Port = 465;
I am trying to send SMTP secured mail with attachment using PHPMailer.
I made this function with PHPMailer library
public function sendCsv($subject,$body,$path,$mail_to,$from_name,$from_mail,$replyto){
require getcwd() .'/lib/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'tester#mydomain.com';
$mail->Password = '**************';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom($from_mail, $from_name);
$mail->addAddress($mail_to);
$mail->addReplyTo($replyto, 'no reply');
$mail->addAttachment($path);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
echo $error;
} else {
$sucess = 'Mail sent!';
echo $sucess;
}
}
Now if I comment the line $mail->isSMTP(); it's working fine . but I think it's not SMTP secured. Else I am getting this message: "Mail error: SMTP connect() failed. "
I searched for this problem but didn't get the proper answer to what I am looking for. Please help me.
I am working in a dev server with HTACCESS protection
Well I have found the details to fix the issue here https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting .
I have just Turned on less secure apps https://www.google.com/settings/security/lesssecureapps from my gmail account.
This takes few minutes or an hour to active.
My current code :
public function sendCsv($subject,$body,$path,$mail_to,$from_name,$from_mail,$replyto){
require getcwd() .'/lib/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
//$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'mymail#gmail.com';
$mail->Password = 'mypass';
$mail->setFrom($from_mail, $from_name);
$mail->addAddress($mail_to);
$mail->addReplyTo($replyto, 'no reply');
$mail->addAttachment($path);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
echo $error;
} else {
$sucess = 'Mail sent!';
echo $sucess;
}
}
Else you have to Setup a app from console.developers.google.com
Follow this guide : https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2
<?php
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->Host = 'smtp.domainename.com';
$mail->Port = 465;
$mail->Username = 'info#domainename.com';
$mail->Password = 'password for my email';
$mail->setFrom('info#domainename.com');
$mail->addAddress('me#gmail.com');
$mail->Subject = 'The needed information for next login!';
$mail->Body = 'my message';
//send the message, check for errors
if ($mail->send()) {
echo"success";
}
?>
I used phpmailer, it was working only in my gmail account but if i tried to use cpanel email(eg:me#domainname.com) it doesn't working till now and i have phpmailer forder.
Try to set smtp host: mail.domainname.com
By default most cpanel based hostings set SMTP servers address like mail.domainname.com and mostly use the same address in MX record
You can get Your MX record by using this service: mxtoolbox
Remove:
$mail->SMTPSecure = 'ssl';
Add:
$mail->Port = 25;
$mail->SMTPDebug = 3;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
debug will verbose the actions, ErrorInfo show why it's not sending.
I am trying to configure PHPMailer in order to have it working ona Windows IIS server using php scripts.
The application I have is written in PHP so we expected we could easily use PHPMailer to send emails.
Well, that doesn't work.
We have tried configuring it as SMTP, but we still get error in configuration.
Here it is our script:
date_default_timezone_set('Etc/UTC');
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 1;
//$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "account#gmail.com";
$mail->Password = "gmailPWD";
$mail->setFrom('info#website.com', 'The Website');
$mail->addAddress($to, '');
$mail->Subject = $subject;
$mail->msgHTML($message);
if (!$mail->send()) {
$errors = "Errori Mailer: " . $mail->ErrorInfo;
} else {
$errors = "<h5>Sent!</h5>";
}
Any clue? How can that be used on windows server?
Thank you
EDIT
So, it was a problem of Gmail and its security settings. I followed this post: https://stackoverflow.com/a/25175234/1873501 and everything went smooth!
I just wanted to share it.
You've downloaded and extracted the class I am assuming. Maybe you're not autoloading? Make sure you include:
require '/path/to/PHPMailerAutoload.php';
It should be fine on IIS/Windows Server. See some of the examples here in the documentation (and in the examples folder): https://github.com/PHPMailer/PHPMailer
Also, if you are getting any errors make sure you list them so we know how to help you further.
function sendMail($request) {
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 4;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'yourmail#gmail.com;
$mail->Password = 'yourgamilPass';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('yourgmail#gmail.com', 'Title');
$mail->addAddress(emailsendingto#gmail.com);
$mail->addReplyTo('yourgmail#gmail.com');
$mail->isHTML(true);
$mail->Subject = '$the subject of the e-mail';
$mail->Body = 'The body of the email';
$mail->AltBody = 'Alternative'; // this is mostly sent to your own mail
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
I don't know if my code is the issue or if I had configured Zoho's SMTP's settings incorrectly.
Basically I want the ability to dynamically send emails with a simple php function like for example
phpMail("to#example.com", $subject, $body, "from#example.com", "replyto#example.com");
This is my PHPMailer.php script (it sees the function and its settings)
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.zoho.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '**REMOVED**'; // SMTP username
$mail->Password = '**REMOVED**'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->isHTML(true); // Set email format to HTML
// SYTAX: phpMail($from, $reply, $to, $subject, $body);
function phpMail($to, $subject, $body, $from = "from#example.com", $reply = "replyto#example.com") {
if (isset($from))
$mail->From = $from;
$mail->FromName = "testing";
if (isset($to))
$mail->addAddress($to);
if (isset($reply))
$mail->addReplyTo($reply);
if (isset($subject))
$mail->Subject = $subject;
if (isset($body))
$mail->Body = $body;
$mail->AltBody = $body;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
Now the issue with this is that, I'm not receiving emails nor am I receiving any errors / messages but it's also not sending me an email.
When you make a PHP function, make sure your objects aren't outside of your function.
My error wasn't that though, it was basically my own stupidity which had nothing to do with the code but more of my "form post data".
The following configuration works for me using zoho mail.
Give it a try:
$mail=new JPhpMailer;
$mail->IsSMTP();
$mail->Host='smtp.zoho.com';
// Enable this option to see deep debug info
// $mail->SMTPDebug = 4;
$mail->SMTPSecure = 'ssl';
$mail->Port='465';
$mail->SMTPAuth=true;
$mail->Username='your_email_address';
$mail->Password='your_eamil_address_password';
$mail->isHTML(true);
$mail->SetFrom('your_email_address','Your Name');
$mail->Subject='PHPMailer Test Subject via smtp, basic with authentication';
$mail->AltBody='To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML('<h1>JUST A TEST!</h1>');
$mail->AddAddress('destination_email_address','John Doe');
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}