Send email using Gmail SMTP CodeIgniter [duplicate] - php

I'm googling for how to this error a lot of hours. I've tried all solutions from this topic without luck. <? phpinfo(); ?> (the only difference I'm using Appserver instead of IIS) it doesn't show anything realated to SSL. What else should I try?
The full error message:
<b>Warning</b>: fsockopen() [<a href='function.fsockopen'>function.fsockopen</a>]: 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 <b>C:\AppServ\www\webgitz\test\class.smtp.php</b> on line <b>122</b><br />
SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? (27010016)
and code:
<?php
require_once "validation.php";
require_once "PHPMailer.php";
require_once "class.smtp.php";
$email= "foo#gmamcil.com";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port =465; // set the SMTP server port
$mail->Host = "smtp.gmail.com"; // GMAIL's SMTP server
$mail->SMTPDebug = 2;
$mail->SMTPSecure = 'ssl';
$mail->Username = "xxxxxx#gmail.com"; // GMAIL username
$mail->Password = "*********"; // GMAIL password
$mail->AddReplyTo("XXXXX#gmail.com","AAAA"); // Reply email address
$mail->From = "XXXX#gmail.com";
$mail->FromName = "...."; // Name to appear once the email is sent
$mail->Subject = "...."; // Email's subject
$mail->Body = "Hello World,<br />This is the HTML BODY<br />"; //HTML Body
$mail->AltBody = "..."; // optional, commentA out and test
$mail->WordWrap = 50; // set word wrap
$mail->MsgHTML($msg); // [optional] Send body email as HTML
$mail->AddAddress("$email", "fooo"); // email address of recipient
$mail->IsHTML(true); // [optional] send as HTML
if(!$mail->Send()) echo 'ok'; else echo 'error';
?>

Check your php.ini file, on it, if you have this:
;extension=php_openssl.dll
change it to
extension=php_openssl.dll
After that, remember to restart Apache, either using any control panel that Appserver offers, or using the services windows on the control panel of the system.
Also be sure that php_openssl.dll is on a folder that the system can reach, many people use to solve the problem of dll location problems by copying them to C:\windows or C:\windows\system32. Although that shouldn't be necessary.
After that, execute a file with phpinfo and check that it's enabled.
I have read somewhere, can't pinpoint it, that some installation of that kind of wamps have more that one php.ini, but only one is really active, could it be that you have edited the wrong one?

The proper answer is to replace the line
;extension=php_openssl.dll
in the php.ini file with
extension=php_openssl.dll
If you cant find extension=php_openssl.dll in the php.ini file, then just simply append the following line to the file:
extension=php_openssl.dll
This should work!

I had a similar error with ssl for hours.
What worked for me was copying php\libeay32.dll, php\ssleay32.dll and php\ext\php_openssl.dll from the php directory to the Apache2.2\bin directory. Then restart apache.

For some people, those solutions don't work.
For me the solution was this: copy dll libeay32.dll and ssleay32.dll to the bin directory of the server - apache.
The solution suggest in a comment in:
http://php.net/manual/fa/openssl.installation.php
When I look in the error log, I understood that the problem was in loading the dll's and putting them to the server directory was the only way.

Related

SMTP Not Working on WAMP Server

I have installed WAMP Server on my Windows 10 PC and when I try to send emails through a valid SMTP configuration it doesn't work. The same SMTP configuration works on another LAMP installation and also on a live server.
When I try sending the email through a PrestaShop installation I get following error:
Error: Please check your configuration
Connection could not be established with host smtp.gmail.com [ #0]
And with Magento I get following error:
SMTP Pro Self Test Results
Sending test email to your contact form address: xxxxxxx#example.com from: xxx.adsxx#example.com. Unable to send test email.
Exception message was: Could not open socket
Please check the user guide for frequent error messages and their solutions.
Default templates exist.
Email communications are enabled.
As per my understanding, this issue is not dependent on Magento or PrestaShop, it is coming because of the WAMP installation.
Do I have to enable some extension or something for the WAMP installation? Or have I missed something else?
Please help. Already wasted a lot of time investigating and trying solutions from the Web, but nothing seems to be working.
Download the sendmail.zip
Create a folder named “sendmail” in “C:\wamp\”.
Extract these 4 files in “sendmail” folder: “sendmail.exe”, “libeay32.dll”, “ssleay32.dll” and “sendmail.ini”.
Open the “sendmail.ini” file and configure it as following
smtp_server=smtp.gmail.com
smtp_port=465
smtp_ssl=ssl
default_domain=localhost
error_logfile=error.log
debug_logfile=debug.log
auth_username=[your_gmail_account_username]#gmail.com
auth_password=[your_gmail_account_password]
pop3_server=
pop3_username=
pop3_password=
force_sender=
force_recipient=
hostname=localhost
You do not need to specify any value for these properties: pop3_server, pop3_username, pop3_password, force_sender, force_recipient. The error_logfile and debug_logfile settings should be kept blank if you have already sent successful email(s) otherwise size of this file will keep increasing. Enable these log file settings if you don’t get able to send email using sendmail.
Enable IMAP Access in your GMail’s Settings -> Forwarding and POP/IMAP -> IMAP Access:
Enable “php_openssl” and “php_sockets” extensions for PHP compiler:
Open php.ini from “C:\wamp\bin\apache\Apache2.2.17\bin” and configure it as following (The php.ini at “C:\wamp\bin\php\php5.3.x” would not work) (You just need to configure the last line in the following code, prefix semicolon (;) against other lines):
Restart WAMP Server.
Create a PHP file and write the following code in it:
<?php
$to = 'recipient#yahoo.com';
$subject = 'Testing sendmail.exe';
$message = 'Hi, you just received an email using sendmail!';
$headers = 'From: [your_gmail_account_username]#gmail.com' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=utf-8';
if(mail($to, $subject, $message, $headers))
echo "Email sent";
else
echo "Email sending failed";
?>
Make appropriate changes in $to and $headers variables to set recipient and sender (“From” header) addresses. Save it as “send-mail.php”. (You can save it anywhere or inside any sub-folder in “C:\wamp\www”.)
Open this file in browser, it MUST work now
These are possibly causes which prevent you from sending mails via WAMP server.
Your firewall configurations may (by default) block some ports used by WAMP for sending emails.
These ports may be already used by your others applications. Prefer to this to know which port is being used by which application.
Administrators rights are required for the WAMP server to be able to send mail. Run the server as administrator.
Since you're sure that settings have already worked on a server so I assume there nothing to check with PHP.ini
make sure that your -
extension=php_openssl.dll
extension=php_sockets.dll
is enable in php.ini file.
check is Enable ssl_module under Apache Module.
You can use PHP Mailer or Download php mailer with example from here. Once you setup the phpmailer with your project use following code to send mail using gmail SMTP :
require_once('inc/class.phpmailer.php'); # INCLUDE PHPMailer CLASS FILE
require_once("inc/class.smtp.php"); # INCLUDE OTHER SMTP FILE
Include above files in your project or PHP file which you use to send email and then :
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0; # ENABLE SMTP DEBUG INFORMATION (FOR TESTING)
# 1 = ERROR AND MESSAGE
# 2 = MESSAGE ONLY
$mail->SMTPAuth = true; # ENABLE SMTP AUTHENTICATION
$mail->Host = "Host Addresss"; # smtp.gmail.com
$mail->Port = 587; # PORT NUMBER
$mail->Username = "example#gmail.com"; # SMTP EMAIL USER NAME
$mail->Password = "xxxxxxx"; # SMTP ACCOUNT PASSWORD
$mail->SetFrom('example#gmail.com', 'Test Name');
$mail->SMTPSecure = 'tls';
$mail->AddReplyTo("example#gmail.com","SMTP TEST");
$v_Msg ="This is a test message via SMTP";
$mail->Subject = "EMAIL SUBJECT";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; # OPTIONAL
$address = "sendto#email.com"; # ADDRESS WHERE YOU WANT TO SEND MAIL
$mail->AddAddress($address, "TESTING");
$mail->MsgHTML($v_Msg); # EMAIL CONTENT
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo; # ERROR MESSAGE, IF MAIL NOT SENT SUCCESSFULLY
}else {
echo "Message sent!"; # SUCCESS MESSAGE
}
NOTE : Make sure your smtp gmail account must be Allow less secure apps: ON. You can turn on less secure apps for your gmail
account using THIS URL
Sometimes its easier not to rely on your own smtp server. (firewalls everywhere)
You can experiment with some other online mail delivery services than gmail, like mailgun or sendgrid.

SMTP ERROR: Failed to connect to server: Connection refused (111) with Office365

Sorry if this is a road heavily traveled. I've seen the other posts about this but nothing in them has either solved the problem I'm having or ignited a lightbulb that helped me solve it myself.
Here's my code:
require 'PHPMailerAutoload.php';
$config = parse_ini_file('/path/to/file/config.ini', true);
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->Debugoutput = 'html';
$mail->isSMTP();
$mail->Host = $config['host']; //smtp.office365.com
$mail->SMTPAuth = true;
$mail->Username = $config['username']; //an.existing.account#appinc.co
$mail->Password = $config['password']; //confirmed this is being passed correctly
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->From = $config['username'];
$mail->FromName = 'Website Forms';
$mail->addAddress('sales#appinc.co', 'Some Name');
$mail->addReplyTo('sender.email#somedomain.com', 'SenderFirst SenderLast');
$mail->addBCC('my.email.address#appinc.co');
$mail->isHTML(true);
$mail->Subject = 'Contact Form Submission';
$mail->Body = 'Some html here';
$mail->AltBody = 'Some alt content here';
if(!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
//perform success actions
exit();
}
I've confirmed that the domain, username and password are all correct and being passed in correctly. Important to note that this worked on our local dev server prior to launch. Once the site was moved to our hosting account (Hostgator) is when it stopped working. I've confirmed with HG that port 587 is open on our server.
Here is the error message I'm seeing:
Connection: opening to smtp.office365.com:587, t=10, opt=array ()
SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed.
Message could not be sent.Mailer Error: SMTP connect() failed.
Any help that can be provided is very much appreciated, even if it's just a link to an article that explains why it won't work now that it's in our production environment.
None of the answers worked for me.
After many hours, I found the problem, but only works for Cpanel/WHM
Login into WHM.
Go to ConfigServer Security & Firewall inside plugins option.
Click on Firewall configuration
Filter by SMTP Settings
Look for SMTP_ALLOWUSER option and add the Cpanel account's username separated by coma
Restart the firewall.
If you don't have access to WHM ask your provider.
In PHP 5.5 and phpmailer there's a bug with the port number. Don't set port number ( mail->port = ....) this causes the error:
"smtp error failed to connect to server connection refused 111"
Leave it at the default port number of 25 and it works !
It turns out that HG needed to modify the settings to the firewall on our server. Once they did that, it worked great. So, if you're having a similar problem, I'd recommend making sure everything is correct on your end, but then to check with your hosting provider to see what needs to be done on their end.
If you are using cPanel/WHM you need to make sure:
Restrict outgoing SMTP to root, exim, and mailman (FKA SMTP Tweak) - is set to OFF. (This can be edited inside "Server Configuration » Tweak Settings" (Search: SMTP))
If you also have the ConfigServer Security & Firewall enabled, you will need to edit your Firewall configuration. Click 'Firewall Configuation' then choose "Filter by SMTP Settings". Now look for SMTP_ALLOWUSER option and add the cPanel account's username separated by coma. Hit "Change" and then restart the firewall.

Mail function not working - PHP

I am new to PHP and just started learning it. I am trying to send a test mail to my gmail account using the mail() function.
Here is my code:
$message = "Hello PHP!";
mail("mygmailaccount#gmail.com", "PHP Test", $message);
But its not working. This is what the error looks like:
Click here to view it in a webpage.
I have seen lots of similar questions and I have also tried all the solutions mentioned on SO.
I have made changes to my php.ini file as shown below.
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = mygmailaccount#gmail.com
I am using WAMP server on a Windows 7 (64-bit) machine.
Please help me solve this. Thanks!
https://github.com/PHPMailer/PHPMailer
This is the official PHPMailer.
all you have to do is upload all the files and folders to a folder with the mailing php file(the file which have the code shown by you). Then look at the example folder or look at the example folders if you need smtp.You should have figured it out , looks like you got good programming skill.If you have problem , comment to informed me.
PS. mail() function sometimes not reliable. I face that problem often until I use phpmailer for my own system.
EDIT: Put it altogether like this. send.php is the file I'm going to write the followling code.
Next , the send.php code!!
<?php
require 'PHPMailerAutoload.php';
$sendto ="destinationemail";//Input your own
$sendfrom ="yourmaskedmail";//input your own
$topic ="test";
$passage ="test";
$mail = new PHPMailer(true);
//Send mail using gmail
if($send_using_gmail){
$mail->IsSMTP(); // telling the class to use SMTP
$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 = "*hidden*"; // GMAIL username Input your own
$mail->Password = "*hidden"; // GMAIL password Input your own
}
//Typical mail data
$mail->AddAddress($sendto,$sendto);
$mail->SetFrom($sendfrom,$sendfrom);
$mail->Subject =$topic;
$mail->Body =$passage;
try{
$mail->Send();
echo "Success! This email has been sent to ".$sendto. " in the name of ".$sendfrom;
} catch(Exception $e){
//Something went bad
echo "Unable to process your request now, please try again later";
}
?>
Change the mailsender,mailreciever and contents of the mail. Also , input your gmail username and password. After that, run the php, wait for your email to come. I've just test it 8 minutes ago and it worked.
Have a look at this page:
http://www.php.net/manual/en/mail.configuration.php
Short version: to make your php.mail function work, you need to figure out how to send an e-mail using your server and make sure your configuration in php.ini is up to date. (For details, see the link above).
Does your server run sendmail or another mail agent? Or doesn't it run anything like that at all?
If so, make sure the paths are set correctly.
If not, use a third party SMTP service. GMail offers it, but the problem is that it requires authentication, which php.mail() doesn't support. If you still want to use it, see this post for alternatives to php.mail().

Phpmailer using smtp with Gmail not working - connection timing out

I've looked into the following links:
phpmailer send gmail smtp timeout
send email using Gmail SMTP server through PHP Mailer
http://uly.me/phpmailer-and-gmail-smtp/
...and tried to implement for myself a combination of those however...most of the time it sends this message...
Message could not be sent.
Mailer Error: SMTP connect() failed.
However there was one time where it sent this when I experimented between "tls" and "ssl"...
SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed.
Message could not be sent.
Mailer Error: SMTP connect() failed.
My code is attached...did I somehow miss something? I asked the web hosting service if they're blocking and gave them a template of my code - they said the server allows connections to Gmail's SMTP.
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail -> IsSMTP();
$mail -> SMTPDebug = 2;
$mail -> SMTPAuth = 'true';
$mail -> SMTPSecure = 'tls';
$mail -> SMTPKeepAlive = true;
$mail -> Host = 'smtp.gmail.com';
$mail -> Port = 587;
$mail -> IsHTML(true);
$mail -> Username = "myemail#gmail.com";
$mail -> Password = "mypassword";
$mail -> SingleTo = true;
$to = xxx;
$from = xxx;
$fromname = xxx;
$subject = xxx;
$message = xxx
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$mail -> From = $from;
$mail -> FromName = $fromname;
$mail -> AddAddress($to);
$mail -> Subject = $subject;
$mail -> Body = $message;
if(!$mail -> Send()){
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail-> ErrorInfo;
exit;
}
I dug into it. Use fsocketopen, which is native to php, to test the connection and eliminate most of the potential problems. Write a simple php page with this:
$host = "smtp.gmail.com";
$port = "587";
$checkconn = fsockopen($host, $port, $errno, $errstr, 5);
if(!$checkconn){
echo "($errno) $errstr";
} else {
echo 'ok';
}
You should get back "ok". If not you know you have a connection problem that has nothing to do with Phpmailer. If that's the case it's probably the hosting company. If not then it's probably something simple about the difference between your local host and the hosting company like different versions of php.
I suspect though that this script won't make the connection
I had this same problem and solved it:
First, turn on smtp error logging in phpmailer:
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
Then retry your phpmailer email send. You will see the entire SMTP conversation on standard error output. If you're using a web server, look in the web server log file.
I could then see the error response from gmail. Gmail was not accepting the login.
The error within the smtp conversation refers to an article. It gives some tips:
Allow less secure apps to use your account.
Login to gmail from a web browser.
Confirm the gmail login captcha. (It may not actually display a captcha to you, but this was the additional step that suddenly allowed my email to go through.)
Use ssl
$mail -> SMTPSecure = 'ssl';
Port should be 465
$mail -> Port = 465;
Change your host to:
$mail -> Host = 'ssl://smtp.gmail.com';
Hopefully it works
Check to make sure you can reach gmail from your webhost. I'm assuming it's linux. SSH in and on the command line type
telnet smtp.gmail.com 587
You should get back
Connected to smtp.something
It has to be a configuration difference between localhost and your provider
This question has many duplicates, so here's a canned answer:
Base your code on the gmail example provided with PHPMailer
Check out the troubleshooting docs
Be aware of this issue, related to what Larry K said.
That might probably be Gmail blocking your access.
Go to your security configurations and see if it's blocking any access..
Or try to change your password and try again.
Add
date_default_timezone_set('Etc/UTC');
before including the autoloader. SMTP Needs to have the timezone set which was my issue.
Download sendmail for Windows from
http://www.glob.com.au/sendmail/sendmail.zip
Copy sendmail.exe and sendmail.ini into C:/usr/lib/
Edit sendmail.ini and enter your mail account credentials.
You might want to configure these 2 fields as well (or sending may not work)
force_sender=you-sender#yourdomain.com
force_recipient=you#yourdomain.com
By the way I uncommented debug_logfile so I can see what data is being sent to my SMTP server.
edit c:\php\php.ini
sendmail_from = you#yourdomain.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = C:/usr/lib/sendmail.exe -t -i
Restart apache
Start sendmail.exe either from [Start] > Run > C:/usr/lib/sendmail.exe or Go to C:/usr/lib in Windows Explorer and then DoubleClick on the exe file.
And this solution appears in Sendmail Wamp Php
I tried it on Windows 10 now and it runs with gmail account
Added:
Open CMD and make a sendmail as daemon/service using
sc create SendMail binpath= "C:\usr\lib\sendmail.exe"
Now, be sure you have OpenSSL installed on your system, you can download it from here: https://slproweb.com/products/Win32OpenSSL.html
Installs the version what do you need and dont remember to install "files to windows folder". Do a restart and try again, you will have it solved.

Warning: stream_socket_enable_crypto(): this stream does not support SSL/crypto in C:\xampp\htdocs\12work\class.smtp.php on line 197

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

Categories