I have written the following code to send email from my website hosted in Namecheap.
<?php
include("constants.php");
require_once('libs/phpmailer/class.phpmailer.php');
require_once("libs/phpmailer/class.smtp.php");
require ("libs/phpmailer/PHPMailerAutoload.php");
try {
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->SMTPSecure = 'tls';
$mailer->Host = 'smtp.zoho.com';
$mailer->SMPTDebug = 2;
$mailer->Port = 587;
$mailer->Username = EMAIL;
$mailer->Password = PASSWORD;
$mailer->SMTPAuth = true;
$mailer->From = EMAIL;
$mailer->FromName = "User";
$mailer->Subject = 'New Query Received';
$mailer->isHTML(true);
$mailer->Body = '<p>Hello,</p><p> The following query was received from '.$_POST['name']. '.<br>"'. $_POST['message']. '"</p><p>'.'His email is '.$_POST['email'].'</p><p>Hope you will have a great day.</p><p>Best Regards,</p>';
$mailer->AddReplyTo(EMAIL_REC, 'Contact');
$mailer->AddAddress(ADDRESS, NAME);
$mailer->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
));
if ($mailer->Send()) {
echo "Sent";
}
else {
echo $mailer->ErrorInfo;
}
}
catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
header('Location:index.html');
exit;
?>
I am using zoho mail for my email host but I have tried the same using a gmail account too. I have already cross verified the EMAIL and its password.
The code works perfectly fine in my localhost but causes "SMTP connect() failed" in the Namecheap server.
If I comment $mailer->IsSMTP(); then no error is shone but email is not sent.
Related
I am using php mailer to send mail. It is working on localhost. But not working on live cpanel hosting.
Mail is sending proper in my localhost. In live server I found error.
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
public function send_mail($to,$bodytext,$subject)
{
try{
// start mailer
$toAddress = $to;
$message = $bodytext;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->IsHTML(true);
$mail->Username = "xxxxxx#gmail.com"; // your gmail address
$mail->Password = "xxxxx#123"; // password
$mail->SetFrom("xxxxxx#gmail.com");
$mail->Subject = $subject; // Mail subject
$mail->Body = $message;
$mail->AddAddress($toAddress);
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
return False;
} else {
return true;
}
// end mailer
}
catch (Exception $e)
{
return $e->getMessage();
}
}
I have been facing trouble with phpmailer on live server as it is giving me error smtp connect() failed (Netword is unreachable 101) but working good on localhost please someone guide me how to fix it i have been tried different approaches but nothing worked...
phpmailer code
require '/home/schoswiy/public_html/assets/PHPMailer-master/src/PHPMailer.php';
require '/home/schoswiy/public_html/assets/PHPMailer-master/src/SMTP.php';
require '/home/schoswiy/public_html/assets/PHPMailer-master/src/Exception.php';
set_time_limit(0);
if(isset($_POST['send_message'])){
$name = trim($_POST['name']);
$subject = trim($_POST['subject']);
$email = trim($_POST['email']);
$message = trim($_POST['message']);
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->SMTPAuth = true;
$mail->Port= 465;
$mail->SMTPDebug = 4;
$mail->Username ='myemail#gmail.com';
$mail->Password = 'pass';
$mail->SMTPSecure = 'ssl';
$mail->setFrom('myemail#gmail.com', 'xyz');
$mail->addReplyTo($email, $name);
$mail->addAddress('myemail#gmail.com');
$mail->Subject = $subject;
$mail->Body = $message;
if($mail->send()){
$_SESSION['msg'] = 'Message Send Successfully';
header("location:contact-us.php");
echo "messsage Send";
}
else {
echo "<script type='text/javascript'> alert('Failed to send')</script>";
}
}
?>
Try to catch the error.
maybe that answer could be useful.
PHP - Error handling
you can make a more comfortable search
Im creating a little Email Script at the Moment with PHPMailer + SMTP Authentication. I tried now sending an E-Mail using a wrong Passwort - but it still gives back "true" for success... anyone have any idea?`
Here is My Function, that i use to call sendmail:
$erfolg_email = true;
foreach($empfaenger as $value)
{
$response = $this->sendMail($smtp, $value, $content, $files);
if($response != true)
{
return $response;
$erfolg_email = false;
}
}
And here is my PHPMailer Function
function sendMail($smtp, $empfaenger, $content, $attachements)
{
$mail = new PHPMailer(true);
try{
$mail->IsSMTP();
$mail->Host = $smtp['SMTP'];
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Port = $smtp['Port'];
$mail->SMTPSecure = 'tls';
if($smtp['Domain'] != '')
{
$username = $smtp['Username']."#".$smtp['Domain'];
}else
{
$username = $smtp['Username'];
}
$mail->Username = $username; // SMTP username
$mail->Password = $smtp['Password']; // SMTP password
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->From = $smtp['Email_Address'];
$mail->AddAddress($empfaenger);
$mail->WordWrap = 50;
$mail->isHTML(true);
foreach($attachements as $value)
{
$mail->AddAttachment($value);
}
$mail->Subject = "Mahnung";
$mail->Body = $content;
$mail->AltBody = "Sie haben offene Posten";
if($mail->Send() == true)
{
echo $mail->ErrorInfo;
return $mail->ErrorInfo;
}else
{
return $mail->ErrorInfo;
}
} catch (phpmailerException $e) {
return $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
return $e->getMessage(); //Boring error messages from anything else!
}
}
$smtp contains an array with all the SMTP INformation, Email address, Signature, Smtp Server, Port, Username, Password and SSL Usage...
I am pretty sure, I am using the wrong username and password, as no Email is getting through - but i still get "true" as a result of the send mail function... its not even echoing the error. I did even try to give an Error Message, when sending is successfull... But nothing
Any help is apreciated!
Cheers
Your function sendMail doesn't return a boolean. Since it's an array / object the result is always true in this case. Try printing $response before your if statement and you will see the error.
In your php mailer function file itself you can add this coding at the last:
$send = $mail->Send(); //Send the mails
if($send){
echo '<center><h3>Mail sent successfully</h3></center>';
}
else{
echo '<center><h3>Mail error: </h3></center>'.$mail->ErrorInfo;
}
}
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('/');
}
I'm trying to run PHPMailer for an internal contact form and I am getting the error ERROR: AUTH not accepted from server. Here is my current code..
require_once('/includes/class.phpmailer.php');
include("/includes/class.smtp.php");
if (isset($_POST['submit'])) {
$mail = new PHPMailer(true);
$mail->IsSMTP();
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
try {
$mail->Host = "192.168.6.6";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = True;
$mail->Username = 'xxxxx';
$mail->Password = '***';
$mail->Port = 25;
$mail->AddAddress('xxx#xxx.com', 'xxxxx');
$mail->SetFrom($email);
$mail->Subject = 'New message from Contact Form';
$mail->Body = $message;
$mail->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
};
This error basically means that your attempt to authenticate was rejected by the remote server. Different PHPMailer settings (well SMTP settings) are required by different remote mail servers.
This could be caused by
Using the wrong port
Using the wrong host
Incorrect user/pass
Incorrect SMTPSecure
Example SMTP setup:
Gmail: use of phpmailer class
Hotmail: phpmailer with hotmail?
If you are using this internally, you may not need to use SMTP authentication at all, depending on your server settings. Try this and see if it works:
require_once('/includes/class.phpmailer.php');
include("/includes/class.smtp.php");
if (isset($_POST['submit'])) {
$mail = new PHPMailer(true);
$mail->IsSMTP();
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
try {
$mail->Host = "192.168.6.6";
$mail->SMTPDebug = 0;
$mail->SMTPAuth = False;
$mail->Port = 25;
$mail->AddAddress('xxx#xxx.com', 'xxxxx');
$mail->SetFrom($email);
$mail->Subject = 'New message from Contact Form';
$mail->Body = $message;
$mail->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
};