How I send mail from localhost in PHP

0
send mail from localhost using phpmailer

Well, everyone knows that it's a hard part to send mail from the localhost in the live server. It's a piece of cake to send mail from PHP. When we try to send mail from the same method on localhost, it shows the error which asks us to configure PHP mailer, but while doing this using your Google account, it's riskier to do.

While sending mail from the localhost XAMPP server, we get the below errors.

Today I am going to tell you a simple way to send mail from localhost. I am going to use Mailjet mail services to send mail. You can use any other API service as you want. I love its services because it gives me 200 emails per day, which is enough for me without paying. After getting any mailer API, you need to download a configuration folder that will help us to configure the mail port in our localhost server.

<?php 

	include('phpmailer/PHPMailerAutoload.php');
		$mail = new PHPMailer(true);

		try {
				$mail->isSMTP();
				$mail->Host = 'in-v3.mailjet.com'; // hostname
				$mail->SMTPAuth = true;
				$mail->Username = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; //username
				$mail->Password = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; //password 
				$mail->SMTPSecure = 'tls';
				$mail->Port = 587; //smtp port

				$mail->setFrom('xxxx@gmail.com', 'Jeamshiv Tips & Tricks');//registered mail and name
				$mail->addAddress('xxxx@gmail.com');//send to

				$mail->isHTML(true);
				$mail->Subject = 'Mail Subject';
				$mail->Body = 'Hello there try this code to send mail from localhost.';
				$mail->Body .= 'This trick also work with hosting server also.';

				$mail->send();
			
		} catch (Exception $e) {
			echo 'Email could not be sent. Mailer Error: ' . $mail->ErrorInfo;
		}

 ?>

For sending mail from localhost you need to include PHPMailerAutoload file to your PHP code, You will get PHPMailerAutoload file link in the below of this article. Don't worry about this. And the username and password you will get from the Mailjet website in Setup My SMTP link.

We can send HTML mail also using this service instead of typing the simple text you need to type HTML codes and after that, you will able to send HTML mail to anyone from this PHP code.

After doing this now, you can see that you can send mail from localhost. This trick also works on a free server that does not allow email services but supports the PHP language. If still you are facing some error comment your error in the below comment box.

Download PHPMailerAutoload file

Best and simple way to send mail from php you need to watch my this video.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !