PHPMailer, SMTP connect() failed error with Gmail - php

I’m trying to make a contact form and I’m using PHPMailer. I tried that on localhost with xampp and it works perfect. But when i upload to my host i get the error SMTP connect() failed.
Here is my code:
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPAuth = true;
$m->Host = "smtp.gmail.com";
$m->Username = "mymail#gmail.com";
$m->Password = "mypass";
$m->SMTPSecure = "ssl";
$m->Port = "465";
$m->isHTML();
$m->Subject = "Hello world";
$m->Body = "Some content";
$m->FromName = "Contact";
$m->addAddress('mymail#gmail.com', 'Test');
I've tried to change the port to 587 and the SMTPsecure to tls (and all the combinations). But doesn’t work. Any advice to solve this?
Thanks

This answer work form me: https://stackoverflow.com/a/47205296/2171764
I use:
$mail->Host = 'tls://smtp.gmail.com:587';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);

You may need to specify the address from which the message is going to be sent, like this:
$mail->From = 'user#domain.com';
I would also give isHTML a parameter, either true or false:
$m->isHTML(true);
Another option is trying to drop the port specification all together. There are several other parameters that you may find useful. The following example is code I've tested, see if you can adapt it for your uses:
$mail = new PHPMailer;
$mail->isSMTP();/*Set mailer to use SMTP*/
$mail->Host = 'mail.domain.com';/*Specify main and backup SMTP servers*/
$mail->Port = 587;
$mail->SMTPAuth = true;/*Enable SMTP authentication*/
$mail->Username = $username;/*SMTP username*/
$mail->Password = $password;/*SMTP password*/
/*$mail->SMTPSecure = 'tls';*//*Enable encryption, 'ssl' also accepted*/
$mail->From = 'user#domain.com';
$mail->FromName = $name;
$mail->addAddress($to, 'Recipients Name');/*Add a recipient*/
$mail->addReplyTo($email, $name);
/*$mail->addCC('cc#example.com');*/
/*$mail->addBCC('bcc#example.com');*/
$mail->WordWrap = 70;/*DEFAULT = Set word wrap to 50 characters*/
$mail->addAttachment('../tmp/' . $varfile, $varfile);/*Add attachments*/
/*$mail->addAttachment('/tmp/image.jpg', 'new.jpg');*/
/*$mail->addAttachment('/tmp/image.jpg', 'new.jpg');*/
$mail->isHTML(false);/*Set email format to HTML (default = true)*/
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
header("Location: ../docs/confirmSubmit.html");
}
Hope this helps!

Related

PHPMailer not working

