Php send localhost smtp

How To Send Email From Localhost Using PHP

In this post about “How to send mail from localhost in php using wamp/xampp”, sometimes we need to test the mail sending function from our development environment. We can send mail from our localhost using some mail configuration by XAMPP/LAMP/WAMP server.

First, we need to enable php_openssl php extensions from php.ini file. I am using GMAIL SMTP server to send mail from localhost and sendmail package.

It is a mail transport agent which can be found in php.ini file. The sendmail package is inbuilt in XAMPP. So if you are using XAMPP then you can easily send mail from localhost.

Video Tutorial:

If you are more comfortable in watching a video that explains about How To Send Mail From Localhost Using XAMPP, then you should watch this video tutorial.

Step 1: we need to enable php_openssl php extensions from php.ini file. For XAMPP, it is located in C:\XAMPP\php\php.ini . You can download from php_openssl.dll.

Step 2: Find the mail function in php.ini file. You can find the code like below.

[mail function] ; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury ; SMTP = localhost ; smtp_port = 25 ; For Win32 only. ; http://php.net/sendmail-from ;sendmail_from = postmaster@localhost ;sendmail_path = "\"D:\xampp\sendmail\sendmail.exe\" -t"

replace with your SMTP configuration parameters like the below,

[mail function] ; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury SMTP = smtp.gmail.com smtp_port = 25 ; For Win32 only. ; http://php.net/sendmail-from sendmail_from = phpflow@gmail.com sendmail_path = "\"D:\xampp\sendmail\sendmail.exe\" -t"

Step 3: Open sendmail.ini which is located on «c:\xampp\sendmail\sendmail.ini» and change SMTP configuration parameters.

[mail function] smtp_server=smtp.gmail.com smtp_port=25 auth_username=phpflow@gmail.com auth_password=XXXXXXX force_sender=phpflow@gmail.com

Step 4: Restart your Apache server.

Step 5: Send mail using php mail() function.

mail("parvez1487@gmail.com","Success","Send mail from localhost using PHP");

Sometimes port 25 is not open, so you need to change to 587 OR 465.

Now you have configured mail from localhost and use it.I hope this php tutorial helps to understand the working functionality of mail and sending mail from localhost using PHP SMTP.

Источник

How to Send Email from Localhost in PHP

Localhost is used as a development server to develop a web application. All the functionality of the web application is tested on the localhost server before moving it to the production server. But, the problem arises when email functionality needs to be tested on the localhost server. Generally, the email sending feature is not working with the PHP built-in functions in localhost.

If the web application is built with PHP, the mail() function is used to send email from the script using PHP. But the PHP mail() function will not work in localhost. In this tutorial, we’ll show how you can send email from localhost in PHP. Using this example script you can send email from any localhost server (XAMPP, WAMP, or any others) using PHP.

We will use the PHPMailer library to send emails from localhost using PHP. The PHPMailer library provides the easiest way to send an email from localhost with an SMTP server using PHP. Not only the text email, but you can also send HTML email from localhost in PHP using PHPMailer.

SMTP Server Credentials:
Before getting started create an email account on your server and collect the SMTP credentials (Host, Port, Username, Password, etc.) that will require to be specified in the code later.

Send Email from Localhost with PHP

The following code snippet will send HTML email from localhost using PHPMailer.

  • Include the PHPMailer library and create an instance of this class.
  • Set SMTP credentials (host, username, password, and port).
  • Specify sender name and email ( $mail->setFrom ).
  • Set recipient email address ( $mail->addAddress ).
  • Set email subject ( $mail->Subject ).
  • Set the body content of the email ( $mail->Body ).
  • Use the send() method of PHPMailer class to send an email.
// Import PHPMailer classes into the global namespace 
use PHPMailer\PHPMailer\PHPMailer;
use
PHPMailer\PHPMailer\SMTP;
use
PHPMailer\PHPMailer\Exception;

// Include library files
require 'PHPMailer/Exception.php';
require
'PHPMailer/PHPMailer.php';
require
'PHPMailer/SMTP.php';

// Create an instance; Pass `true` to enable exceptions
$mail = new PHPMailer;

// Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'email_password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to

// Sender info
$mail->setFrom('sender@example.com', 'SenderName');
$mail->addReplyTo('reply@example.com', 'SenderName');

// Add a recipient
$mail->addAddress('recipient@example.com');

//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

// Set email format to HTML
$mail->isHTML(true);

