PHPMailer isn't working - phpmailer

require_once('class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure="tls";
$mail->Host = "smtp.mail.yahoo.com";
$mail->Port = 587;
$mail->Username = "********#yahoo.com";
$mail->Password = "*****";
$mail->SetFrom('*********#yahoo.com', 'my name');
$mail->Subject = "A Transactional Email From Web App";
$mail->MsgHTML('text');
$mail->AddAddress('*********#yahoo.com', 'my name');
if($mail->Send()) {
echo "Message sent!";
}else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
So, I try to send an Email with PHPMailer, but the code it does not work,the localhost page isn't working. I copied class.phpmailer.php and class.smtp.php to /var/www/html folder (Before I specified the path in require_once (/usr/share/...)). I enabled ssl extension. I use Ubuntu 16.04 and I installed libphp-phpmailer. What should I do? (Any options to send mail with smtp would be apreciated, already tried: postfix and sendmail).

go through the above comment link..!!
try this
<?php
include "PHPMailer_5.2.4/class.phpmailer.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "******#gmail.com";
$mail->Password = "*******";
$mail->SetFrom('aswad#yahoo.com', 'my name');
$mail->Subject = "A Transactional Email From Web App";
$mail->MsgHTML('text');
$mail->AddAddress('aswad#yahoo.com', 'my name');
if($mail->Send()) {
echo "Message sent!";
}else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>

try to comment the
$mail->IsSMTP();
line
also check the SELinux config
on the shell type
sestatus -b | grep sendmail
if you see that
httpd_can_sendmail off
just type this 2 lines:
restorecon /usr/sbin/sendmail
setsebool -P httpd_can_sendmail 1

Related

Wamp Server and PHP mailer

I have a WAMP server database which contains users with their emails and activation codes.
How do I send an email for every new record in the WAMP server database automatically with its activation code? Using PHP.
P.s: I can already send an email from WAMP using PHP mailer but I have to specify the receiver each time.
This is the mail.php script that I use for sending emails using PHP mailer
<?php
require "init.php";
require"PHPMailerAutoload.php";
require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Mailer = "smtp";
$mail->Host = "smtp.gmail.com";
$mail->Port = "587";
$mail->IsHTML(true);
$mail->Username = "******#gmail.com";
$mail->Password = "******";
$mail->From = "*******#gmail.com";
$mail->FromName = "Vodafone-El7a2ny";
$mail->AddAddress("*****", "Marwa Hashem");
$mail->Subject = "Activation Code";
$mail->Body = " Dear User,
Thank you for registering in El72any Mobile Application,
Your Activation Code is
Best Regards,
****";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Email was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
exit;
} else {
echo 'Email has been sent.';
}
?>
This is the register.php script
<?php
require "init.php";
$EmailAddress=$_POST["EmailAddress"];
$MobileNumber=$_POST["MobileNumber"];
$Password=$_POST["Password"];
$sql_query = "insert into user_information VALUES('$EmailAddress', '$MobileNumber', '$Password','');";
if(mysqli_query($con,$sql_query))
{
echo "<h3>Database Insertion Success...</h3>";
}
else
{
echo "Data Inerstion Error...".mysqli_error($con);
}
?>
Also this is the init.php script I used to connect to my database
<?php
$db_name="CustomerDB";
$mysql_user="root";
$mysql_pass=null;
$server_name="localhost";
$con=mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
if(!$con)
{
echo "Connection Error...".mysqli_connect_error();
}
else
{
echo "<h3>Database connection Success...</h3>";
}
?>
This is really an opinion question since you've not posted any code. It helps to start by reading the docs and examples that are provided with PHPMailer. In your case, start with the mailing list example.

phpmailer No Relay Access Allowed Error