I have tried my best to recieve message on a gmail account using phpmailer but i can't. It shows message sent successfully but i don't recieve any message. I even edited the sendmail.ini file. This is the code:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
//$mail->SMTPDebug = 2;
//$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myemail#gmail.com';
$mail->Password = 'mypassword';
$mail->SMTPSecure = 'ssl';
$mail->Port = 587;
$mail->setFrom('myemail#gmail.com', 'Mailer');
$mail->addAddress('example#gmail.com', 'Joe User');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
if($mail->send()){
echo 'Message has been sent successfully';
} else {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
I no so many questions concerning this have been answered but i still need help
try:
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
Hope it will help.
Change setFrom to SetFrom and try to change IP to 465.
You can also try the following.
$mail = new PHPMailer();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "email#gmail.com";
$mail->Password = "password";
$mail->SetFrom("example#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email#gmail.com");
if($mail->send()){
echo 'Message has been sent successfully';
} else {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
If you still get error than provide the error message.
First of all, try to uncomment line
$mail->isSMTP();
If you send mail via https, try to add following code:
$mail->SMTPOptions = array( 'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true ) );

How to send e-mail from localhost with phpmailer?

okay, so I already try it for many times. The results was not error but I didn't receive any e-mail in my inbox or spam folder
here is my mail.php
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
//$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "localhost"; // SMTP server
//IsSMTP(); // send via SMTP
$mail->SMTPDebug = true;
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com"; // SMTP server Gmail
$mail->Mailer = "gmail";
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = "henrikus.antony#gmail.com"; //
$mail->Password = "******"; // SMTP password
$webmaster_email = "henrikus.antony#gmail.com"; //Reply to this email ID
$email = "rikunime.share#gmail.com"; // Recipients email ID
$name = "Hendrikus Anthony"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Anthony";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Anthony");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Ini adalah Email HTML";
$mail->Body = "Ini adalah email contoh"; //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";
}
?>
please someone, I really need help. do I need a hosting? or there are something wrong with my syntax? whether sendmail.ini and php.ini affect the mail.php?
Here is my solution which I found out from couple of articles.
<?php
require_once "vendor/autoload.php";
$mail = new PHPMailer;
//Enable SMTP debugging.
$mail->SMTPDebug = 3;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "name#gmail.com";
$mail->Password = "password";
//If SMTP requires TLS encryption then set it
//$mail->SMTPSecure = "tls";
//Set TCP port to connect to
$mail->Port = 587;
$mail->From = "name#gmail.com";
$mail->FromName = "Full Name";
$mail->smtpConnect(
array(
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false,
"allow_self_signed" => true
)
)
);
$mail->addAddress("reciever#ymail.com", "Recepient Name");
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
This won't require any server settings on your localhost.
$mail->smtpConnect(
array(
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false,
"allow_self_signed" => true
)
)
);
This part of code asks smtp not to verify any connection and can send the mail without verifying the sender.
Also you need to enable IMAP settings from your mailbox settings.
Here are the links for reference.
https://www.sitepoint.com/sending-emails-php-phpmailer/
https://github.com/PHPMailer/PHPMailer/issues/368#issuecomment-75821110
You need an SMTP server to send out mail. Assuming you want to use this for testing purposes, try downloading a free SMTP local server such as this one.
If you want to actually send out mail in a production environment, consider using an external service such as SendGrid or MailChimp. Alternatively, if you want to stick with SMTP, you are going to need your own web server to send mail from.
You shouldn't comment out the line that tells the mailer to use smtp except you really want to use the normal mail function,which i don't think you want to
<?php
require 'PhpMailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
$mail->isSMTP();
// change this to 0 if the site is going live
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
//use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "xxxxxx#gmail.com";
$mail->Password = "******";
$mail->setFrom('xxx#ww.com', 'Somebody');
$mail->addReplyTo('xxx#ww.com', 'Somebody');
$mail->addAddress('xxx#ww.com', 'Somebody');
$mail->Subject = 'New contact from somebody';
// $message is gotten from the form
$mail->msgHTML($message);
$mail->AltBody = $filteredmessage;
if (!$mail->send()) {
echo "We are extremely sorry to inform you that your message
could not be delivered,please try again.";
} else {
echo "Your message was successfully delivered,you would be contacted shortly.";
}
?>
please note you must be connected to the internet for gmail's smtp to work

use phpmailer to send email from localhost to gmail: SMTP connect() failed

i want to send an email from localhost to gmail, but it says "SMTP connect() failed"
here is my code. I've also tried to change configuration php.ini and sendmail.ini but no luck.
Thank you.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'asd#gmail.com';
$mail->Password = 'password';
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('asd#gmail.com');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = 'Email'
$mail->Body = "This is body";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Try out the following :
Ensure that the PHPMailer library is included in the code.
Ensure that you are internet connection from localhost is not inhibited by any proxy.
Happy Coding :)
check the line $mail->Subject = 'Email' for a missing ;

SMTP connect() failed PHPmailer - PHP

