PHPMailer works where CakeEmail doesn't with the same configuration - php

I have the following problem, as you may have imagined by the title I have a CakePHP v2.5.6 application with a contact form, and it's giving me an authentication error every time I submit it, how was my surprise, after trying a simple test using PHPMailer it works perfectly with apparently the same configuration.
CakePHP configuration (app/Config/email.php)
<?php
class EmailConfig {
public $info = array(
'transport' => 'Smtp',
'host' => 'smtp.foo.com',
'port' => 25,
'username' => 'username',
'password' => 'password'
);
}
CakePHP sender code
CakeEmail::deliver('foo#foo.es', 'Subject', 'Test', 'info');
CakePHP error report
PHPMailer test script
<?php
require './PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.foo.com';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->Port = 25;
$mail->setFrom('info#example.es', 'Mailer');
$mail->addAddress('foo#foo.es', 'Mr. foo');
$mail->addReplyTo('info#example.es', 'Information');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
So my question is, is there actually any differences between configurations, or I'm missing something? Why is PHPMailer working and CakeEmail isn't?
Thank you in advance :)

I found the solution which seems pretty dummy but the debug information was not completly accurate about the error cause.
The thing is missing and where PHPMailer and CakeEmail differ is in the from config field.
PHPMailer from configuration
$mail->setFrom('info#example.es', 'Mailer');
As you may notice the CakeEmail config file lacks of that field, so just adding it the error disappears.
The final CakeEmail config file should look like:
<?php
class EmailConfig {
public $info = array(
'transport' => 'Smtp',
'host' => 'smtp.foo.com',
'port' => 25,
'username' => 'username',
'password' => 'password',
'from' => 'info#example.es'
);
}
I hope this helps someone :)

Related

Mailer Error Mail() without being connected

<?php require 'PHPMailer_5.2.4/class.phpmailer.php';
$mail = new PHPMailer;
$mail->SMTPDebug=0;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'gmail-smtp-in.l.google.com';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'demotp18#gmail.com'; // SMTP username
$mail->Password = '****'; // * is just for not to show password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->Port = 587;
$mail->setFrom('neelsoni.sn#gmail.com');
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
) );
$mail->addAddress('demotp18#gmail.com'); // Name is optional
$mail->WordWrap = 50;
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>
output : Message could not be sent.Mailer Error: The following From address failed: neelsoni.sn#gmail.com : Called Mail() without being connected
my question is about PHPMAILER
What is the problem in the code ??
why i get this error ? i turn on less secure in gmail.
And in HOST i tried smtp.gmail.com but there is same error i seen, thats why i found this host type.
same code in my one friend's laptop it's working but another friend is facing this same error.
please help me !!!!!!!!!!

SMTP Error: Could not connect to SMTP host. i tried all result found on stackoverflow and githubs

