How send mail in php without using mail function
There are several ways to send mail in PHP without using the built-in mail()
function. Here are a few options:
- PHPMailer: PHPMailer is a popular PHP library that provides a simple and easy-to-use interface for sending emails. You can install it using Composer or download it from the official website.
Example:
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_password';
$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email';
$mail->send();
- SwiftMailer: SwiftMailer is another popular PHP library for sending emails. You can install it using Composer or download it from the official website.
Example:
require 'SwiftMailer/Swift.php';
$mailer = Swift_Mailer::newInstance();
$mailer->setFrom('[email protected]', 'Your Name');
$mailer->addAddress('[email protected]', 'Recipient Name');
$mailer->Subject = 'Test Email';
$mailer->Body = 'This is a test email';
$mailer->send();
- Gmail API: If you have a Google Apps account, you can use the Gmail API to send emails. You'll need to set up a project in the Google Cloud Console and enable the Gmail API.
Example:
require 'google/apiclient/autoload.php';
$client = new Google_Client();
$client->setApplicationName("Your App Name");
$client->setDeveloperKey("YOUR_API_KEY");
$service = new Google_Service_Gmail($client);
$message = new Google_Service_Gmail_Message();
$message->setTo('[email protected]');
$message->setSubject('Test Email');
$message->setTextBody('This is a test email');
$service->users_messages->send('me', $message);
- Sendmail: If you have a Sendmail server set up on your server, you can use the
sendmail
command-line utility to send emails.
Example:
exec('sendmail -t -i -f [email protected] [email protected] < email.txt');
Note: Make sure to replace [email protected]
and [email protected]
with your actual email addresses.
Remember to always validate and sanitize user input when sending emails to prevent spam and security issues.