I've been using the default mail function of PHP before but decided to use another service for emailing features. I used PHPMailer but it is not working.
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Host = gethostbyname('smtp.gmail.com');
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "xxx#gmail.com";
$mail->Password = "xxx";
$mail->setFrom('xxx#gmail.com', 'Name');
$mail->addReplyTo('xxx#gmail.com', 'no-reply');
$mail->addAddress('xxx#gmail.com', 'Name');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->AltBody = 'This is a plain-text message body';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
The code also doesn't echo any errors.
It does not echo any errors, but I think it also doesn't echo 'Message sent!'
Try the following
Append the brackets
$mail = new PHPMailer();
Set debug to messages AND ERRORS for testing purpose
$mail->SMTPDebug = 1;
To send HTML Body, either after setting $mail->Body call
$mail->IsHTML(true);
Or use the Method to set HTML body which will combine both of them:
$mail->MsgHTML('This is the HTML message body <b>in bold!</b>');
Try this and see if it works
EDIT:
Also look that the casing is as suggested (I am not sure if this may give errors, but just to be sure)
$mail->SetFrom
$mail->AddReplyTo
$mail->AddAddress
Related
I had an error with the sending of an e-mail with php, because I have an smtp server name call smtp.office365.com, how should I try?
<?php
require "PHPmailer/src/PHPMailer.php";//Am I correctly include thelibraries?
require "PHPmailer/src/smtp.php";
$mail = new PHPMailer(true); >I get an error
$mail->isSMTP();
$mail->Host='smtp.office365.com';
$mail->Port=25;
$mail->SMTPAuth= true;
$mail->Username='username';//What username have I had to type?
$mail->Password='password';//The password is above office365 email?
$mail->SetFrom('sender_email', 'FromEmail');
$mail->addAddress('destination', 'ToEmail');
$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';
?>
I get an error, because when I try to execute It on a server Linux It won't send the email and It gave me a 'HTTP ERROR 500'
$mail->Host=smtp.office365.com
$mail->Port=587
$mail->Username=XXXXXXX
$mail->Password=XXXXXXX
$mail->Encryption=tls
require "PHPmailer/src/PHPMailer.php";
require "PHPmailer/src/smtp.php";
$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 = 'tls'; // secure transfer enabled REQUIRED for 365
$mail->Host = 'smtp.office365.com';
$mail->Port = 25;
$mail->Username = 'username';
$mail->Password = "password";
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress('myemail#gmail.com');
Mailer Error: SMTP Error: Could not connect to SMTP host.;
this error is generating when i run this code in godaddy.
<?php
require("PHPMailer_5.2.0/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtpout.secureserver.net"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username#domain.com"; // SMTP username
$mail->Password = "******"; // SMTP password
$mail->From = "username#domain.com";
$mail->FromName = "User";
$mail->AddAddress("Sendto#gmail.com"); // name is optional
$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. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
Try below code it's working for me!
<?php
require("src/PHPMailer.php");
require("src/SMTP.php");
require("src/Exception.php");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->SMTPAutoTLS = false;
$mail->Port = 25;
//Recipients
$mail->setFrom('from#gmail.com', 'Mailer');
$mail->addAddress('to#gmail.com', 'XYZTABC');
//Content
$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 "<pre>";
print_r($e);
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
If i understand correctly, using the SMTP address you are using you need to specify the port too (465).
However, according to the godaddy documentation, you should be using the following SMTP server:
relay-hosting.secureserver.net
This code may help you.
$mail->Host = "smtpout.secureserver.net";
your host name should be like mydomain.com
$mail->Port = 465;
i solved this using localhost as smtp server, port 25, no ssl and no authentication.
I'm using wordpress, with divi theme on a godaddy server.
Here is my code. I really need to know what is wrong with my code.I am getting this error:-
"SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting"
<?php
require_once 'PHPMailer/class.phpmailer.php';
//sending mail for verification
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->Host = 'mail.mydomain.com';
$mail->Username = 'myusername'; // Enter your SMTP username
$mail->Password = "mypassword"; // SMTP password
$mail->From = "support#mydomain.com";
$mail->FromName = " Verification";
$mail->AddAddress("emailid#yahoo.com", "Name");
$mail->AddReplyTo("myemail#gmail.com", "Information");
$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";
if($mail->Send()){
echo "Mail sent";
}else{
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
My credentials are correct.I did follow the link in the error message and tried almost everything mentioned there but nothing seems to work for me. This is the shortest and trimmed version of my original code, the idea is to tell that even this is not working for me.
I found the solution my this problem. Here are the changes I made and it works perfectly fine.
<?php
require_once 'PHPMailer/class.phpmailer.php';
require_once 'assets/import/PHPMailer/class.smtp.php'; //I added this here which wasn't added in original script
//sending mail for verification
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->Host = 'mail.mydomain.com';
$mail->Username = 'myusername'; // Enter your SMTP username
$mail->Password = "mypassword"; // SMTP password
$mail->From = "support#mydomain.com";
$mail->FromName = " Verification";
$mail->AddAddress("emailid#yahoo.com", "Name");
$mail->AddReplyTo("myemail#gmail.com", "Information");
// Adding SMTPOption is the main thing that solved my problem. I got this from troubleshooting page of PHPMailer. And they
// recommended to do this only if every other option fails.
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$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";
if($mail->Send()){
echo "Mail sent";
}else{
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
I am working on phpmailer but I am receiving this error, I dont know what is wrong. I search and did many changes. but noting helped me.
I am testing both on live server and on localhost.
on both I am receiving the error
Message could not be sent.Mailer Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Please have a look on my code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
require("PHPMailer/class.phpmailer.php"); // path to the PHPMailer class
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'stockholm85#gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'agohar1985#gmail.com';
$mail->FromName = 'Mailer';
//$mail->addAddress('zia_gt#yahoo.com', 'Zia Ullah'); // Add a recipient
$mail->addAddress('zia_gt#yahoo.com'); // Name is optional
//$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 = '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';
}
?>
I have folder on name PHPMailer and mail.php is the file.
I will appreciate your support because I am new to php
Thank you in advance
open your cmd and type the following to get php-mailer,
wget https://github.com/PHPMailer/PHPMailer/archive/master.zip
unzip the master. mention the file path clearly in your code.
Here
<?php
require 'PHPMailer-master/PHPMailerAutoload.php'; //file path
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'user#gmail.com';
$mail->Password = '_password_';
$mail->SMTPSecure = 'tls';
$mail->From = 'sender#example.com';
$mail->FromName = 'Your Name';
$mail->addAddress('recipient#example.com');
$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';
}
?>
test it
I've looked through all the old posts on stackoverflow and many on other websites and find lots of posts with the same error, but none of the fixes work.
The following is the error I get:
Mailer Error: Could not instantiate mail function.
Is there something missing or incorrect?
<?php
require '../PHPMailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "censored#gmail.com";
$mail->Password = "censored";
$mail->setFrom('censored#gmail.com');
$mail->addReplyTo('censored#gmail.com');
$mail->addAddress('censored#gmail.com');
$mail->Subject = 'PHPMailer mail() test';
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
I found what you're missing.
You need to set these 2 things to get it to work properly:
$mail->isSMTP();
$mail->SMTPSecure = 'ssl';