i ve been trying to test sending emails from php using the phpmailer class. my config is this:
require '../php/library/class.phpmailer.php';
require '../php/library/class.smtp.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->Username = "anactual#email.com";
$mail->Password = "thepassword";
$mail->setFrom('existing#gmail.com', 'Real Name');
$mail->addAddress( $validEmail );
$mail->Subject = 'Confirmation Code';
$mail->msgHTML('Follow this link: http://test.com/confirm?code=' . $newAccount->confirmationCode . '');
$mail->AltBody = 'This is a plain-text message body with this link: http://test.com/confirm?code=' . $newAccount->confirmationCode;
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
However, things do not seem to work, and i just copy pasted the code from the gmail example. i ve searched the world wide web for the error output:
SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Name or service not known (0)
SMTP connect() failed.
Mailer Error: SMTP connect() failed.
but still can not find out wha't wrong. any help please?
UPDATE i have disabled the two step verification in gmail
Try starting with the gmail example code provided with phpmailer rather than the old code you've used. https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
Then check out the troubleshooting guide here https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Related
I'm trying to send a mail using phpmailer but getting this error "SMTP connect() failed". I've already allowed less secure apps on gmail. Is all the smtp setting correct? Please guide me where I'm wrong.
<?php
require_once 'PHPMailer-master/PHPMailerAutoload.php';
require 'PHPMailer-master/class.phpmailer.php';
require 'PHPMailer-master/class.smtp.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->host = 'smtp.gmail.com';
$mail->username = 'mymail#gmail.com';
$mail->password = 'mypassword';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPDebug = true;
$mail->isHTML();
$mail->Subject = 'form data';
$mail->Body = 'this is the body of message';
$mail->FromName = 'The Form';
$mail->AddAddress('junaidshekh21#gmail.com','Junaid Shaikh');
if($mail->send())
{
echo "sent successfully";
die();
}
else
{
echo "could not send";
}
?>
Use ErrorInfo in phpmail to see what's wrong
add this before sending the email
$mail->SMTPDebug = 2; //enables SMTP debug information (for testing)
and change from
echo "could not send";
to
echo "Mailer Error: " . $mail->ErrorInfo;
Notes:
Read the phpmail troubleshooting, specifically the part that talks
about "SMTP Error: Could not connect to SMTP host."
Use the official gmail example from the phpmailer github repo.
You may have to allow less secure apps to access your gmail
account
Your ip may be blocked by google.
UPDATE:
From the official phpmailer gmail example:
$mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
Your PHPmailer settings are ok i think it was less secure app access problem.
you must make your gmail to less secure to get access.
got to this url https://www.google.com/settings/security/lesssecureapps
to access your gmail as SMTP.
Following is the code that I am using for sending mail using PHPMailer but getting error.
public function changePassword(){
if($this->request->is('post')){
require_once(ROOT .DS. 'vendor' . DS . 'PHPMailer' . DS . 'class.phpmailer.php');
$email = 'abc#gmail.com';
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->isHTML(true);
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'TLS'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // or 587
$mail->IsHTML(true);
$mail->Username = "xyz#gmail.com";
$mail->Password = "dead_gone";
$mail->SetFrom("xyz#gmail.com");
$mail->AddAddress($email);
$mail->Subject = "password recovery";
$mail->Body = "your password is:- sdx_12345 click here to log in <a href ='http://localhost/cake/logins'> click here </a> ";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "mail sent";
}
}
}
ERROR:
SMTP -> ERROR: AUTH not accepted from server: 530 5.7.0 Must issue a STARTTLS command first. v3sm8416695par.17 - gsmtp
SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.
This property is case-sensitive:
$mail->SMTPSecure = 'tls'
It would help if you based your code on the gmail example provided with PHPMailer - it's not as if you're the first to connect to gmail!
try a SSL connection over the port 465 and look at you security settings in you account from google
You should also put
$mail->IsHTML(true);
after you set your body and attachments.
Edit:
It also might be possible that TLS has to be lowercase.
I've been trying to implement SMTP with PHPMailer for about a week now. I'm revising my old mail() php script and am having difficulty even setting up a live test taken right from the example since I keep receiving the same error. I'm currently running a HostGator dedicated server and my MX record (mail server) is through Office 365/Outlook. For some reason, I keep getting the can't connect to network error as seen in the image below.
My code is below which I took from the github SMTP example. I originally used port 587 with 'tls' authentication but this did not work and after speaking with Microsoft they suggested I do port 25 with no authentication. Still the same error. Can anyone explain to me what i'm doing wrong or suggest a better route of approach? Thanks!
Code:
<?
require(dirname(__FILE__)."/PHPMailerAutoload.php");
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.office365.com';
$mail->Port = 25;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "jason#***.com";
$mail->Password = "***";
$mail->setFrom('jason#***.com', 'Jason');
$mail->addReplyTo('jason#***.com', 'Jason');
$mail->addAddress('jason#***.com', 'Jason');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML($email_message);
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
<?php
require '/etc/PHPMailer/PHPMailerAutoload.php';
$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 = "my#gmail.com";
$mail->Password = "password";
$mail->SetFrom("from#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("to#gmail.com");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
This is my mail functionality code & it gives me the error
SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: No address associated with hostname
I don't have extension=php_openssl.dll in my php.ini file but still I've included this and the same issue
To avoid the problem of dynamic IP (every time i ping smtp.gmail.com I see a slight difference in the last 3digit chunk), you just have to use php built-in gethostbyname() to read the IP in real-time.
$smtp_host_ip = gethostbyname('smtp.gmail.com');
Then use that as host.
Hope it helps.
I got a project which I need to create a personal web site but I can not send e mail via contact form. Could you pls assist me what do I need to do in order to fix it.
I have added class.phpmailer.php and class.smtp.php. Here is php code;
<? php
include 'class.phpmailer.php';
$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 = "behzatdeniz82#gmail.com";
$mail->Password = "myseccretpassword"; //Don't reveal password with others
$mail->SetFrom("behzatdeniz82#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("behzatdeniz99#gmail.com");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
After I change the php code as above, now I begin to receive this error. Can anyone help me how to fix ?
SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "ssl"
- did you forget to enable it when you configured PHP? (1702478359)
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error:
Could not connect to SMTP host.
It seems like SetFrom method is missing. There are multiple versions of PHPMailer available. Download PHPMailer 5.1 which contains a setFrom method:
public function SetFrom($address, $name = '',$auto=1) {
Just Try.
just a small bug, which may be the solution: a missing ' here, after My Name, $mail->SetFrom($mail->Username, 'My Name);