I am using wordpress locally on my laptop and I want to use smtp to send emails to users from the website page (not from the wordpress dashboard). I wrote the code in the page template and it tells me that the email was sent successfully but when I check the inbox and the junk or spam for the recipient email I do not find any emails received from the sender.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
$mail = new PHPMailer;
//$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'me#gmail.com';
$mail->Password = 'gmail_password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('me#gmail.com', 'Atheer');
$mail->addReplyTo('me#gmail.com', 'Atheer');
$mail->addAddress('ahood.q26#gmail.com');
$mail->isHTML(true);
// Mail subject
$mail->Subject = 'Email from Localhost by CodexWorld';
$bodyContent = '<h1>Hello</h1>';
$bodyContent .= '<p>This is Atheer<b>CodexWorld</b></p>';
$mail->Body = $bodyContent;
if(!$mail->send()) {
echo 'Message could not be sent. Mailer Error: '.$mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
I am commenting the $mail->isSMTP(); becuase it was giving errors. I even tried the tls encryption and same problem it tells me send successfully while it is not sent.
Related
Could not instantiate mail function.
Message couldnot be sentMailer Error: Could not instantiate mail function.
require 'PHPMailer/PHPMailerAutoload.php';
$mail =new PHPMailer;
$mail->HOST ='localhost';
$mail->PORT = 25;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Username = 'Username';
$mail->Password = 'Pass';
$mail->setFrom('info#zoeticpolymers.com');
$mail->addAddress('nitesh54546#gmail.com','Nitesh');
$mail->AddReplyTo("info#zoeticpolymers.com");
$mail->isHTMl(true);
$mail->Subject = 'PHP Mailer Subject';
$mail->Body = '<h1>You are Welcome Here.....</h1>';
if(!$mail->send()){
echo 'Message couldnot be sent';
echo 'Mailer Error: ' . $mail->ErrorInfo; die;
}else{
echo 'Message has been sent'; die;
}
Have you tried with smtp? The cause of Could not instantiate mail function error may be more than one reason. when you try to send large emails and your PHP error log contains the message Can not send a message: Too big then your mail transfer agent (Sendmail, postfix, Exim, etc) is refusing to deliver these emails.
The solution is to configure the MTA to allow larger attachments. But this is not always possible. The alternate solution is to use SMTP. You will need access to a SMTP server (and login credentials if your SMTP server requires authentication), consider the given example.
$mail->IsSMTP();
$mail->Host = "smtp.example.com";
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
Your Mistake: You used HOST as Localhost it's SMTP.mail.com change mail to your server
You are using older version of PHPMailer also let me help you by typing the correct one
You should download from PHPMailer GitHub
Not vendor for this example
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; //for Gmail
$mail->SMTPAuth = true;
$mail->Username = 'user#gmail.com';
$mail->Password = 'your Gmail pass';
$mail->Port = 587; // TCP port
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User');
$mail->isHTML(true);
$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';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}```
**Your error was that you used HOST as localhost and older version of PHPMailer.
But use default mail() but change the PHP version to 7.3 since it's better now.**
If your code looks good, try to install PostFix on your server.
sudo apt-get install postfix
It worked for me on digital ocean.
got a quick question about my contact form. When u send a message then the page reload and u coming to the header of the page. I would like to have it that you stay on the contact section when u send the message and that under the send button a text shows up like "hey, the message is send" I got in the code my homepage as link but I have a one-age site so I dont know how to fix the section that it stands on the contact section...
here is the code from the phpmailer:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.test.de'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'MY EMAIL'; // SMTP username
$mail->Password = 'MY PASSWORT!'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('MY EMAIL');
$mail->addAddress($_POST['mail']); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $_POST['subject'];
$mail->Body = $_POST['text'];
$mail->send();
//echo 'Message has been sent';
header('Location: http://www.example.com/contact.php');
exit();
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
if you need the homepage to see what I mean: www.yveslumiere.com
Cheers Yves :-)
I'm trying to send an email every time someone inputs a form on my website but the SMTP isn't working. It keeps on showing me this error. The credentials of the email account are correct. Also, I have already tried SMTPAuth = False; but still the same result.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port = 587;
$mail->SMTPSecure = 'TLS';
$mail->SMTPAuth = true;
$mail->Username = 'sender#test.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
//Recipients
$mail->setFrom('test#test.com', ' Services');
$mail->addAddress('recipient#test.com', $name); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Contact Us Message';
$mail->Body = 'Sent a message, This is the message :';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
header('Location: tempage.php');
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
What is going wrong? Thanks for your help.
I'm also struggling 2 days with same issue.
After i find this solution. It's working fine.
Try this.
$mail->Host = 'smtp.office365.com';
change to
$mail->Host = 'smtp.live.com';
can someone help me to find what is wrong with the following code that i used to send mail in php.
error_reporting(E_STRICT);
date_default_timezone_set('asia/kolkata');
require_once('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 = "manikandan#gmail.com";
$mail->Password = "mypassword";
$mail->From = ("manikandan#gmail.com");
$mail->SetFrom("manikandan");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("peter1991#gmail.com");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
i am getting the following error:- 2015-01-05 04:32:10 Invalid address: manikandan
i am using php 5.2.2 ,Apache 2.0 Handler in windows.
setFrom expects an email address as the first parameter. Try:
$mail->setFrom('manikandan#gmail.com');
If you also want to set a proper name, use the second parameter
$mail->setFrom('manikandan#gmail.com', 'John Smith');
http://phpmailer.github.io/PHPMailer/classes/PHPMailer.html#method_setFrom
Also, I think you should be using the autoloader instead of including the PHPMailer class directly.
try this:
$mail->setFrom('manikandan#gmail.com', 'manikandan'); //setFrom expect email
Full code:
<?php require 'PHPmailer/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 = 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 = 465;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "manikandan#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "mypassword";
//Set who the message is to be sent from
$mail->setFrom('manikandan#gmail.com', 'manikandan');
//Set an alternative reply-to address
$mail->addReplyTo('peter1991#gmail.com', 'peter');
//Set who the message is to be sent to
$mail->addAddress('peter1991#gmail.com', 'peter1991');
//Set the subject line
$mail->Subject = 'Test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->Body = "Hi ,! Welcome to PHP mail function. \n\n .";
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.gif');
$mail->SMTPAuth = true;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
To resolved this just turn on access for less secure app in your google account. Hope this helps.
I am trying to send an email with Gmail smtp, but I dont send any mail.
Neither do I receive any errors, I just get an empty page.
Here is my code, I hope you can help me with this.
<?php
require 'class.phpmailer.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->Username = "MyEmail";
$mail->Password = "Mypassword";
$mail->setFrom('MyEmail');
$mail->addReplyTo('MyEmail');
$mail->addAddress('MyEmail');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->Body = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Please note that I entered the correct emails and passwords.
By default PHPMailer will send mail using php's mail() function (sendmail). You need to include smtp class for phpMailer to able to send mail using SMTP
require 'class.phpmailer.php';
require 'class.smtp.php';
you can get it from github