I am trying to send mail using phpmailer but it is giving error
<?php
include("/mail/class.phpmailer.php");
// Basic Header Input
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->Username = 'username'; // your GMail user name
$mail->Password = 'password'; //"Password";
// ---------- adjust these lines ---------------------------------------
$mail->From= 'mg#gmail.com';
$mail->FromName="My site's mailer";
$mail->AddAddress("mg123#gmail.com");
$mail->Subject = "Your invoice";
$mail->IsHTML(false);
$mail->AddAttachment('test1.pdf', 'invoice.pdf'); // attach files/invoice-user-1234.pdf, and rename it to invoice.pdf
$mail->Body = "Please find your invoice attached.";
if(!$mail->Send())
{
echo "Error sending: " . $mail->ErrorInfo;;
}
else
{
echo "Letter is sent";
}
?>
it is showing error like Error sending:
"Error sending: The following From address failed:
mg#gmail.com : Called Mail() without being connected"
i try with adding code $mail->SMTPDebug = 1; then it is showing error like below
SMTP -> ERROR: Failed to connect to server:
php_network_getaddresses: getaddrinfo failed: No such host is known.
(0) The following From address failed: mg#gmail.com
: Called Mail() without being connected
Change this,
$mail->From= 'mg#gmail.com';
You are mixing single quotes and double quotes
Related
I am trying to send an email using Mailer but getting below error
Connection: opening 2017-05-25 08:22:07 SMTP ERROR: Failed to connect
to server: (0) SMTP connect() failed. Mailer Error: SMTP connect()
failed.
php_openssl extension & IMAP both are enabled. I tried to find it on google but still no luck.
Code:
function sendMail($subject='',$to='',$emailcontent='',$attach='')
{
global $_mailmsg;
//echo $emailcontent;exit;
$mail = new PHPMailer;
$mail->SMTPDebug = 4;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = '465';
$mail->SMTPAuth = true;
$mail->Username = 'xx#gmail.com';
$mail->Password = 'xxxx';
$mail->SMTPSecure = 'ssl';
$mail->From = 'xx#gmail.com';
$mail->FromName = 'Test';
$mail->addAddress($to); // Add a recipient
if(!empty($cc)){ $mail->addCC($cc); }
if(!empty($bcc)){ $mail->addBCC($bcc); }
$mail->WordWrap = 50;
if($attach != ''){
$mail->addAttachment($attach);
}
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = 'Test';
try
{
if($mail->send()) {
return 1;
exit;
}
else
{
echo 'Mailer Error: ' . $mail->ErrorInfo;
return 0;
}
}
catch(Exception $e)
{
return 0;
}
}
This looks like your server is not permitted to connect to remote SMTP servers, something very common at big ISPs like GoDaddy. If you do the steps described in the troubleshooting guide you can figure out what's blocking you. The fact that there is no link to the guide in your error message tells me that you're using a very old version of PHPMailer, so get the latest.
PHPMailer has nothing to do with IMAP; that's for inbound mail only.
As you are using Gmail, just turn on "Allow less secure apps":
https://myaccount.google.com/u/0/lesssecureapps
And also you'll probably need to allow access to your Google account without unlock captcha:
https://accounts.google.com/DisplayUnlockCaptcha
<?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 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
Could not access file: http://localhost/k/d/filename.pdf
2014-07-28 16:20:10 SMTP ERROR: Failed to connect to server: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
(10060) SMTP connect() failed. Mailer Error: SMTP connect() failed.
I am trying to send an email. I'm using XAMPP and PHPMailer, and I have no idea what I'm doing wrong. I've looked up tons of similar issues, but I can't find my error.
require("PHPMailer/class.phpmailer.php");
require 'PHPMailer/class.smtp.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "username#gmail.com";
$mail->Password = "password";
$webmaster_email = "username#gmail.com";
$email=$_POST['email'];
$name=$_POST['email'];
$mail->From = "username#gmail.com";
$mail->FromName = "User Name";
$mail->AddAddress($_POST['email'],$_POST['email']);
$mail->AddReplyTo("username#gmail.com","User Name");
$mail->WordWrap = 50;
$mail->AddAttachment($_POST['file']);
$mail->IsHTML(true);
$mail->Subject = "sent document";
$mail->Body = "Here is the document you requested. Thank you.";
$mail->AltBody = "Here is the document you requested from myITpc. Thank you.";
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
$_POST['email'] contains an email address, such as joe.shmo#hotmail.com and $_POST['file'] contains a local filepath, like http://localhost/folder/file.pdf.
Thanks!
I am using the following code to sent mail for a contact form. The mail works perfectly when using a windows server setup with xampp, but fails in Linux server RHEL 5.
I am getting "SMTP Error: Could not connect to SMTP host". I tried telnet the host from terminal. It is getting connected
public function smtpmailer($to,$cc, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->IsHTML();
$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;
$mail->Username = 'XXX#gmail.com';
$mail->Password = 'XXXXXXXX';
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
$mail->AddCC($cc);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
While Debugging is set to 1 I am getting the following error
SMTP -> ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Name or service not known (0)
SMTP Error: Could not connect to SMTP host.
Seems as if name resolving is not working. Check /etc/resolv.conf, it should contain a line like this:
nameserver 8.8.8.8
Replace 8.8.8.8 by the IP of your desired nameserver.