im new in php im trying to use phpmailer to send email using gmail
here is the code i wrote
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'sendermail#gmail.com';
$mail->Password = '<some password>';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('anadresse#gmail.com', 'GestionStock');
$mail->addAddress('anadresse#gmail.com', 'hamza');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} ?>
i got as results SMTP Error: Could not connect to SMTP host.
SMTP Error: Could not connect to SMTP host.
Message could not be sent.Mailer Error: SMTP Error: Could not connect to SMTP host.
can someone tell me what im doing wrong !!
The same problem I had using Gmail and I solved it by downloading the cacert.pem certificate from the site https://curl.haxx.se/docs/caextract.html You should also write the php.ini file as follows: extension = php_openssl.dll openssl.cafile = C: \ xampp \ php \ extras \ ssl \ cacert.pem
It must be activated in the GMail account in the label: Access and security of the option:
Allow less secure apps access option: ON
This solution is thanks to the matteobin user contribution of stackoverflow
i figured out that the problem not from code , its from the account gmail , idk exactly what is but i have different accounts , some works and other not working , i tried creating new gmail account and enabling lesssecureapps but not worked
it works only in a specific gmail with a disabled lesssecureapps
The following code has worked for me.
require('./phpmailer/PHPMailerAutoload.php');
require('./phpmailer/class.phpmailer.php');
$mail=new PHPMailer();
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->CharSet = "utf-8";
$mail->Host = "smtp.gmail.com";
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->SMTPAuth=true;
$mail->isSMTP();
$mail->Username="myemail#gmail.com";
$mail->Password="mypassword";
$mail->setFrom('myemail#gmail.com','Some text here');
$mail->addAddress($email);
$mail->addReplyTo('myemail#gmail.com');
$mail->isHTML(true);
$mail->Subject="Confirmation email";
$mail->Body="<h2 style='text-align='center';'>Confirmation email</h2></br>
<p>Your message has been received. One of our team members will contact you shortly.</br></br>Thank you for contacting us.</p>";
if(!$mail->send())
{
echo "Message could not be sent!";
echo $mail->ErrorInfo;
}
else
{
echo "Message sent successfully!";
}
I was having the same problem. I added the following code:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
It is because, in the newer version, PHP has implemented stricter SSL behaviour which has caused this problem. Hope this solves your problem as well.

PHPMailer will only send emails from localhost and won't send from server

I have written a basic script to send an email using PHPMailer.
When I run my script locally to send the email to myself it works fine but when I try to run it from the server, I don't get an email. I want to send emails without specifying SMTP in PHPMailer.
The PHPMailer library is in the codebase directory which is in the same directory as the code below. Also the same on the server.
Here is my PHP code:
$to = 'aaqib.farooq#example.co.uk';
$toName = 'Aaqib Farooq'
require_once('codebase/PHPMailer/class.phpmailer.php');
$mail = new PHPMailer;
$mail->From = "fromname#example.co.uk";
$mail->FromName = "Joe Bloggs";
$mail->addAddress($to, $toName);
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<p>Lorem ipsum blah blah blah. This is my messsage.</p>";
$mail->AltBody = "This is the plain text version of the email content";
if ( !$mail->send() ){
echo 'Email not sent';
} else {
echo 'Email was sent successfully';
}
I've already checked stackoverflow for answers but couldn't find what I need. Can anyone help?
Add this fileds:
$mail->IsSMTP();
$mail->Host = 'host of mail';
$mail->Port = 465 or 587;
$mail->SMTPAuth = true;
$mail->Username = 'username - usually some#mail';
$mail->Password = 'password';
Possible you will need also such settings:
$mail->SMTPSecure = '';
$mail->SMTPAutoTLS = false;
$mail->smtpConnect(
array(
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false,
"allow_self_signed" => true
)
)
);
Jeez. I've been trying to solve this issue since yesterday. Turns out it was working all along but was going to my junk.

Internal Server Error 500 when sending mail to AWS SES with cakephp running on Hiawatha