I am new to PHP. I was trying to send myself a sample e-mail through PHPmailer. I am using gmail's smtp server. I am trying to send a sample mail from my gmail account to my yahoo account. But I am getting the error : Mailer Error: SMTP connect() failed.
Here is the code :
<?php
require "class.phpmailer.php";
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "ssl://smtp.gmail.com";
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "myemail#gmail.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password
$webmaster_email = "myemail#gmail.com"; //Reply to this email ID
$email="myyahoomail#yahoo.in"; // Recipients email ID
$name="My Name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->Port = 465;
$mail->FromName = "My Name";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"My Name");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "subject";
$mail->Body = "Hi,
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";
}
?>
I am using WAMP server on a Windows 7 64-bit machine. What could be the prob?
Please help me solve this. Thanks!
The solution of this problem is really very simple. actually Google start using a new authorization mechanism for its User.. you might have seen another line in debug console prompting you to log into your account using any browser.! this is because of new XOAUTH2 authentication mechanism which google start using since 2014.
remember.. do not use the ssl over port 465, instead go for tls over 587. this is just because of XOAUTH2 authentication mechanism. if you use ssl over 465, your request will be bounced back.
what you really need to do is .. log into your google account and open up following address
https://www.google.com/settings/security/lesssecureapps
and check turn on . you have to do this for letting you to connect with the google SMTP because according to new authentication mechanism google bounce back all the requests from all those applications which does not follow any standard encryption technique.. after checking turn on.. you are good to go..
here is the code which worked fine for me..
require_once 'C:\xampp\htdocs\email\vendor\autoload.php';
define ('GUSER','youremail#gmail.com');
define ('GPWD','your password');
// make a separate file and include this file in that. call this function in that file.
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->SMTPAutoTLS = false;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
You need to add the Host parameter
$mail->Host = "ssl://smtp.gmail.com";
Also, check if you have open_ssl enabled.
<?php
echo !extension_loaded('openssl')?"Not Available":"Available";
Solved an almost identical problem, by adding these lines to the standard PHPMailer configuration. Works like a charm.
$mail->SMTPKeepAlive = true;
$mail->Mailer = “smtp”; // don't change the quotes!
Came across this code (from Simon Chen) while researching a solution here, https://webolio.wordpress.com/2008/03/02/phpmailer-and-smtp-on-1and1-shared-hosting/#comment-89
Troubleshooting
You have add this code:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
And Enabling Allow less secure apps:
"will usually solve the problem for PHPMailer, and it does not really make your app significantly less secure. Reportedly, changing this setting may take an hour or more to take effect, so don't expect an immediate fix"
This work for me!
$mail->SMTPKeepAlive = true;
$mail->isSendMail(); // instead of isSMTP();
On shared hosting, your mail server may occasionally experience connection issues.
Here I found solution
[Edit]
Here is the link content, in case the link stop working for some reason (it happens)
PHPMailer and SMTP on 1and1 shared hosting
I use PHPMailer on my 1and1 shared hosting account. Recently, I could not send email anymore.
I tried debugging and this is the error that PHPMailer throws:
Language string failed to load: connect_host
After googling for a solution and trying different SMTP servers, accounts and SMTP ports, I decided to switch to sendmail and it worked like a charm! All I needed to do was to replace $mail->isSmtp() with:
$mail->isSendMail()
sendMail is located in its default location on 1and1 servers : /usr/sbin/sendmail, so no change of settings was required.
Conclusion:
1and1 probably closed its outgoing SMTP ports on its shared hosting servers. Consequently, if you use PHPMailer, don’t use SMTP mode anymore.
If anyone is still unable to solve the issue, please check following thread and follow callmebob's answer.
PHPMailer - SMTP ERROR: Password command failed when send mail from my server
I fixed it ...
https://github.com/PHPMailer/PHPMailer/tree/5.2-stable
<?php
require 'PHPMailerAutoload.php';
$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 = 'm7#gmail.com'; // SMTP username
$mail->Password = 'pass'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->setFrom('m7#gmail.com', 'Mailer');
$mail->addAddress('dot#gmail.com', 'User'); // Add a recipient
$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';
}
Turn on access and enjoy..! That is on Gmail account setting.
You are missing the directive that states the connection uses SSL
require ("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // turn of SMTP authentication
$mail->Username = "YAHOO ACCOUNT"; // SMTP username
$mail->Password = "YAHOO ACCOUNT PASSWORD"; // SMTP password
$mail->SMTPSecure = "ssl";
$mail->Host = "YAHOO HOST"; // SMTP host
$mail->Port = 465;
Then add in the other parts
$webmaster_email = "myemail#gmail.com"; //Reply to this email ID
$email="myyahoomail#yahoo.in"; // Recipients email ID
$name="My Name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "My Name";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"My Name");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "subject";
$mail->Body = "Hi,
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";
}
As a side note, I have had trouble using Body + AltBody together although they are supposed to work. As a result, I wrote the following wrapper function which works perfectly.
<?php
require ("class.phpmailer.php");
// Setup Configuration for Mail Server Settings
$email['host'] = 'smtp.email.com';
$email['port'] = 366;
$email['user'] = 'from#email.com';
$email['pass'] = 'from password';
$email['from'] = 'From Name';
$email['reply'] = 'replyto#email.com';
$email['replyname'] = 'Reply To Name';
$addresses_to_mail_to = 'email1#email.com;email2#email.com';
$email_subject = 'My Subject';
$email_body = '<html>Code Here</html>';
$who_is_receiving_name = 'John Smith';
$result = sendmail(
$email_body,
$email_subject,
$addresses_to_mail_to,
$who_is_receiving_name
);
var_export($result);
function sendmail($body, $subject, $to, $name, $attach = "") {
global $email;
$return = false;
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = $email['host']; // SMTP server
// $mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = $email['host']; // sets the SMTP server
$mail->Port = $email['port']; // set the SMTP port for the GMAIL server
$mail->SMTPSecure = "tls";
$mail->Username = $email['user']; // SMTP account username
$mail->Password = $email['pass']; // SMTP account password
$mail->AddReplyTo($email['reply'], $email['replyname']);
if(stristr($to,';')) {
$totmp = explode(';',$to);
foreach($totmp as $destto) {
if(trim($destto) != "") {
$mail->AddAddress(trim($destto), $name);
}
}
} else {
$mail->AddAddress($to, $name);
}
$mail->SetFrom($email['user'], $email['from']);
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($body);
if(is_array($attach)) {
foreach($attach as $attach_f) {
if($attach_f != "") {
$mail->AddAttachment($attach_f); // attachment
}
}
} else {
if($attach != "") {
$mail->AddAttachment($attach); // attachment
}
}
$mail->Send();
} catch (phpmailerException $e) {
$return = $e->errorMessage();
} catch (Exception $e) {
$return = $e->errorMessage();
}
return $return;
}
if everything fails then for gmail you must turn on access to 3rd party apps to connect to ur gmail account.
https://www.google.com/settings/security/lesssecureapps // turn it
on
Just make sure you passed the right parameters
eg. the correct outgoing server host, username, and password
$mail->SMTPDebug = false;
$mail->Host = 'email_smtp_host'
$mail->SMTPAuth = false
$mail->Username = 'username'
$mail->Password = 'password'
$mail->SMTPSecure 'tls'
$mail->Port = '587'
If you're using VPS and with httpd service, please check if your httpd_can_sendmail is on.
getsebool -a | grep mail
to set on
setsebool -P httpd_can_sendmail on
Try adding this line to your script. This worked for me!
$mail->Mailer = “smtp”;
This is usually a result of the server not accepting SSLv2 or SSLv3 connections which is a standard for cPanel/WHM in favor of TLS only connections. You can check this by going to WHM>>Service Configuration>>Exim Configuration Manager -> Options for OpenSSL
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
<?php
use PHPMailer\PHPMailer\PHPMailer;
require_once "PHPMailer/src/Exception.php";
require_once "PHPMailer/src/PHPMailer.php";
require_once "PHPMailer/src/SMTP.php";
$mail = new PHPMailer();
$mail ->isSMTP();
$mail ->Host ="smtp.gmail.com";
$mail -> SMTPAuth = true;
$mail ->Username = 'youremail#gmail.com';
$mail ->Password = 'password';
//$mail ->SMTPSecure=PHPMailer::ENCRYPTION_STARTTLS;
$mail ->Port = '587';
$mail ->SMTPSecure ='tls';
$mail ->isHTML(true);
$mail ->setFrom('youremail#gmail.com','email send name');
$mail ->addAddress('receiver#gmail.com');
$mail ->Subject = 'HelloWorld';
$mail ->Body = 'a test email';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
if ($mail->send()){
echo "Message has been sent successfully";
}else{
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
make sure that you configure less secure app settings in gmail using this link https://www.google.com/settings/security/lesssecureapps and make sure you downloaded PHPMailer-master.zip from github
working fine with MAMP server locally :)
After days of searching, I found out that the protocol name needs to be in lowercase.
$mail->SMTPSecure = "ssl"; # lowercase
find the "class.smtp.php" file
original:
$this->smtp_conn =fsockopen(
$host,
$port,
$errno,
$errstr,
$timeout
);
change to:
$this->smtp_conn = #stream_socket_client(
$host,
$port,
$errno,
$errstr,
$timeout
);

