Yii, PHPMailer Cannot be loaded - php

im trying for days many ways to send email from a local server using gmail.. i once made it with a yii extension but after a while, its not working, i tried to add the whole PHPMailer and make the mailing with the proper way.
this is my code:
static function gmail($email)
{
$mail = new phpmailer();
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Username = "mail#gmail.com";
$mail->Password = "password"; //best to keep this in your config file
$mail->Subject = 'subject';
$mail->Body = 'message';
$mail->addAddress($email);
$mail->send();
}
i downloaded the PHPMailer library from Github and extracted all to components folder.
The way how i added to config file is like this:
'import'=>array(
'application.models.*',
'application.components.*',
'application.components.PHPMailer.*'
),
on the first try i get this error include(phpmailer.php): failed to open stream: No such file or directory
Update: i forgot to say, the way how i access this function is this way Mailer::gmail('$this->email'); where Mailer.php has the gmail function.

Double check your php.ini file to make sure phpmailer is located in the include_path because other than that based on the code you provided everything looks good.
(Sorry, would have put this in the comments but I am still unable to comment just yet.)

I'm not quite sure, I am using Yiis phpmailer extension for several month and it always worked very well.
However, try this with the Github thing:
require_once <PATH TO PHPMAILER> . 'class.phpmailer.php';
$mail = new PHPMailer();
[...] Rest of your code here
Replace with the absulute path to your class.phpmailer.php

i was able to send the mail by checking on gmail history security, it was showing that someone was trying to access my account with invalid authorization, i ended up by using YiiMailer, its a bit confusing about gmail.
the code was like this:
$mail = new YiiMailer();
//$mail->clearLayout();//if layout is already set in config
$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 = "account#gmail.com";
$mail->Password = "password";
$mail->setFrom('no-reply#strandedgrounds.sr', 'Stranded Grounds');
$mail->setSubject('Recuperacion de contraseƱa');
$mail->AddAddress("mail#hotmail.com");
$mail->send();

Related

why does "Email failed to send: SMTP connect() failed." still appear after having enabled less secure apps

My PHPMailer will not connect to my gmail account properly although I have enabled less secure apps and have checked my username and password several times.
I have enabled less secure apps to access my gmail account "tokens.tts#gmail.com". I have also tried to use both ssl and tls but neither seem to work. I have looked at several videos online and they all seem to mention enabling the less secure apps option but nothing else.
<?php
use PHPMailer\PHPMailer\PHPMailer;
$name = "Michiel Olieslagers";
$email = "tokens.tts#gmail.com";
$subject = "This is just a regular email";
$body = "I am writing this email to see whether this actually function propperly.";
require_once "PHPMailer/PHPMailer.php";
require_once "PHPMailer/SMTP.php";
require_once "PHPMailer/Exception.php";
$mail = new PHPMailer();
// $mail->isSMTP();
// $mail->Host = "smtp.gmail.com";
// $mail->SMTPAuth = true;
// $mail->SMTPSecure = "tls";
// $mail->Username = "tokens.tts#gmail.com";
// $mail->Password = "hqyzZYax";
// $mail->Port = "587";
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "tokens.tts#gmail.com";
$mail->Password = "********";
$mail->Port = "465";
$mail->isHTML(TRUE);
$mail->setFrom($email, $name);
$mail->addAddress("micholieslagers#gmail.com");
$mail->Subject = $subject;
$mail->Body = $body;
if ($mail->send()){
echo "Email is sent";
}
else { echo "Email failed to send: ". $mail ->ErrorInfo; }
?>
I should be receiving a test email at my second account "micholieslagers#tts.edu.sg" however this does not happen.
Looks like the new process for GMTP SMTP has a compulsion.
Please follow the below steps to achieve the same.
Make sure you enable 2-step authentication process with your mobile number before proceeding below steps.
Go to google account from this link.
You will see settings page with tabs on left panel. Click on security tab.
Click on app passwords link. Make sure here you enabled 2-step verification.
You will see select the app and device you want to generate the app password for.
Select app as mail and select your relevant device or you can choose other at last.
Click on the generate button, you will get the generated password. Copy and save it somewhere as it's one-time password otherwise you have to generate it again by creating new app password.
Now STMP configuration,
Host - smtp.gmail.com
Username - your email address for which you generated the app.
Password - Your generated password
SMTPSecure - TLS or SSL whichever work
Port - 465 for SSL and 587 for TLS
This should work as I am still using this configuration.

PHPmailer issue - php coding above html but not receiving emails

