phpmailer does not connect to SMTP servers - smtp

i've been trying to use phpmailer and tried using live.com and gmail.com but it always can't connect to SMTP server. here's the full code (i've tried live.com using smtp.live.com but i get the same problem "Message could not be sent.Mailer Error: SMTP connect() failed.")
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$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 = 'my emil address'; // SMTP username
$mail->Password = 'my password'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable encryption, 'ssl' also accepted
$mail->From = 'the same email address';
$mail->FromName = 'Mailer';
//$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('another email address'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$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';
}

I suspect it's because you've not set the port, and SMTPSecure should be lower case. Change this:
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
Beyond that, check that you're allowed to send outbound mail by your ISP/firewall and that your DNS is working.

Try This:
$mail->SMTPSecure = 'tls';
$mail->Host = 'tls://smtp.gmail.com';
$mail->Port = 587; //You have to define the Port
$mail->SMTPDebug = 3;
Remove This:
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'TLS';

Related

Output ('',S); breaking code with Mpdf and PHPmailer

I am trying to set up sending a pdf attachment with mPDF and PHPMailer.
mPDF is woking fine when output is download to browser, the minute I change it to Output('',S) the code stops working with Error 500 and I have tried different versions of the mailer code with no luck. I cant find any error with the syntax and all online tutorials seem to agree with the code. I am stumped
My code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once __DIR__ . '/vendor/autoload.php';
// Grab variables
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$message = $_POST['message'];
$mpdf = new \Mpdf\Mpdf();
$data = "";
$data .= "<h1>Testing this</h1>";
//Add Data
$data .='<strong>First Name</strong> '. $fname.'<br>';
$data .='<strong>Last Name</strong> '. $lname.'<br>';
$data .='<strong>Email</strong> '. $email .'<br>';
if($message)
{
$data .='<br/><strong>Message</strong><br/> '. $message.'<br>';
}
//WritePdf
$mpdf->WriteHtml($data);
//output to bowser
$pdf = $mpdf->output('',S);
$enquirydata = [
'First Name' => $fname,
'Last Name' => $lname,
'Email' => $email,
'Message' => $message
];
sendEmail($pdf,$enquirydata);
function sendEmail($pdf, $enquirydata)
{
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'in-v3.mailjet.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('myemails#gmail.com', 'Test Form');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
// Attachments
// Content
$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';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
?>
If you are in a localhost, comment the line with the validation $email->isSMTP();
It works for me in my local.

Contact form with PHPMailser doesn't work

I would use PHPMailer with a contact form but I am doing something wrong because it doesn't work.
<?php
require ("class.phpmailer.php");
if (isset($_POST['submit'])) {
$name=$_POST['name'];
$subject=$_POST['subject'];
$email=$_POST['email'];
$message=$_POST['message'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.mail.com";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'myemail#mail.com';
$mail-> Password = 12345;
$mail->From = ($email);
$mail->FromName = ($name);
$mail->addAddress = 'myemail#mail.com';
$mail->isHTML(false);
$mail->Subject = "Enquiry from Website submitted by $name";
}
if (!$mail->Send()) {
echo "<script>alert('Submission failed.');</script>";``
}
else {
echo "<script>alert('Email has been sent successfully.');</script>";
}
?>
Would you be so kind to give me a little big help? I don't understand where I'm doing wrong.
Thank you.
If you inspect debug output you can find a hint. Most possible reasons are incorrect parameters or maybe smtp server rejects request.
Please post debug output.

SMTP ERROR: Failed to connect to server: Network is unreachable (101) SMTP Connect failed

Excuse me, I really need some help for this code :
<?php
require 'PHPMailerAutoload.php';
require 'class.phpmailer.php';
$from = "xxx#gmail.com";
$mail = new PHPMailer();
$mail->SMTPDebug=2;
$mail->IsSMTP(true); // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com"; // SMTP host
$mail->Port = 587; // set the SMTP port
$mail->Username = "xxx#gmail.com"; // SMTP username
$mail->Password = "xxx"; // SMTP password
$mail->SetFrom($from , 'Admin xxx');
$mail->Subject = "mail title";
$mail->MsgHTML("Halo halo");
$address = "xxx#gmail.com";
$mail->AddAddress($address, $to);
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
And I get this error: 2018-06-08 01:52:09 SMTP ERROR: Failed to connect to server: Network is unreachable (101) 2018-06-08 01:52:09 SMTP connect() failed.
I have tried all of solution about this problem, but nothing happen. please, I need your help. thank you..

Phpmailer can't send email (Could not instantiate mail function.;)

My sendmail function:
$usr = $user;
$body_msg = $this->getBodyEmail($fields);
$email_from = 'contato#mydomain.com';
$email_name = 'Name';
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SetLanguage("br", "phpmailer/language/phpmailer.lang-br.php");
$mail->setFrom( $email_from, $email_name);
$mail->isHTML(true);
$mail->Subject = 'Contato';
$mail->Body = $body_msg;
$mail->addAddress($fields['email'], $fields['nome']);
Of course I hid the domain.
So I try the $mail->send(), but it will not work and the $mail->ErrorInfo; displays: "Could not instantiate mail function.".
This system used to work without SMTP, I do not need it.
EDIT:
I changed the SetFrom to From and FromName and it seems to have worked.
$mail->From = 'contato#mydomain.com';
$mail->FromName = 'Name';
What it can be?
I changed the SetFrom to From and FromName and it seems to have worked.
$mail->From = 'contato#mydomain.com';
$mail->FromName = 'Name';

phpmailer and gmail SMTP ERROR: Failed to connect to server: Network is unreachable (101) SMTP connect() failed

I need help please
this is my code:
require '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 = 'some#gmail.com';
$mail->Password = 'somepass';
$mail->addAddress('another#gmail.com', 'Josh Adams');
$mail->Subject = 'PHPMailer GMail SMTP test';
$body = 'This is the HTML message body in bold!';
$mail->MsgHTML($body);
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
and I get this error:
2013-12-11 15:15:02 SMTP ERROR: Failed to connect to server: Network is unreachable (101) SMTP connect() failed. Mailer Error: SMTP connect() failed.
any help please?
You might want to start by isolating this problem to determine whether it's truly a network problem; or whether it's specific to PHP mailer or your code. On your server, from a command prompt, try using telnet to connect to smtp.gmail.com on port 587, like so:
telnet smtp.gmail.com 587
You should see a response from smtp.gmail.com, like so:
Trying 173.194.74.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP f19sm71757226qaq.12 - gsmtp
Do you see this, or does the connection attempt hang and eventually time out? If the connection fails, it could mean that your hosting company is blocking outgoing SMTP connections on port 587.
This worked for me:
change:
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = '465';
to
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = '587';
The code below:
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Host = 'ssl://smtp.gmail.com:465';
I was facing the same issues on Godaddy hosting so I spend lots of time and fix it by disabling the SMTP restriction on the server end.
SMTP code is for PHPMailer
$mail = new PHPMailer(true);
try {
//Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->SMTPDebug = 2; //Enable verbose debug output
//$mail->isSMTP(); //Send using SMTP
$mail->Host = "tls://smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = contact#example.in;
$mail->Password = SMTP_PASSWORD;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//Recipients
$mail->setFrom('contact#example.in', 'Online Order');
$mail->addAddress('test#gmail.com'); //Add a recipient
$mail->addReplyTo('contact#example.in', 'Jewellery');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Order';
$mail->Body = 'This is test email content';
$mail->AltBody = 'This is test email content';
if($mail->send()){
return '1';
}else{
return 'Order email sending failed';
}
change
$mail->SMTPSecure = "tls";
with
$mail->SMTPSecure = 'ssl';