Php send mail using localhost

How to Send Email from Localhost in PHP

PHP send email using Inbuilt PHP MAIL() function. This tutorial shows you how to send email in PHP ubuntu, and windows xampp using inbuilt PHP mail() function.

If you work with PHP, you need to send emails to users like when the user registered with us, send reset password link in the mail, if any offers send to users by email, send invoice or pdf, welcome mails, etc.

How to Send Email from Localhost in PHP Ubuntu Or Windows Xampp

Using the PHP inbuilt mail() function, You can send text or HTML in emails from localhost in PHP ubuntu and windows xampp.

The PHP mail() Function

Sending email is a very common process for any web application. Such as welcome email, reset password link, invoice or PDF if there is an offer, etc.

Читайте также:  Python defaultdict to dict

Here you will learn how to send an mail from your web application to one or more users using the PHP built-in mail () function. either in plain-text form or in HTML format.

The basic syntax of PHP mail function is:

mail(to, subject, message, headers, parameters)

Send Plain Text In Emails – PHP

The easy way to send an email with PHP is to send a text in the email. In the below-given example, we first declare the variables — recipient’s email address, subject line, and message body. After that, we pass these define variables to the mail() function to send the email.

Example of sending plain text in emails

The below example sends plain text in emails using the PHP Mail() function.

[email protected]'; $subject = 'Testing Purpose'; $message = 'Hi there, this is the test mail using php mail() function?'; $from = '[email protected]'; // Sending email if(mail($to, $subject, $message)) < echo 'Your mail has been sent successfully.'; >else < echo 'Unable to send email. Please try again.'; >?>

Note that:- If you are sending a mail using Gmail you have to allow non-secure apps to access Gmail you can do this by going to your Gmail settings here.

Once less secure apps are enabled; now you can use your Gmail for sending the emails.

Send HTML in Emails Using PHP Mail Function

When you send a simple text message in the mail using PHP mail function, that is treated as simple text. We are sending a well-designed mail to users, so that time we will send HTML in mail

If you want to send an HTML in emails using PHP mail function. So don’t worry, the email sending process will be the same. However, this time we add additional headers as well as an HTML formatted message.

Example of sending HTML in the email using PHP mail()

[email protected]'; // receiver email $subject = 'Tesing Purpose'; // subject $from = '[email protected]'; // send email // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Create email headers $headers .= 'From: '.$from."\r\n". 'Reply-To: '.$from."\r\n" . 'X-Mailer: PHP/' . phpversion(); // Compose a simple HTML email message $message = ''; $message .= '

Hi There!

'; $message .= '

This is a testing mail with html?

'; $message .= ''; // Sending email to receipt if(mail($to, $subject, $message, $headers)) < echo 'Your mail has been sent successfully.'; >else < echo 'Unable to send email. Please try again.'; >?>

Conclusion

That’s all; in this tutorial, you have learned how to send email in PHP using the mail() function. Also, you have learned how to send simple text and formatted HTML in mails using the inbuilt PHP mail() function.

Источник

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

Источник

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.

Источник

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.

Источник

Оцените статью