PHP MAILER - EMAIL SENDING - php

I want to send a confirmation mail to an user who is trying to register. Iam using PHP Mailer for sending mails. Here is my code:
require_once("PHPMailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true;// turn on SMTP authentication
$mail->Username = "root";// SMTP username
$mail->Password = "";// SMTP password
$mail->SetLanguage("en","PHPMailer/language");
//compose mail
$mail->From = "admin#localhost.com";
$mail->FromName = "Cinema Admin";
$mail->AddAddress($_POST['email']);
$mail->AddReplyTo("admin#localhost.com", "Cinema Admin");
$mail->Subject = 'Your confirmation link here';
$mail->Body = "Your confirmation link\r\n";
$mail->Body .= "Click on this link to activate your sccount\r\n";
$mail->Body .= "http://localhost/user.login_plugin/confirm.php?passkey=$confirm_code";
//send mail
if (!$mail->Send())
{
echo "Message was not sent <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
When I execute the file,it says
Message was not sent
Mailer Error: SMTP connect() failed.
Could anyone suggest me for sending mails from localhost to the registered user's emailid.
I had searched with few titles regarding "ending emails" in stack overflow, but couldn't find any solution from any posts.
Thank You in advance

Could you try remove the smtp part? From your comment , you do not have mail server setup.
$mail = new PHPMailer();
$mail->SetLanguage("en","PHPMailer/language"); // remove smtp things

You must need a SMTP details in order to send an email using SMTP, For example if you want to setup SMTP using gmail,
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername#gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
If you don't want use SMTP, you can called default mail function
$mail->IsMail(); //Sets Mailer to send message using PHP mail() function.

Related

Could not instantiate mail function. Message was not sent PHPMailer Error: Could not instantiate mail function

I am using PHPMailer to send mails. I have deployed my website on Google Cloud VM Instance
Here is my mail sending code
<?php
require_once('phpmailer/PHPMailerAutoload.php');
$mail = new \PHPMailer;
//$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->isHTML(true);
$mail->addAddress($_POST['email']);
$mail->Subject = "Testing PHPMailer with localhost";
$mail->Body = "Hello, Below are the details of contact ";
$mail->Body = "<table><tr><td>Name:</td><td>".$_POST['name']."</td></tr><tr><td>Email Address:</td><td>".$_POST['email']."</td></tr><tr><td>Subject :</td><td>".$_POST['subject']."</td></tr><tr><td> Message :</td>".$_POST['$POST['msg']."</td></tr></table>";
if(!$mail->Send())
echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
?>
But the mail is not be able to send. Can anyone help me out? I can provide further code references if anyone needs
Try using SMTP to send email:
$mail->IsSMTP();
$mail->Host = "smtp.example.com";
// optional
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
You have commented out isSMTP() so you are not using SMTP (so none of your SMTP settings will do anything), but the default mail() transport. This requires that you have a mail server installed in your server, which you apparently don’t.
To fix, either install a mail server (postfix is good) or enable SMTP, as chirag suggested.
All of this is covered in the PHPMailer troubleshooting guide.

PHPMailer: Don't understand the from address

I'm trying to learn PHPMailer and I don't understand why it needs an email address and password.
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
It's not coming from a persons email, it's coming from the site. What email am I supposed to put here and why?
The email address is the address which the emails are sent from. The username and password are required for SMTP servers that require authentication in order to send mail.
The way email works is that party 1 has an email account which it uses to send mail to party 2. Party 1 can be a site, a person, a script or something else, but it still needs to have an email account to be able to send email.
This is the way SMTP (Simple Mail Transfer Protocol) has been defined, and you need to follow the definitions of that protocol to get your mail delivered.
It is not necessary to provide password -
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.example.com"; // SMTP server
$mail->From = "example#gmail.com";
$mail->AddAddress("example#gmail.com");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
Click here

I want to send email from my gmail account using php i am searching for answer on net since 4 hours but error appears

I am using wamp server and I have following code in my final.php. I am also
adding error that appears as below after code as comment kindly check that as.
I am newer to php. I am finding simple code so that I can sent message through my account, but I don't understand why this code do not have option for password.
Is password require? Please help me on this with detail and I also need to send email to multiple users simultaneously.
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->From = "63yogesh#gmail.com";
$mail->AddAddress("11ce028#charusat.edu.in");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
//The following From address failed: 63yogesh#gmail.com Message was not sent.Mailer error: The following From address failed: 63yogesh#gmail.com
SMTP server error: 5.7.0 Must issue a STARTTLS command first. jw8sm18271654pbc.73 - gsmtp
The example here says you have to set several data, such as authentication:
//Username to use for SMTP authentication
$mail->Username = "yourname#example.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
and SMTP port number:
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
EDIT:
This is the Basic Example using SMTP from their official site, try to edit it with your infos
require_once('../class.phpmailer.php'); //include("class.smtp.php"); //optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$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);
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

PHPMailer for SMTP sends mail to inbox but from invalid mail address

Need help!!!
I am using php mailer in my current project. It works successfully, after sending a mail successfully to the other end it says the mail comes from the smtp server account not from the $from what i use here-
$to ="muradautorun#gmail.com";
$from ="tanvir_cse0906#yahoo.com";
My smtp server code here..
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = "muradautorun";
$mail->Password = ""; //my passsword here
Here is my complete code-
$to ="muradautorun#gmail.com";
$from ="tanvir_cse0906#yahoo.com";
$from_name="S.M. Murad Hasan Tanvir";
$subject="A sending mail code";
$body ="This is a sending email code, the file attach here is about the coding.";
//here i use gmail as a smtp server
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // 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 = "muradautorun";
$mail->Password = ""; //my passsword here
$mail->SetFrom($from, $from_name);
$mail->addAttachment('emailSend.php'); // Add attachment(just the file url)
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
echo 'Mail error: '.$mail->ErrorInfo;
}
else {
echo 'Message sent!';
}
?>
Where i doing wrong i can't find out please help me...
Thanks in advance.
You need to allow sending mail as "tanvir_cse0906#yahoo.com" on your "muradautorun" gmail account first.
You can do this by following the following steps:
Login with "muradautorun"
Click the gear in the top right and choose "Settings"
Click on the "Accounts and Import" tab
Click on link "Add another email address you own" in the "Send mail as" section, and follow the instructions.

SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate

include("class.phpmailer.php");
include("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->Port = 465; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "******#gmail.com"; // your SMTP username or your gmail username
$mail->Password = "******"; // your SMTP password or your gmail password
$from = "***********#example.com"; // Reply to this email
$to="******#gmail.com"; // Recipients email ID
$name=" Name"; // Recipient's name
$mail->From = $from;
$mail->FromName = "Webmaster"; // Name to indicate where the email came from when the recepient received
$mail->AddAddress($to,$name);
$mail->AddReplyTo($from,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Sending Email From Php Using Gmail";
$mail->Body = "This Email Send through phpmailer, This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
am getting this error
SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.
Since you are using SSL. Add this line of code
$mail->SMTPSecure = 'ssl';
Also change your
$mail->Host = "ssl://smtp.gmail.com";
to
$mail->Host = "smtp.gmail.com";
Also, make sure if you have enabled the extension php_openssl.dll
if you are sure your username and password are correct
then do these steps
1 $mail -> SMTPSecure = 'ssl';
2 $mail->Host = "smtp.gmail.com";
3 https://support.google.com/mail/answer/78754
4 go to google acocunts then connected app and change to less secure apps on
follow all steps in number 3

Categories