Hi Friends, Hope you are doing well. Today we’ll see Complete Guide for Sending Emails in PHP. Actually, there are many methods to perform it which we learn in this post. I am writing this post because I didn’t found all the methods in one place. So, I am trying to do it.
Sending Mails in PHP using mail() function
Its a one of the traditional way to sending emails but it also have some limitations. It does not provide any assistance for making use of popular features such as encryption, authentication, HTML messages, and attachments.
1 2 3 4 5 6 7 |
<?php $subject = 'I am a Sample Email'; //Subject of Email $message = 'This mail is sent via the PHP mail function'; //message Body mail($to_email,$subject,$message,$headers); ?> |
Output
Sending Mails using PHPMailer
PHP Mailer is a good and most popular library to send emails safely and easily via PHP code from a web server. Sending emails directly by PHP code requires a high-level familiarity with SMTP standard protocol and related issues and vulnerabilities about Email injection for spamming.
If PHP Mailer is already installed so you can add the path of the ‘PHPMailer.php’ else you can download PHP Mailer & Upload to public_html folder & add the path
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php //Import the PHPMailer class into the global namespace use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'PHPMailer-master/src/Exception.php'; require 'PHPMailer-master/src/PHPMailer.php'; require 'PHPMailer-master/src/SMTP.php'; $mail = new PHPMailer; //PhP Mailer Instance $mail->Subject = 'PHPMailer mail() test'; //Subject $mail->Body = 'This is support html template'; //Email Body $mail->addAttachment('attachment.png'); //Attachment //send the message, check for errors if (!$mail->send()) { echo 'Mailer Error: '. $mail->ErrorInfo; } else { echo 'Message sent!'; } ?> |
Output
Sending SMTP Emails
Few hosting providers do not allow sending emails without SMTP Authentication. Like GCP (Google Cloud Platform), If You have disabled emails without authentication from Domain Registrar settings or you want to send emails with other SMTP Server. Then, In this condition, SMTP authentication is required before sending emails. It’s the most secured way for sending emails
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php //Import the PHPMailer class into the global namespace use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'PHPMailer-master/src/Exception.php'; require 'PHPMailer-master/src/PHPMailer.php'; require 'PHPMailer-master/src/SMTP.php'; $mail = new PHPMailer; //PhP Mailer Instance $mail->SMTPDebug = 3; //Enable SMTP Debugging $mail->isSMTP(); //Enable PHPMailer for SMTP Use $mail->Host = "smtp.gmail.com"; //SMTP Host Name $mail->SMTPAuth = true; //Enable SMTP Authentication $mail->Password = "pwd@123zzz"; //Password $mail->SMTPSecure = "tls"; //Enable TLS Encryption $mail->Port = 587; //SMTP Port $mail->addReplyTo('id@domain.com', 'Reply To Name'); //Reply To Email Address $mail->addAddress('sender@domain.com', 'Sendor Name'); // Send Too Email Address $mail->Subject = 'PHPMailer mail() SMTP Test'; //Subject $mail->Body = 'This is support html template'; //Email Body $mail->addAttachment('attatchment.png'); //Attachment //send the message, check for errors if (!$mail->send()) { echo 'Mailer Error: '. $mail->ErrorInfo; } else { echo 'Message sent!'; } ?> |
Output
Sending Emails Using Pear Mail
Pear mail is a class that provides multiple interfaces for sending Emails. It also provides supporting functions useful to multiple mailer backends. Currently, supported backends include PHP’s native mail() function, Sendmail, and SMTP. This package also provides an RFC822 email address list validation utility class.
Before implementing code you need to install Pear mail. Please follow these steps for Pear Mail Installation
Step 1. Login to Hosting Control Panel > Software > Php Pear Packages
Step 2. Note the Include Path.
Example
1 |
ini_set("include_path", '/home/sitename/php:' . ini_get("include_path") ); |
Search ‘Mail’ at PHP Extension(s) and Application(s)
Step 3. Install Mail 1.4.1
Now the installation process has been completed. Here is the sample code for Pear Mail
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php ini_set("include_path", '/home/name/php:' . ini_get("include_path") );// Replace code with your ini path require_once "Mail.php"; $subject = "Pear Mail Email Test"; //Email Subject //SMTP Credentials $host = "domain.com"; $port = "587"; $password = "password@123#"; $email_message = "I'm the body of the message " . "\n"; //Email Body $headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject); 'Reply-To: '.$from."\r\n" . 'X-Mailer: PHP/' . phpversion(); $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $email_message); if (PEAR::isError($mail)) { echo "<p>" . $mail->getMessage() . "</p>"; } else { echo("<p>Message successfully sent!</p>"); } ?> |
Output
Sending Emails using Swift Mailer
Swift Mailer is one of the popular packages for sending emails in PHP. It offers enhanced security and handles large attachments and images with low memory usage.
Click Here to know the steps for Swift Mailer Installation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php require_once './vendor/autoload.php'; try { // Create the SMTP transport $transport = (new Swift_SmtpTransport('smtp.domain.com', 587)) ->setPassword('password@123#'); $mailer = new Swift_Mailer($transport); // Create a message $message = new Swift_Message(); $message->setSubject('Swift Mailer Test Mail'); // Add attachment $attachment = Swift_Attachment::fromPath('./attachment.pdf'); $message->attach($attachment); // Set the plain-text part $message->setBody('Hi I am Plain Text.'); // Set the HTML part $message->addPart('I am html</br>body'); // Send the message $result = $mailer->send($message); } catch (Exception $e) { echo $e->getMessage(); } |
Please like & share this post if you like it
Comments