Trying to send mail through PHPMailer
Error message:Fatal error: Uncaught Error: Object of class PHPMailer\PHPMailer\PHPMailer could not be converted to string in C:\xampp\htdocs\ummasite\MailHandler.php:54 Stack trace: #0 {main} thrown in C:\xampp\htdocs\ummasite\MailHandler.php on line 54
Code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/PHPMailer/src/Exception.php';
require 'PHPMailer/PHPMailer/src/PHPMailer.php';
require 'PHPMailer/PHPMailer/src/SMTP.php';
//Create a new PHPMailer instance
$mail = new PHPMailer(true);
$mail -> isSMTP(true);
$mail -> isHTML(true);
$mail -> Host = 'smtp.gmail.com';
$mail -> Port = '587';
//Set the encryption mechanism to use - STARTTLS or SMTPS
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail account you created credentials earlier
$mail-> Username = 'voiceovers4u#gmail.com';
$mail -> Subject = 'UMMA Database Change';
//Password to use for SMTP authentication - the 16 character password generated you copied before (no brakes)
$mail -> Password = "usonvaejwrzjwmem";
$mail -> Sender = "voiceovers4u#gmail.com";
$mail -> To = "voiceovers4u#gmail.com";
$mail -> AddAddress('voiceovers4u#gmail.com', 'PappaBear');
$mail -> Subject ="UMMA Correspondence";
$mail -> Context = "Sending content using PHPMail function";
$mail -> From = "voiceovers4u#gmail.com";
$Country = $_GET['Country'];
$UFName = $_GET['FName'];
$ULName = $_GET['LName'];
$UGuestID = $_GET['Email'];
$Phone = $_GET['Phone'];
$Org = $_GET['Org'];
$USubject = $_GET['subject'];
$UMessage = $_GET['message'];
$mail -> Body = "
<h1>Message from UMMA Website</h1>
<p>$UFName $ULName</p>
<p>$UGuestID</p>
<p>$Phone</p>
<p>$Org</p>
<p>$USubject</p>
<p>$UMessage</p>
php if($UReply!=1) ?>
<p>Reply Unnecessary</p>;
php else
<p>Please reply</p>;
php endif ?>
';
$mail -> AltBody = strip_tags(Body);
$mail -> send();
$str1 = 'Successful!';
$str2 = 'Unsuccessful!';
phpif($emailStatus) {
echo $str1 .;
} else {
echo $str2 .;
}";
?>
What can I do to stop this Fatal Error. Thank you in advance!
This question already has answers here:
Send email using the GMail SMTP server from a PHP page
(16 answers)
Phpmailer using smtp with Gmail not working - connection timing out
(8 answers)
PEAR Mail unable to connect to Gmail SMTP, failed to connect to socket
(9 answers)
PHPMailer with GMail: SMTP Error
(2 answers)
Closed 4 years ago.
Im trying to send an email on localhost(wamp) but it gives me the error on top!
I also tried with 'tls' and port 587 but same error
my email is configured to receive insecure connection and Imap option is activated ..
pls help me , I need this for school project
<?php
$mailto = $_POST['mail_to'];
$mailSub = $_POST['mail_sub'];
$mailMsg = $_POST['mail_msg'];
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail ->IsSmtp();
$mail ->SMTPDebug = 2;
$mail ->SMTPAuth = true;
$mail ->SMTPSecure = 'ssl';
$mail ->Host = "smtp.gmail.com";
$mail ->Port = 465; /* or 587;*/
$mail ->IsHTML(true);
$mail ->Username = "xxxxx";
$mail ->Password = "xxxxx";
$mail ->SetFrom("xxxxx");
$mail ->Subject = $mailSub;
$mail ->Body = $mailMsg;
$mail ->AddAddress($mailto);
if(!$mail->Send())
{
echo "Mail Not Sent";
}
else
{
echo "Mail Sent";
}
?>
I tried to change port number and 'ssl' to 'tls'. I also activated extension=php_openssl.dll in php.ini, restarted the server and allowed the low security applications on Gmail (https://www.google.com/settings/security/lesssecureapps)
These solutions seem to work for everyone but not me! can you help? here is my code:
<?php
session_start();
include 'dbConnector.php';
$toAddress = $_POST['toEmail'];
$emailSubject = $_POST['subject'];
$emailContent = $_POST['content'];
$fromPassword = $_POST['frompassword'];
require 'PHPMailer-master/class.phpmailer.php';
require 'PHPMailer-master/PHPMailerAutoload.php';
require 'PHPMailer-master/class.smtp.php';
$mail = new PHPMailer();
$mail -> IsSMTP();
$mail -> SMTPDebug = 1;
$mail -> SMTPAuth = true;
$mail -> SMTPSecure = 'ssl';
$mail -> HOST = "smtp.gmail.com";
$mail -> Port = 465; //587
$mail -> IsHTML(true);
$mail -> Username = $_SESSION['loggedUserEmail'];
$mail -> Password = $fromPassword;
$mail -> SetFrom($_SESSION['loggedUserEmail']);
$mail -> Subject = $emailSubject;
$mail -> Body = $emailContent;
$mail -> AddAddress($toAddress);
if(!$mail -> Send ())
{
echo "Your mail has not been sent!";
}
else
{
echo "Your email has been sent successfully!";
}
?>
I am trying to send an email to user and admin using php-mailer.But when I send the mail, the user Acknowledgement mail is also getting sent to the admin. The e mails getting sent are in proper format. What is the issue here?
My Code is Here:
$name = "User Name";
$email = "user#gmail.com";
$mobile = 'User Phone No.';
$body = "<div><div>Name: <b>$name</b></div> <div>Email: <b>$email</b></div> <div>Mobile: <b>$mobile</b></div></div>";
//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
date_default_timezone_set('Etc/UTC');
require 'PHPMailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail -> isSMTP();
function sendEmail($mail, $from, $password, $to, $body, $subject) {
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail -> SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail -> Debugoutput = 'html';
//Set the hostname of the mail server
$mail -> Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail -> Port = 587;
//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 = $from;
//Password to use for SMTP authentication
$mail -> Password = $password;
//Set who the message is to be sent from
$mail -> setFrom($from, $from);
//Set who the message is to be sent to
$mail -> addAddress($to, $to);
//Set the subject line
$mail -> Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail -> msgHTML($body);
//Replace the plain text body with one created manually
$mail -> AltBody = 'This is a plain-text message body';
$sucess = $mail -> send();
// //send the message, check for errors
// if (!$sucess) {
// echo "Mailer Error: " . $mail -> ErrorInfo;
// } else {
// echo "Message sent!";
// }
}
$from = "noreplyadmin#gmail.com";
$maiAdmin = "admin#gmail.com";
$password = "password";
$subject = "Enquiry";
// Send Mail to admin
sendEmail($mail, $from, $password, $maiAdmin, $body, $subject);
// Send Mail to User
$userMessage = "We Received your request. Our representative will contact you soon.";
sendEmail($mail, $from, $password, $email, $userMessage, 'Acknowlwdgement');
You are using the same instance and have not cleared the previous out of the PHPMailer $mail instance. You can either clear the old data out of the PHPMailer Instance or do this:
$adminmail = new PHPMailer();
$adminmail -> isSMTP();
$usermail = new PHPMailer();
$usermail -> isSMTP();
sendEmail($adminmail, $from, $password, $maiAdmin, $body, $subject);
$userMessage = "We Received your request. Our representative will contact you soon.";
sendEmail($usermail, $from, $password, $email, $userMessage, 'Acknowlwdgement');
Whatever floats your boat I suppose.
This is my php file:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
if(isset($_POST['send'])){
date_default_timezone_set('Etc/UTC');
$email = $_POST['email'];
$name = $_POST["name"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$mail -> isSMTP();
$mail->PluginDir = "/path/to/phpmailer/dir";
$mail -> SMTPDebug = 1;
$mail -> Host = "smtp.gmail.com";
$mail -> SMTPSecure = "ssl";
$mail -> Port = 465;
$mail -> SMTPAuth = true;
$mail -> Username = "xxx"; //username example#gmail.com
$mail -> password = "xxx"; //password
$mail -> setFrom("autobandendiscount#gmail.com", "Anwar Elbouhdifi");
$mail -> addAddress($email, $name);
$mail -> Subject = $subject;
$mail -> Body = $message;
$mail -> AltBody = $message;
if(! $mail -> send()){
echo"message error: " . $mail -> ErrorInfo;
}else{
echo"Success!" ;
}
}
?>
And the mailer returns this error
2014-05-28 22:51:05 CLIENT -> SERVER: EHLO localhost 2014-05-28 22:51:05 CLIENT -> SERVER: AUTH LOGIN 2014-05-28 22:51:05 CLIENT -> SERVER: YXV0b2JhbmRlbmRpc2NvdW50QGdtYWlsLmNvbQ== 2014-05-28 22:51:05 CLIENT -> SERVER: 2014-05-28 22:51:05 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 r5sm46692086wjq.26 - gsmtp 2014-05-28 22:51:05 CLIENT -> SERVER: QUIT SMTP connect() failed. message error: SMTP connect() failed.
I know it says username and password not accepted but I know for 100% it is the right password and username.
Probably not the solution but worth a try: spell Password with a big P.