// Mail subject
$mail->Subject = 'Email from Localhost by CodexWorld';

// Mail body content
$bodyContent = '

How to Send Email from Localhost using PHP by CodexWorld

'
;
$bodyContent .= '

This HTML email is sent from the localhost server using PHP by CodexWorld

'
;
$mail->Body = $bodyContent;

// Send email
if(!$mail->send()) <
echo
'Message could not be sent. Mailer Error: '.$mail->ErrorInfo;
> else <
echo
'Message has been sent.';
>

Note that: If you want to use Gmail as an SMTP server, set your Google email address as SMTP username and password as SMTP password.

You can send emails with multiple attachments from localhost with PHPMailer.

// Add attachments $mail->addAttachment('/var/tmp/file.tar.gz'); $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name

Are you want to get implementation help, or modify or enhance the functionality of this script? Click Here to Submit Service Request

If you have any questions about this script, submit it to our QA community — Ask Question

Источник

Send Email from Localhost using PHP with SMTP and PHPMailer

Localhost is the web developer’s favorite home. Local development environment is always convenient to develop and debug scripts.

Mailing via a script with in-built PHP function. This may not work almost always in a localhost. You need to have a sendmail program and appropriate configurations.

If the PHP mail() is not working on your localhost, there is an alternate solution to sending email. You can use a SMTP server and send email from localhost and it is the popular choice for sending emails for PHP programmers.

This example shows code to use PHPMailer to send email using SMTP from localhost.

mail sending from localhost using smtp

PHP Mail sending script with SMTP

This program uses the PHPMailer library to connect SMTP to send emails. You can test this in your localhost server. You need access to a SMTP server.

Before running this code, configure the SMTP settings to set the following details.

  1. Authentication directive and security protocol.
  2. Configure SMTP credentials to authenticate and authorize mail sending script.
  3. Add From, To and Reply-To addresses using the PHPMailer object.
  4. Build email body and add subject before sending the email.

The above steps are mandatory with the PHPMailer mail sending code. Added to that, this mailing library supports many features. Some of them are,

  • Single and multiple file attachments to the email.
  • Allows rich content in the email body.
  • Provides property to set the alternate mail body to be compatible with older clients.

Set the SMTP Debug = 4 in development mode to print the status details of the mail-sending script.

SMTPDebug = 0; $mail->isSMTP(); $mail->Host = 'SET-SMTP-HOST'; $mail->SMTPAuth = true; $mail->SMTPSecure = "ssl"; $mail->Port = 465; $mail->mailer = "smtp"; $mail->Username = 'SET-SMTP-USERNAME'; $mail->Password = 'SET-SMTP-PASSWORD'; // Sender and recipient address $mail->SetFrom('SET-SENDER-EMAIL', 'SET-SENDER_NAME'); $mail->addAddress('ADD-RECIPIENT-EMAIL', 'ADD-RECIPIENT-NAME'); $mail->addReplyTo('ADD-REPLY-TO-EMAIL', 'ADD-REPLY-TO-NAME'); // Setting the subject and body $mail->IsHTML(true); $mail->Subject = "Send email from localhost using PHP"; $mail->Body = 'Hello World!'; if ($mail->send()) < echo "Email is sent successfully."; >else < echo "Error in sending an email. Mailer Error: ErrorInfo>"; > ?> 

Google and Microsoft disabled insecure authentication

Earlier, programmers conveniently used GMail’s SMTP server for sending emails via PHP. Now, less secure APPs configuration in Google is disabled. Google and Microsoft force authentication via OAuth 2 to send email.

There is ill-informed text going around in this topic. People say that we cannot send email via Google SMTP any more. That is not the case. They have changed the authentication method.

IMPORTANT: I have written an article to help you send email using xOAuth2 via PHP. You can use this and continue to send email using Google or other email service providers who force to use OAuth.

Alternate: Enabling PHP’s built-in mail()

  1. If you do not have access to an email SMTP server.
  2. If you are not able to use xOAuth2 and Google / Microsoft’s mailing service.

In the above situations, you may try to setup your own server in localhost. Yes! that is possible and it will work.

PHP has a built-in mail function to send emails without using any third-party libraries.

The mail() function is capable of simple mail sending requirements in PHP.

In localhost, it will not work without setting the php.ini configuration. Find the following section in your php.ini file and set the sendmail path and related configuration.

Источник

Читайте также:  Php server текущий урл
Оцените статью