My email is not sent when use gmail smtp server? - email

I am using gmail smtp server on my website to send email when users register but currently I can not send emails all information is correct. How can I fix this issue? I am using php mailer
function send()
{
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 465;
$mail->Username = 'example#gmail.com';
$mail->Password = 'mypassword';
$mail->From = 'noreply#example.com';
$mail->FromName = 'example';
$mail->AddAddress($this->_tpl_vars['TO']);
if (isset($this->_tpl_vars['CC']))
{
$cc = explode(';', $this->_tpl_vars['CC']);
foreach ($cc as $c)
if (!empty($c))
$mail->AddCC($c);
}
$mail->AddReplyTo('noreply#example.com');
$i = array();
$i = 3;
$mail->IsHTML(true);
$mail->Subject = $this->_tpl_vars['SUBJECT'];
$mail->Body = $this->_tpl;
#$mail->Send();
}
I see message in my gmail account one user try login with my ip server gmail block it.
Thank you.

You can go to https://www.google.com/settings/security/lesssecureapps
and turn on "Access for less secure apps"
The proper way now: you may want to check https://developers.google.com/gmail/xoauth2_libraries for PHP examples on how to do it using Oauth2

Related

Send mail using an email account created on Hostinger (PHP)

I am sending an email through PHPMailer, the code is hosted on hostinger's hpanel and I am using an email I created on the hpanel. After running the code, I get no errors, and no feedback on whether the mail has been sent, it just doesn't show anything.
Please help me I currently don't know what to do.
require '../../vendor/autoload.php';
$mail = new PHPMailer(true);
try
{
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'smtp.titan.email';
$mail->SMTPAuth = true;
$mail->Username = 'admin#hostinger.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'SSL';
$mail->Port = 465;
$mail->setFrom('admin#hostinger.com', 'Hostinger');
$mail->addAddress($email, $username);
$mail->addReplyTo('admin#hostinger.com', 'For any Information');
$mail->addCC('admin#hostinger.com');
$mail->isHTML(true);
$mail->Subject = 'Sending message';
$mail->Body = $message;
$mail->AltBody = "Hello there";
$mail->send();
echo "Sent";
}
catch (Exception $eax)
{
echo 'EMAIL SENDING FAILED. INFO: '.$mail->ErrorInfo;
}
No need to Use
$mail->isSMTP();
Comment $mail->isSMTP();
Make sure you are using the right smtp host:
$mail->Host = 'smtp.hostinger.com'

phpmailer script on godaddy is not working [duplicate]

This question already has answers here:
PHPMailer GoDaddy Server SMTP Connection Refused
(21 answers)
Closed 5 years ago.
SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Name or service not known (0) 2017-11-15 13:11:07 SMTP connect() failed.
<?php
require 'PHPMailerAutoload.php';
//echo !extension_loaded('openssl')?"Not Available":"Available <br/>";
$email = $_POST['email'];
$number = $_POST['phone'];
$client = $_POST['client'];
$to = 'myemailid#gmail.com';
$subject = 'user registration';
$phone = "phone number:".$number;
$message = "client details:"."\n"."email:".$email."\n"."phone number:".$number."\n"."client:".$client;
$headers = "From:".$email;
$mail = new PHPMailer;
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'hosting.secureserver.net'; // ssl://smtp.gmail.com // Specify main and backup SMTP servers
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->From = 'myemailid#gmail.com';
$mail->SMTPSecure = 'none'; //TLS // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; //587 // TCP port to connect to
$mail->setFrom($email);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->Body = $message;
if($mail->send()) {
header("Location: ../../thankyou.html");
}
else {
echo $mail->ErrorInfo;
//header("Location: ../../error.html");
}
?>
this is the error i am receiving, as i am new to this php any detailed explanation would be highly appreciated. thank you
The host hosting.secureserver.net does not exist. You need to set the correct server and credentials for the outbound SMTP server. This information should be provided by whoever is hosting your email services - probably your web host.

PHPmailer: SMTP connect() failed (not working)

I want to send mail using phpmailer from localhost XAMPP using SMTP server (gmail). But I keep getting this error:
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I tried many solutions regarding uncomment openSSL in php.ini file, changing the port 465("ssl") and 587("tls"), but it does not work.
My codes:
<?php
date_default_timezone_set('Etc/UTC');
'PHPMailerAutoLoad.php';
class.phpmailer.php if not already loaded
$port =465;
$securetype = 'ssl';
$from = 'myemail#gmail.com';
$name = 'User';
$toemail= "mymail#gmail.com";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->isSMTPDebug = 1;
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myemail#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = $securetype;
$mail->Port = $port;
$mail->From = $from;
$mail->FromName = $name;
$mail->addAddress($toemail);
$mail->isHTML(true);
$mail->Subject = 'Test Mail Subject!';
$mail->Body = 'This is SMTP Email Test';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
You will need class.phpmailer.php. If you're using SMTP, you'll need class.smtp.php.
Try this demo code:
<?php
require 'class.phpmailer.php';
require 'class.smtp.php';
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
#require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'mail.domain.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 25;
//Set the encryption system to use - ssl (deprecated) or tls
//$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "demo#domain.com";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom('demo#domain.com', 'subillion');
//Set an alternative reply-to address
#$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress("example#gmail.com");
//Set the subject line
$mail->Subject = 'Demo !!';
//Read an HTML message body from an external file, convert referenced images to embedded,
$mail->isHTML(true);
$msgbody = "This is a demo test !";
$mail->Body = $msgbody;
//send the message, check for errors
if (!$mail->send()) {
// echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>

Contact Form Using SMTP

I have a simple contact form that works if I do not use smtp. I've tried to add smtp to use with mandrill, but it doesn't work. Emails aren't going through to mandrill.
I do not see any errors after sending either even if I enabled '$mail->SMTPDebug = 1;' I've tried on both local and live server environments.
<?php
require_once('../mail/class.phpmailer.php');
$emailaddress = "name#domain.com";
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$ip = $_SERVER['REMOTE_ADDR'];
$message = $_POST['message'];
$subject = "Contact Form: $name";
$body = "You have received a new message from website.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nIP Address: $ip\n\nMessage:\n$message";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.mandrillapp.com";
//$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->Host = "smtp.mandrillapp.com";
$mail->Port = 587;
$mail->Username = "username";
$mail->Password = "password";
$mail->CharSet = 'UTF-8';
$mail->SetFrom($email_address);
$mail->AddReplyTo($email_address);
$mail->Subject = "Contact Form: ".$_POST['name']." ";
$mail->MsgHTML($body);
$mail->AddAddress($emailaddress);
$mail->Send();
?>
I figured it out. I needed to also include the 'class.stmp.php' file as well. It doesn't have to be call, but needs to in the same folder as 'class.phpmailer.php.'

phpmailer noname attachment

I'm using phpmailer to send email. But all of my emails are send with noname attachement. I already checked if variables are set before I use addAttachemnt function and they are. It looks like this:
$fname = $_FILES['file']['name'];
$fTmpName = $_FILES['file']['tmp_name'];
$mail = new PHPMailer();
$mail->From = "mymail#mymail.com";
$mail->FromName = "mymail.com"; //moje meno
$mail->IsHTML(true);
$mail->IsSMTP();
$mail->Host = "smtp.mymail.com";
$mail->Subject = "Subject";
$mail->AddAddress($email);
echo $fnameZivotopis;
$mail->AddAttachment($fTmpName,$fname);
$mail->Body = $msg;
$mail->Send(); // send message
Try to use AddStringAttachment(string $att_file_data, string $att_file_name, ...).