I'm trying to use phpmailer to work alongside a contact form I have made. I was originally using simple php coding for the form, but this would not work on the web company's server, so they asked me to use phpmailer instead. I have changed the coding a little bit to try and integrate it into the contact page that has the contact form.
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.live.com'; // Specify main and backup SMTP servers
$mail->Username = "myemail#hotmail.com";
$mail->Password = "mypassword";
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->addAddress('stacey_victoria#hotmail.com', 'Sender'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Message from Website';
$mail->Body = $message; $name;
$mail->From = $email;
if($_POST){
$feedback = 'Thanks for your message';
}
?>
The contact page appears as normal, and once the submit button has been pressed, the echo-feedback message is shown. However, no email is sent through.
I edited the "Simple Example" found here: https://github.com/PHPMailer/PHPMailer and uploaded the php file to the server and this worked fine so I know the smtp information I have used is correct and working.
Is it even possible to put phpmailer coding above the html coding?
If so, what am I doing wrong?
You are not sending the email...
Use :
if($mail->Send())

SMTP Error: Could not authenticate. Sign-in attempt prevented

I am trying to send email by phpmailer class.But i see this problem : SMTP Error: Could not authenticate.
And in my gmail account i see a mail : sign in attempt prevented
I am using this credentials :
function send_mail($email,$message,$subject)
{
require_once('mailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->AddAddress($email);
$mail->Username="mymail#gmail.com";
$mail->Password="password";
$mail->SetFrom('mymail#gmail.com','Coding Cage');
$mail->AddReplyTo("mymail#gmail.com","Coding Cage");
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->Send();
}
Soemthing might be wrong or how i can provide permission as there is a message in my inbox : Sign-in attempt prevented
Like what #Synchro said, you are using the old version of PHPMailer. Use the latest here.
You also mentioned that an email Sign in attempt prevented landed in your inbox. Try clicking on Review your devices now and there should be a button or link that says Allow this sign in attempt or similar. Then, try to run the code again to see if it works.
If not, well, if you have 2FA turned on in your Google account, try turning it off.
Hope it helps :)

phpMailer: Could not instantiate mail function

I created a form that uses phpMailer to email the results to the website owner. Of course, before I add the owner's email address I use mine to test that the form works. When I use my email the form sends perfectly, however, when I use the owner's address it throws the error "could not instantiate mail function" and won't send the form. The error only occurs with email addresses associated with the owner's domain. Any idea why this could be happening?
If I type this into the command line it works fine:
echo 'test' | mail -s 'test' me#example.com
edit: I wasn't initially using SMTP, but it's now configured as shown below. The error message is now "SMTP Error: The following recipients have failed xxx#somedomain.com" and the end result is still the same. It can e-mail to a number of gmail test addresses but has issue with the owner's email#hisdomain.com. Further, with SMTPDebug it's now producing the error "RCPT TO command failed: 550 No Such User Here" The owner's e-mail, however, works without issue when e-mailed through gmail, outlook, etc.
phpMailer code:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->Debugoutput = "error_log";
$mail->Host = "mail.mydomain.com";
$mail->SMTPAuth = true;
$mail->Username = "admin#mydomain.com";
$mail->Password = "XXXXXXXXXXXXXXX";
$mail->CharSet = 'UTF-8';
$mail->AddReplyTo($emailAddress);
$mail->SetFrom("admin#mydomain.com");
$mail->AddAddress($toAddress,$toName);
$mail->Subject = $emailSubject;
$mail->isHTML(true);
$mail->Body = $emailBody;
$mail->AltBody = strip_tags($emailBody);
// Attempt to send the e-mail
if (!$mail->send()) {
//error handling
}
There are couple of things you should try and check with this particular error message:
Make sure you can use regular php mail() function. Create a blank page and use the php mail() to send a test email. If that works, maybe its your SMTP that's having issues with the particular user domain. Setup gmail SMTP or a different SMTP to send emails:
$mail->IsSMTP();
$mail->Host = "smtp.domain.com";
// optional
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
Can you share your phpMailer source for us to view?
Set $mail->SMTPDebug = 2; so you can see what the server has to say, and read the troubleshooting guide.
You're using authentication without encryption, which is not a good combination and many servers won't allow that. Add this:
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
You've based your code on an old example, so you're probably using an old version of PHPMailer too; get the latest from github.

PHPMailer Send() function is stopping a script after executing

I am using PHPMailer to send a user the results of a form using the following steps:
$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 = "user#example.com";
$mail->Password = "passwd";
$mail->SetFrom("user#example.com");
$mail->Subject = "Test";
$mail->Body = mailMaker($data);
$mail->IsHTML(true);
$mail->AddAddress("otheruser#example.com");
$mail->Send();
This is a part of a larger .php document which is called by ajax and returns the form data back to the page.
When the $mail->Send() function is called the email is sent and everything works fine, but the script ends and I don't get the data back during ajax. I know it is this line because if I comment it out everything works (except the mail isn't sent obviously).
I tried moving the mail methods to another script but I kept getting errors, and was unable to even get a var_dump() or anything returned from the ajax. Is there a logical reason Send() is stopping the script in this case?

Categories