PHP -Sending Email from Localhost (XAMPP) Using SendinBlue
Enviroment
- Enviroment: Local
- Web Server: XAMP v3.3.0
- OS: Windows 7
1. Create account on sendinblue
2. SMTP Setting
Go to https://account.sendinblue.com/advanced/api and check your smtp settings
3. Xampp php.ini settings
Open xampp control panel, click on apache config button, a context menu will appear, click on php.ini.
Find and set these settings
SMTP=smtp-relay.sendinblue.com
smtp_port=587
sendmail_from = youremail@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Now go to C:\xampp\sendmail\ and open sendmail.ini and set
smtp_server=smtp-relay.sendinblue.com
smtp_port=587
debug_logfile=debug.log
auth_username=sendinblue_login
auth_password=sendinblue_password
4. Write php code
<?php
// the message
$msg = "First line of text\nSecond line of text";
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
$headers = "From: abc@gmail.com";
$success = mail("xyz@gmail.com","My subject",$msg, $headers);
if($success)
{
echo '
<p>Your message has been sent!</p>
';
} else {
echo '
<p>Something went wrong, go back and try again!</p>
';
}
If you want to use phpmailer, follow these steps
You can simply download complete project from my github
or you can follow these
Step 1: Init composer if your project doesn't have compoer.json file (to learn how to install composer check Composer Tutorial)
composer init
Keep Pressing enter if you don't wanna change details
Step 2: Download phpmailer using composer command
composer require phpmailer/phpmailer
Step 3: Write php code
<?php
require "./vendor/autoload.php";
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer;
$mail->From = "youremail@gmail.com";
$mail->FromName = "M Habib"; //To address and name
$mail->addAddress("xyz@gmail.com", "Recepient Name"); //Recipient name is optional
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent successfully";
}
Comments
Post a Comment