14

I used mail() function in php coding but I failed to send any mail. Before proceeding ahead I want to elaborate the context of using the mail() function.

I didnt host my site so it is on localhost. I did set smtp, port sendmail_path etc.

After searching a lot I it seems that I need to download a mail server. I downloaded a sendmail server that is free and configured it as the site suggested. However, all in vain moreover, someone told me that I can't use mail function until I host my site not only on localhost. Please guide me.

<?php
   $from = "oooo@hotmail.com"; // sender
   $subject = " My cron is working";
   $message = "My first Cron  :)";

   // message lines should not exceed 70 characters (PHP rule), so wrap it

   $message = wordwrap($message, 70);

   // send mail

   ini_set("SMTP","localhost");
   ini_set("smtp_port","25");
   ini_set("sendmail_from","00000@gmail.com");
   ini_set("sendmail_path", "C:\wamp\bin\sendmail.exe -t");

   mail("jXXXXXX@gmail.com",$subject,$message,"From: $from\n");

   echo "Thank you for sending us feedback";

?>

this my sendmail configuration file:

smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=auto

;default_domain=domain.com
auth_username=jxxxx@gmail.com
auth_password=8888

force_sender=j*****@gmail.com

( ! ) SCREAM: Error suppression ignored for
( ! ) Warning: mail() [<a href='function.mail'>function.mail</a>]: Failed to connect to      mailserver at &quot;localhost&quot; port 25, verify your &quot;SMTP&quot; and &quot;smtp_port&quot;    setting in php.ini or use ini_set() in C:\wamp\www\test.php on line 20
Graphican0
  • 167
  • 11
user1195450
  • 141
  • 1
  • 1
  • 7

2 Answers2

34

I think you are not configured properly,

if you are using XAMPP then you can easily send mail from localhost.

for example you can configure C:\xampp\php\php.ini and c:\xampp\sendmail\sendmail.ini for gmail to send mail.

in C:\xampp\php\php.ini find extension=php_openssl.dll and remove the semicolon from the beginning of that line to make SSL working for gmail for localhost.

in php.ini file find [mail function] and change

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = my-gmail-id@gmail.com
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"

(use the above send mail path only and it will work)

Now Open C:\xampp\sendmail\sendmail.ini. Replace all the existing code in sendmail.ini with following code

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=my-gmail-id@gmail.com
auth_password=my-gmail-password
force_sender=my-gmail-id@gmail.com

Now you have done!! create php file with mail function and send mail from localhost.

Update

First, make sure you PHP installation has SSL support (look for an "openssl" section in the output from phpinfo()).

You can set the following settings in your PHP.ini:

ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");
Community
  • 1
  • 1
ɹɐqʞɐ zoɹǝɟ
  • 4,342
  • 3
  • 22
  • 35
  • SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. now i am getting this message. my setting was: ini_set("SMTP","smtp.gmail.com"); ini_set("smtp_port","587"); ini_set("sendmail_from","******@gmail.com"); this was my setting however when I set these settings: – user1195450 Feb 18 '14 at 05:02
  • yes I did check my php.ini file it support openssl, please guide what i need to do now? i already enabled IMAP on gmail – user1195450 Feb 18 '14 at 05:35
  • this my code: ini_set("SMTP","sll://smtp.gmail.com"); ini_set("smtp_port","587"); ini_set("sendmail_from","88888@gmail.com"); //ini_set("sendmail_path", "C:\wamp\bin\sendmail.exe -t"); – user1195450 Feb 18 '14 at 05:43
  • change the port to 465 – ɹɐqʞɐ zoɹǝɟ Feb 18 '14 at 05:49
  • when i change the port still there is some error and it says:Failed to connect to mailserver at "sll://smtp.gmail.com" port 465, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set(); i am accessing my file :localhost:8080/test.php – user1195450 Feb 18 '14 at 06:07
  • are you using wamp or xammp? – ɹɐqʞɐ zoɹǝɟ Feb 18 '14 at 06:26
  • have you enabled ENABLE IMAP option in gmail settings – ɹɐqʞɐ zoɹǝɟ Feb 18 '14 at 06:27
  • i am using wamp and i did enable IMAP – user1195450 Feb 18 '14 at 06:36
  • have you enabled the ssl_module(in apache->apache modules),php_openssl,php_sockets(in php->php extension) in wamp window? – ɹɐqʞɐ zoɹǝɟ Feb 18 '14 at 06:39
  • in folder module I found file mod_ssl.so i dont know how to enable it – user1195450 Feb 18 '14 at 06:52
  • no if you are using wamp then you see can these options in the bottom right corner in your desktop,where you can see the wamp icon – ɹɐqʞɐ zoɹǝɟ Feb 18 '14 at 06:55
  • php_openssl was already enabled but however, php_sockets were not enable. I did enable both of the options – user1195450 Feb 18 '14 at 07:02
  • you have to download the php_sockets.dll file and place it in extensions folder and restart the wamp.if still have problem then in your php.ini file put this extension=php_sockets.dll and restart the wamp,hope you will success :) – ɹɐqʞɐ zoɹǝɟ Feb 18 '14 at 07:14
  • i recommend you should follow all these steps again. http://blog.techwheels.net/send-email-using-wamp-server/ – ɹɐqʞɐ zoɹǝɟ Feb 18 '14 at 07:43
  • but however there is some more troublesome issue need to be discussed. i want to send email via task scheduling on periodic base that mean i have to execute the using php.exe on cli. how can i use PHPmailer class cli? – user1195450 Feb 18 '14 at 09:27
  • if you want to schedule your mails on particullar time,then use crons for that.using cron jobs we can do automatic tasks as per our requiremnt.once see this http://www.jabcreations.com/blog/basic-php-cron-job-example and if you think i am helped you then please accept this as answer so that the new people can easily find out,thanks – ɹɐqʞɐ zoɹǝɟ Feb 18 '14 at 10:44
  • you can see a green tick mark just below the up and down votes at my answer – ɹɐqʞɐ zoɹǝɟ Feb 18 '14 at 17:06
  • @ɹɐqʞɐzoɹǝɟ where can i paste `ini_set("SMTP","ssl://smtp.gmail.com");` ? anywhere? in php.ini – 151291 Oct 12 '15 at 04:47
  • no brother, init_set in an php function so you should put it in top of your php script – ɹɐqʞɐ zoɹǝɟ Oct 12 '15 at 07:12
1

I followed @ɹɐqʞɐ zoɹǝɟ solution. However, it did not fully solve my issue, I had to do one more step.

I had to turn on 'Less Secure App Access' settings from my google account settings page. After doing this I was able to send the mail.

Link to the settings page - Less Secure App Access

I had 2-Step Verification enabled in my account and had to disable it to access the setting.

Note: Turning on less secure app access can make your account vulnerable and is not recommended to use it in the long run. I was using a test account and not my primary account while doing this.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ranjul Arumadi
  • 109
  • 1
  • 2
  • 11