My website is hosted on godaddy, and I just figured out that my website is no more sending emails through phpmailer since last few months. I uploaded the latest version of phpmailer but still no success. The online web mail of my website runs fine. If I use php's "mail" function, it does send emails to gmail, but not to yahoo accounts.
I tried all the three ports 25, 465, and 587, but no luck
I am getting the following error from phpmailer:
SERVER -> CLIENT: 554 p3plsmtpa07-10.prod.phx3.secureserver.net ESMTP No Relay Access Allowed From 50.63.196.51
CLIENT -> SERVER: EHLO lostandfound.pakproject.com
SERVER -> CLIENT:
SMTP ERROR: EHLO command failed:
SMTP NOTICE: EOF caught while checking if connected
SMTP Error: Could not authenticate.
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Following is my code that I am trying to test. (User name, passwords, emails are changed...)
<?php
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = "smtpout.... my_server";
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "here_i_used_my_website_email";
$mail->Password = "here_password";
$mail->setFrom('website_email', 'From name');
$mail->addReplyTo('website_email', 'From name');
$mail->addAddress('another_email', 'name_used_here');
$mail->Subject = 'About the task';
$mail->Body = 'This is email body';
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
$mail->SMTPSecure = false;
$mail->SMTPAuth = false;
It worked for me.
Keep in mind
https://co.godaddy.com/help/mail-server-addresses-and-ports-for-business-email-24071**
I was able to resolve my issue with the following code/settings of phpmailer
<?php
$recipient = "abc#def.com"
$subject = "Subject here";
$emailBody = "This is body";
// PHP MAILER CODE STARTS FROM HERE //
require '../phpmailermaster/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtpout.secureserver.net'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxx#yyyy.com'; // SMTP username
$mail->Password = '3344123'; // SMTP password
//$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
//$mail->Port = 465;
$mail->Port = 80; // TCP port to connect to [THIS PORT ALLOWING EMAILS]
$mail->setFrom('xxx#yyyy.com', 'hello');
//$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress($recipient); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addBCC('bcc#example.com');
//$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 = $subject;
$mail->Body = $emailBody;
$mail->AltBody = $emailBody;
if(!$mail->send())
{
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else
{
echo 'Message has been sent';
}
// PHP MAILER CODE ENDS HERE ==
?>

Error with PHPMailer SMTP connect() failed

I'm using PHPMailer from https://github.com/PHPMailer/PHPMailer and using code below:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';
$mail->port = '25';
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxx#gmail.com'; // SMTP username
$mail->Password = 'xxx'; // SMTP password
$mail->From = 'xxx#gmail.com';
$mail->FromName = 'xxx';
$mail->addAddress('xxx#xxx.com', 'xxx'); // Add a recipient
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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';
}
When I run this, it gives an error:
Message could not be sent.Mailer Error: SMTP connect() failed.
but when I run it on localhost (windows and xampp) it's successful!

How to get SMTP host for my phpmailer

I own my domain .com and it is hosted in different provider of the domain.
And I want to use PhpMailer, but my webhosting doesn't provide me SMTP server but it allows me to create emails of my domain (e.g. sdfsdfsfd#mydomain.com)
So, I really can't use phpmailer because it never connects to SMTP server, what should I do?
I brought my domain in onlydomains and my webhost is 000webhost.
<?php
require './PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'noreply#mydomain.com'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'tls';
$mail->From = 'noreply#mydomain.com';
$mail->FromName = 'Admin';
$mail->addAddress('sdfsdf#hotmail.com'); // Name is optional
mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'swag';
$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';
}
?>
Initially download the php mailer files to your sever(if not downloaded before) & define as following.Avoid using net sources in your code.Once you apply this it will work fine and the mail would be sent as from your mail id.
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPDebug = true;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$mail->Port = 465; // SMTP Port
$mail->Username = "your mail id"; // SMTP account username
$mail->Password = "password"; // SMTP account password
//The following code allows you to send mail automatically once the code is run
$mail->SetFrom('to mail id', 'name'); // FROM
$mail->AddReplyTo('mail id', 'name'); // Reply TO
$mail->AddAddress('recipient id'); // recipient email
$mail->Subject = "First SMTP Message"; // email subject
$mail->Body = "Hi! \n\n some text.";
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
After doing the above code if you still not able to send mail means do the following:
In class.smtp.php file add the below given two lines before the line "Connect to the SMTP server" & it ll work fine.
$host = "ssl://smtp.gmail.com";
$port = 465;

phpMailer with smtp

I have this piece of code:
ini_set('SMTP','smtp.strato.com');
$mail = new PHPmailer();
$mail->IsHTML(true);
It works fine but can I set smtp.strato.com somewhere in phpMailer class?
Have you looked at the smtp example in their site? See here
Unless I'm miss understanding you, it looks to be very straight forward.
$mail = new PHPMailer();
$body = "message";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress("whoto#otherdomain.com", "John Doe");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
You can by changing
$mail->IsSMTP();
$mail->Host = "mail.yourdomain.com";
to
$mail->IsSMTP();
$mail->Host = "smtp.strato.com";
You shouldnt modify the PHPMailer class
//Use this codd to send email using SMTP gmail
<?php
require "PHPMailer/PHPMailerAutoload.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth="login";
$mail->SMTPSecure="ssl";
$mail->Username = "sender#gmail.com";
$mail->Password="password";
$mail->Body="<Body message>";
$mail->MsgHTML=("<h1>Wow Html contents</h1>");
$mail->AddAttachment("Koala.jpg");
$mail->SetFrom("sender#gmail.com","Bucky");
$mail->Subject="My Subject";
$mail->AddAddress("receiver#gmail.com");
if ($mail->Send()){
echo "Sent";
}else
echo "not sent<br>";
echo $mail->ErrorInfo;
?>

Resources