I'm available on Fiverr.com. Hire me today!
How to Send Emails with PHP Mail() Function and PHP Mailer
PHP Scripts

How to Send Emails with PHP Mail() Function and PHP Mailer

18 Jul, 2023

How to Send Emails with PHP Mail() Function and PHP Mailer

How to Send Emails with PHP Mail() Function and PHP Mailer, you can use the following steps:

  • Install and include the PHPMailer library in your PHP script. 
  • Create a new instance of the PHPMailer class. 
  • Configure the mail server settings, such as the host, port, and security settings.
  • Set the sender's email address and name using the setFrom method. 
  • Add the recipient's email address using the addAddress method. 
  • Set the email's subject using the setSubject method.
  • Set the email's body using the msgHTML method, which allows you to send an HTML email. 
  • If you have any attachments to send, use the addAttachment method to add them. 
  • Use the send method to send the email.



Example:

<?php
require 'path/to/PHPMailerAutoload.php';

$mail = new PHPMailer;

//Server settings
$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 = 'username';                 // SMTP username
$mail->Password = 'password';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

//Recipients
$mail->setFrom('[email protected]', 'Your name');
$mail->addAddress('[email protected]', 'Recipient name');

//Content
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body    = '
...
'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; } ?>
Share With  Facebook  Twitter  Pinterest  Linkedin  WhatsApp

 Top 5 Related Query

Leave a Reply