How to Send Email from Localhost in PHP
09/03/2019 . 1 minute, 19 seconds to read . Posted by Admin#htmlemail #codeigniteremail #mysql #mysql #ViewsinCodeigniter #phpmyadmin
Note: To send email from local, PHPMailer is the best option if that does n't work then you can use the following method.
How to send mail from localhost in php using wamp
Requirements:
- Wamp Installed
Solution:
To send email from localhost you need to follow these steps.
- Create a folder named as sendmail in C:\wamp\ or C:\wamp64\.
- Download Sendmail and extract in sendmail folder
- Open the sendmail.ini file and change it as follows
- 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
- Enable IMAP Access in your gmail’s Settings -> Forwarding and POP/IMAP -> IMAP Access:
-
Enable “php_openssl” and “php_sockets” PHP extensions:
- Open php.ini and update sendmail_path as follows:
sendmail_path = "C:\wamp\sendmail\sendmail.exe -t -i"
- Restart WAMP Server.
- Create a PHP file in www(or in a subfolder of www) and write the following code in it:
<?php $to = 'your-recipient@yahoo.com'; $subject = 'My test email'; $message = 'Hi, my message!'; $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";
- Update $to and $headers variables to set recipient and sender (“From” header). Save it as “send_email.php”.
- Open this file in the browser