I'm getting "Internal Server Error 500" after the third or sometimes at the first attempt to send a mail using CakePHP 3 through AWS SES account (in production mode) running on Hiawatha server.
Here is my php code:
public function sendmail()
{
$email = new Email();
$email->transport('SES');
try {
$res = $email->from(['account#example.com' => 'Name'])
->to(['receiver#hotmail.com' => 'Receiver'])
->subject('Test mail')
->send('some text');
} catch (Exception $e) {
$this->Flash->error('Error. Please, try again.');
echo 'Exception : ', $e->getMessage(), "\n";
return $this->redirect('/');
}
$this->Flash->success('Ok. You will receive a confirmation mail');
return $this->redirect('/');}
Here is the transport configuration
'EmailTransport' => [
'SES' => [
'host' => 'email-smtp.eu-west-1.amazonaws.com',
'port' => 25,
'timeout' => 60,
'username' => 'ASDFASADQWE',
'password' => 'FSDFDSFDSFSEREWRWERWER',
'tls' => true,
'className' => 'Smtp'
],
port 465 and 587 are not working at the first attemp
So, basically I can't identify if the problem came from CakePHP, AWS SES or some configuration on the server.
Thank you for any recommendation.
At the end I stop to use cakePHP mail and setup PHPMailer, some difficulties to use compose and make it run, however at the end this is the working code that I can send many mails in a row.
public function sendMailPHPMailer()
{
$mail = new \PHPMailer();
$mail->isSMTP();
$mail->Host = 'email-smtp.eu-west-1.amazonaws.com';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->From = 'mail#mail.com';
$mail->FromName = 'cakePHP PHPMailer';
$mail->addAddress('tomail#mail.com', 'receiver');
$mail->isHTML(true);
$mail->Subject = 'Test using PHPMailer & SES';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text';
if(!$mail->send()) {
$this->Flash->error('error');
echo 'Exception : ', $mail->ErrorInfo, "\n";
return $this->redirect('/');
}else{
$this->Flash->success('ok');
return $this->redirect('/');
}
}
And with this code I can send only 3 mails with an interval of 1s then I receive an error 500.
public function sendmail()
{
$email = new Email();
$email->transport('SES');
try {
$res = $email->from(['mail#mail.com' => 'cakePHP mail'])
->to(['tomail#mail.com' => 'receiver'])
->subject('cakePHP & SES')
->send('message via cakePHP and SES');
} catch (Exception $e) {
$this->Flash->error('error');
echo 'Exception : ', $e->getMessage(), "\n";
return $this->redirect('/');
}
$this->Flash->success('ok');
return $this->redirect('/');
}

Using SMTP PHPMailer for 'Domain' email address, email sent but not recieved

I am using PHPMailer to send email. It works fine when I use gmail smtp but when I try using my domain smtp what I see on screen is 'message sent!' but I don't receive any email at all. I have tried using debugger, it says
We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
and here is my code
<?php
date_default_timezone_set('Etc/UTC');
//Load PHPMailer dependencies
require_once 'PHPMailerAutoload.php';
require_once 'class.phpmailer.php';
require_once 'class.smtp.php';
/* CONFIGURATION */
$crendentials = array(
'email' => 'xxxxx#example.com',
'password' => 'xxxxxx'
);
$smtp = array(
'host' => 'secure.ehostpk.com',
'port' => 465,
'username' => $crendentials['email'],
'password' => $crendentials['password'],
'secure' => 'ssl' //SSL or TLS
);
/* TO, SUBJECT, CONTENT */
$to = 'xxxxxx#example.com'; //The 'To' field
$subject = 'This is a test email sent with PHPMailer';
$content = 'This is the HTML message body <b>in bold!</b>';
$mailer = new PHPMailer();
//SMTP Configuration
$mailer->isSMTP();
$mailer->SMTPDebug = 2;
$mailer->SMTPAuth = true; //We need to authenticate
$mailer->Host = $smtp['host'];
$mailer->Port = $smtp['port'];
$mailer->Username = $smtp['username'];
$mailer->Password = $smtp['password'];
$mailer->SMTPSecure = $smtp['secure'];
//Now, send mail :
//From - To :
$mailer->From = $crendentials['email'];
$mailer->FromName = 'Team'; //Optional
$mailer->addAddress($to); // Add a recipient
//Subject - Body :
$mailer->Subject = $subject;
$mailer->Body = $content;
$mailer->isHTML(true); //Mail body contains HTML tags
//Check if mail is sent :
if(!$mailer->send()) {
echo 'Error sending mail : ' . $mailer->ErrorInfo;
} else {
echo 'Message sent !';
}
I have searched a lot and I don't understand what should I do. Any help would be greatly appreciated.
One thing that I have found is, that on most servers these days (especially cpanel servers) the email address you are attempting to send with actually has to be created within cpanel itself in order for it to work properly.
From my understanding, the script only routes it to the server and the email account, and then it's routed further from there and finally sent out from your email account on the server. So a solution may be to actually setup the email address in your server.

Categories