PHPMAILER Not sending and not giving error

I am trying to let users fill out a contact form, which will then be sent to my email. But its not working for some reason. I just get a blank page with no error message or any text and email is also not sent.
if (isset($_POST['submit']))
{
include_once('class.phpmailer.php');
$name = strip_tags($_POST['full_name']);
$email = strip_tags ($_POST['email']);
$msg = strip_tags ($_POST['description']);
$subject = "Contact Form from DigitDevs Website";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
//$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "info#example.com"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress('info#example.com', 'Information');
$mail->AddReplyTo($email, 'Wale');
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$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;
exit;
}
echo 'Message has been sent';
You need to call:
$mail = new PHPMailer(true); // with true in the parenthesis
From the documentation:
The true param means it will throw exceptions on errors, which we need
to catch.
Its working now, i didnt include the 'class.smtp.php' file. The working code is below:
if (isset($_POST['submit']))
{
include_once('class.phpmailer.php');
require_once('class.smtp.php');
$name = strip_tags($_POST['full_name']);
$email = strip_tags ($_POST['email']);
$msg = strip_tags ($_POST['description']);
$subject = "Contact Form from DigitDevs Website";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
//$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "info#example.com"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress('info#example.com', 'Information');
$mail->AddReplyTo($email, 'Wale');
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$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;
exit;
}
echo 'Message has been sent';
I had the same problem with no error message even with SMTPDebug enabled. After searching around for working examples I noticed that I didn't include the SMTP Secure value. Try adding this line:
$mail->SMTPSecure = 'ssl'; //secure transfer enabled
Work like a charm now.
I had a similar problem. In reference to #Syclone's answer. I was using the default "tls".
$mail->SMTPSecure = 'tls';
After I changed it to
$mail->SMTPSecure = 'ssl';
It worked ! My mailserver was only accepting connections over SSL.
What worked for me was setting From as Username and FromName as $_POST['email']
Hope this helps
PHPMailer use exception.
Try this
try {
include_once('class.phpmailer.php');
$name = strip_tags($_POST['full_name']);
$email = strip_tags ($_POST['email']);
$msg = strip_tags ($_POST['description']);
$subject = "Contact Form from DigitDevs Website";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
//$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "info#example.com"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress('info#example.com', 'Information');
$mail->AddReplyTo($email, 'Wale');
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->Send();
exit;
} catch (phpmailerException $e) {
echo $e->errorMessage(); //error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage();
}
I was trying to load an HTML file to send, which did not belong to the www-data group on my Ubuntu server.
chown -R www-data *
chgrp -R www-data *
Problem solved!
I was debating whether to write my own handler or crow-bar PHPMailer into my existing class structure. In the event it was very easy because of the versatility of the spl_autoload_register function which is used within the PHPMailer system as well as my existing class structure.
I simply created a basic class Email in my existing class structure as follows
<?php
/**
* Provides link to PHPMailer
*
* #author Mike Bruce
*/
class Email {
public $_mailer; // Define additional class variables as required by your application
public function __construct()
{
require_once "PHPMail/PHPMailerAutoload.php" ;
$this->_mailer = new PHPMailer() ;
$this->_mailer->isHTML(true);
return $this;
}
}
?>
From a calling Object class the code would be:
$email = new Email;
$email->_mailer->functionCalls();
// continue with more function calls as required
Works a treat and has saved me re-inventing the wheel.
Try this ssl settings:
$mail->SMTPSecure = 'tls'; //tls or ssl
$mail->SMTPOptions = array('ssl' => array('verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true));

Categories