I am implementing mail functionality by using PHP mailer.
The code is not working on online but its working on local machine. On local machine code sends the mail successfully but on online website it is showing following error:
SMTP -> ERROR: Failed to connect to server: ()
SMTP Error: Could not connect to SMTP host.
Code is:
<?php
include "classes/class.phpmailer.php"; // include the class name
$mail1 = new PHPMailer(); // create a new object
$mail1->IsSMTP(); // enable SMTP
$mail1->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail1->SMTPAuth = true; // authentication enabled
$mail1->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail1->Host = "smtp.gmail.com";
$mail1->Port = 465; // or 587
$mail1->IsHTML(true);
$mail1->Username = "yourmail#gmail.com";
$mail1->Password="password";
$mail1->SetFrom("yourmail#gmail.com");
$mail1->Subject = "Working";
$mail1->Body ="Hi, you got email";
$mail1->AddAddress("yourmail2#gmail.com");
$mail1->Send();
?>
First you have to edit the "php.ini" To find this file display the phpinfo by using following code from the WAMP server. Create one php file and add this content.
<?php
echo phpinfo();
?>
There search for "Loaded Configuration File" That will be the path to your php.ini.
In this file remove the ;(semi colon) given to extension=php_openssl.dll.
After downloading PHPMailer package
Extract Copy the full folder into your project folder.
Use the doc folder for help. In test folder there is one file called testemail.php.
Change the parameter as your need. (Example given below).
Then in the browser type 127.0.0.1/PHPMailer/test/testemail.php.
Then it will show successful message if email sent, else it will give error message.
Example:
//add these codes if not written
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465;
//You have to change these parameters to your requirements.
$mail->Username = “abc#gmail.com”; // GMAIL username
$mail->Password = “abcdefgh”; // GMAIL password
//..code ... There are many other functions to attach file etc.. For that refer doc file.
$mail->AddAddress(“destination#gmail.com”,”Nick name”);
Related
I have written some code to send email. Now i have multiple scripts that uses PHPMailer to send emails. So what i did was i wrote all my SMTP settings in a file after including PHPMailerAutoLoad.php but it still is throwing an error 'Class PHPMailer not found.'
Following is the code snippet of mailsetting.ph which has settings regarding my SMTP Server.
<?php
include("/../PHPMailer/PHPMailerAutoload.php");
$mail= new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->Port =465;
$mail->SMTPSecure = 'ssl';
$mail->From='info#vishalkhare.com';
$mail->FromName='Vishal Khare';
$mail->addReplyTo('info#vishalkhare.com', 'Information');
?>
It says 'Fatal error: Class 'PHPMailer' not found in mailsetting.php on line 3' which clearly means that include statement is executing properly. But Still Class PHPMailer is not included.
I also tried including the class.phpmailer.php file
<?php
include("../class.phpmailer.php");
include("../PHPMailer/PHPMailerAutoload.php");
?>
It is still not working. Please help me out and let me know what to do to include PHPMailer class in the file?
P.S. - It is working on a XAMPP Server but not working as i move the code to a webserver. A godaddy Web server precisely
Problem is not in loading PHPMailAutoLad. Unfortunately Godaddy doesn't support emails of gmail,yahoo,aol,live,hotmail,etc., IF you are using linux cPanel hosting panel then make these changes in your php files
$mail->Host = 'localhost';
$mail->Port = 25;
$mail->ssl = false;
$mail->authentication = false;
I have a hosting server where i have written a php code which is giving me an error, i cant access php.ini as its a hosting server to change anythng
Error : Fatal error: require(): Failed opening required 'PHPMailer-master/PHPMailerAutoload.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/vhosts/evoting.freevar.com/vote/mailtest.php on line 2
PHP Code:
<?php
require 'PHPMailer-master/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 = 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 = "sample#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom('sample#gmail.com', 'Evoting System');
//Set an alternative reply-to address
$mail->addReplyTo('sample#gmail.com', 'Evoting System');
//Set who the message is to be sent to
$mail->addAddress('azaz3#gmail.com', '');
//Set the subject line
$mail->Subject = 'EVoting Otp ';
//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('23bj34');
//Replace the plain text body with one created manually
$mail->AltBody = 'sddsfsd23';
//Attach an image file
// $mail->addAttachment('images/phpmailer_mini.gif');
// send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
I read related post on stackoverflow, but didnt had success yet. Thanks in advance!
You could check your current working directory, like this:
echo getcwd();
If you do so, you will see which is your active folder. The next thing you need to do is to check whether there is a PHPMailer-master folder inside your active folder (case-sensitive if you are using Linux). If it does not exist, then you will need to point to the right location instead or to create the folder and copy the file(s) into it. If it exists, check whether you have the necessary privileges and whether there is a file called PHPMailerAutoload.php inside that folder (again, case-sensitive if you are using Linux).
This isn't rocket science - the error message is very clear - you just need to use the correct path to the autoloader (and the rest of the PHPMailer files), wherever that is for your server. No need to edit php.ini.
I am trying to use PHPMailer to send e-mails over SMTP but so far have had no luck. I've gone through a number of SO questions, PHPMailer tutorials and forum posts but still cannot get it to work. I'll document as many of my failed attempts as I can remember to save time, but firstly here is the code I am using:
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors','On');
require('includes/class.phpmailer.php');
include('includes/class.smtp.php');
$mail = new PHPMailer();
$name = $_POST["name"];
$guests = $_POST["guests"];
$time = $_POST["time"];
$message = "<h1>".$name." has booked a table for ".$guests." at ".$time."</h1>";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$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 = "myEmail#gmail.com"; // SMTP account username
$mail->Password = "myPassword"; // SMTP account password
$mail->SetFrom('myEmail#gmail.com', 'James Cushing');
$mail->AddReplyTo("myEmail#gmail.com","James Cushing");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($message)
$address = "myOtherEmail#me.com";
$mail->AddAddress($address, "James Cushing");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Firstly, when I run this code now I get two different errors. On my local server I get the error:
SMTP -> ERROR: Failed to connect to server: Operation timed out (60)
The following From address failed: myEmail#gmail.com : Called Mail() without being connected
Mailer Error: The following From address failed: myEmail#gmail.com : Called Mail() without being connected
I get moreorless the same error running the same code on my web server, but the first line is:
SMTP -> ERROR: Failed to connect to server: Network is unreachable (101)
Obviously it's worth pointing out that I'm not using the literal "myEmail#gmail.com" but I've substituted my own email out for this post.
Things I've tried
- Using the iCloud SMTP server
- Using a different port
- Enabling the OpenSSL extension in my php.ini file
- Copying code from various PHPMailer examples
- Using Google's "DisplayUnlockCaptcha" system to enable connections
- Sending to and from different addresses
- Removing the "#gmail.com" from the Username property
- A number of other things I can't remember
This has now been driving me mad for about a day, so if anyone can solve it they will be a hero.
Thanks
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "myemail#gmail.com";
$mail->Password = "**********";
$mail->Port = "465";
That is a working configuration.
try to replace what you have
Don't use SSL on port 465, it's been deprecated since 1998 and is only used by Microsoft products that didn't get the memo; use TLS on port 587 instead: So, the code below should work very well for you.
mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$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
Firstly, use these settings for Google:
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls"; //edited from tsl
$mail->Username = "myEmail";
$mail->Password = "myPassword";
$mail->Port = "587";
But also, what firewall have you got set up?
If you're filtering out TCP ports 465/995, and maybe 587, you'll need to configure some exceptions or take them off your rules list.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I got a similar failure with SMTP whenever my client machine changes network connection (e.g., home vs. office network) and somehow restarting network service (or rebooting the machine) resolves the issue for me. Not sure if this would apply to your case, but just in case.
sudo /etc/init.d/networking restart # for ubuntu
First, Google created the "use less secure accounts method" function:
https://myaccount.google.com/security
Then created the another permission:
https://accounts.google.com/b/0/DisplayUnlockCaptcha
Hope it helps.
I am using XAMPP and when trying to send email through localhost I get the following warning:
Warning: stream_socket_enable_crypto(): this stream does not support SSL/crypto in C:\xampp\htdocs\12work\class.smtp.php on line
197
Here is my code:
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.gmail.com"; // specify main and backup server
$mail->SMTPAuth = true;
$mail->Port = 25;
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "sheikh.abm#gmail.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password
$mail->From = "sheikh.abm#gmail.com"; //do NOT fake header.
$mail->FromName = "MailMan";
$mail->AddAddress("sheikh.abm#gmail.com"); // Email on which you want to send mail
$mail->IsHTML(true);
$mail->Subject = "Just a Test";
$mail->Body = "Hello. I am testing <b>PHP Mailer.</b>";
if(!$mail->Send())
{
echo $mail->ErrorInfo;
}else{
echo "email was sent";
}
Alright, we need to enable Open SSL module. Here is how to do it:
Locate and open your php.ini file
Search for the line: ;extension=php_openssl.dll
Enable the module by removing ; char.
Save the file and restart Apache.
Hint:
If you are not familiar with php.ini file, it is recommended to create a backup before modification. Ini is a configuration file and misconfigured or corrupted ini can result in that web server will not start.
If you are using LAMP stacks such as wamp, it should be possible to enable modules via graphic interface.
More about php.ini: https://secure.php.net/manual/en/configuration.file.php
In my local system avast mail shield was on I was not allowing any mail to be sent by smtp gmail once I disabled it, the mails were sending in local system via smtp
Just a quick note,
you use $mail->Port = 25;
$mail->SMTPSecure = "tls";
Port should be 587 for gmail /tls
See https://support.google.com/mail/answer/78775?hl=el
In my local system avast antivirus was on my computer I was not allowing any mail to be sent by SMTP Gmail once I disabled it, the emails were sent in the local system via SMTP
I solved this
Hust close selinux
#setenforce 0
Please help me I am New in PHP and since last 5 Hours i am try to semd mail and now really tired. Thanks.
Here is my code. I am using Gmail account.
include("class.phpmailer.php");
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = $mail->getFile('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "hussaintalha#gmail.com"; // GMAIL username
$mail->Password = "xxxxxxxx"; // GMAIL password
$mail->AddReplyTo("hussaintalha#gmail.com","First Last");
$mail->From = "hussaintalha#gmail.com.com";
$mail->FromName = "First Last";
$mail->Subject = "PHPMailer Test Subject via gmail";
//$mail->Body = "Hi,<br>This is the HTML BODY<br>"; //HTML Body
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 50; // set word wrap
$mail->MsgHTML($body);
$mail->AddAddress("hussaintalha#gmail.com", "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
When I run my file I get This Error:
Warning: fopen(contents.html)
[function.fopen]: failed to open
stream: No such file or directory in
D:\xampplite\htdocs\WebEng\class.phpmailer.php
on line 1870
Warning: fsockopen()
[function.fsockopen]: unable to
connect to ssl://smtp.gmail.com:465
(Unable to find the socket transport
"ssl" - did you forget to enable it
when you configured PHP?) in
D:\xampplite\htdocs\WebEng\class.smtp.php
on line 122 Mailer Error: SMTP Error:
Could not connect to SMTP host.
First of all when you get error messages then that's great! Because in 90% of the cases you find that others have had them too and therefore you'll find plenty of information on the internet about this error message.
So step 1 when getting an error message you don't know yet is always open google and copy paste it there. But, take out any paths or other things which are uniquely connected to your system.
Then about your errors. Especially xampp light doesn't support SSL. Maybe you try an easier sendmail example first. Like a very small one and then increase it step by step.
That's what I always do, when I don't know why something doesn't work. I start with one line and see what it does, then I add another and so forth.
Let's say you start with this and see if it works:
<?php
include("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "hussaintalha#gmail.com"; // GMAIL username
$mail->Password = "xxxxxxxx"; // GMAIL password
$mail->From = "hussaintalha#gmail.com";
$mail->Subject = "PHPMailer Test Subject via gmail";
$mail->Body = "Hi, this is a test";
$mail->AddAddress("hussaintalha#gmail.com", "Hussain");
$mail->send();
?>
oh, and btw. your mail from has a .com too many!
You PHP installation (XAMPP, by the looks of it) does not support SSL. Ensure that the line
extension=php_openssl.dll
is not commented out in your php.ini, restart Apache, and if that still doesn't work try overwriting (or copying) ssleay32.dll and libeay32.dll from your PHP directory into Apache's binary (.exe) directory then restart Apache.