There are so many ways to send email using php. The two main ways to do this is either using PHPMail() or SMTP. PHPMail() is open and used by trojans and viruses. Most clients have infected files which just send mails using PHPMail(), because it doesn't authenticate. It doesn't check if the email being sent is actually coming from the domain and this puts a lot of strain on the mail server, making legitimate mails wait in queue for a long time.
SMTP isn't open and the email must be authenticated.... a real email address and password must be written into your code and must be verified by the mail server before the email is sent. This makes the email legitimate.
If you already have a script using PHPMail() function it is very simple to change it to SMTP, you just need to change like 5 lines of your code and add an email address and password (already created in cpanel) into the code, and it will send emails just fine.
We have a sample code which you can use in our download section on our site (http://www.qservers.net/process/downloads.php).
If you are using a cms software like wordpress or joomla please use the smtp plugins available for these softwares.
For Joomla:
At the backend of joomla, under global configuration --> Server --> Mail Settings -->
Mailer: SMTP
SMTP Port: 25
SMTP Authetication: Yes
SMTP Security: None
SMTP Username: email address created in cpanel
SMTP Password: password for your email address
SMTP Host: localhost
For Wordpress:
1. http://wordpress.org/plugins/easy-wp-smtp/(By wpecommerce)
2. http://wordpress.org/extend/plugins/contact-form-7/
Edit General Settings for Easy WP SMTP
From Email Address: your email address i.e info@mydomain.com
From Name: a name to give your email address i.e Company Name
SMTP Host: localhost
Type of Encription: None
SMTP Port: 25
SMTP Authentication: Yes
SMTP username: Your email address ( same as the From Email Address)
SMTP Password: Password of the email address above
For Developers:
The PEAR library also has a built-in Mail class for sending e-mails, including e-mails over SMTP authentication with an already-existing e-mail account. Here's an example:
<?php
include('Mail.php'); // includes the PEAR Mail class, already on your server.
$username = 'info@mycompany.com'; // your email address
$password = 'mypassword'; // your email address password
$from = "info@mycompany.com";
$to = "myclient@clients.com";
$subject = "This is a sample smtp script";
$body= "Welcome to QServers.net Secure Mailing Platform.";
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject); // the email headers
$smtp = Mail::factory('smtp', array ('host' =>'localhost', 'auth' => true, 'username' => $username, 'password' => $password, 'port' => '25')); // SMTP protocol with the username and password of an existing email account in your hosting account
$mail = $smtp->send($to, $headers, $body); // sending the email
if (PEAR::isError($mail)){
echo("<p>" . $mail->getMessage() . "</p>");
}
else {
echo("<p>Message successfully sent!</p>");
// header("Location: http://www.example.com/"); // you can redirect page on successful submission.
}
?>
Using PHPMailer (PHP 5.x):
=====================
<?php
require 'phpmailer/PHPMailerAutoload.php'; // Phpmail package already on server
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "localhost"; // sets the SMTP server or use the server hostname
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "info@mydomain.com"; // SMTP account username
$mail->Password = "M7k98u9h3"; // SMTP account password
$mail->SetFrom('info@mydomain.com', 'First Last');
$mail->AddReplyTo("info@mydomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->MsgHTML("<html><body>This is a sample message! <br></body></html>");
$mail->AddAddress("demo@frienddomain.com");
//$mail->AddAttachment(""); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
// header("Location: http://www.example.com/");
}
?>
Using PHPMailer (PHP 7.x):
====================
<?php
use PHPMailer\PHPMailer\PHPMailer; // Phpmail package already on server
use PHPMailer\PHPMailer\Exception; // Phpmail package already on server
require 'PHPMailer/src/PHPMailer.php'; // Phpmail package already on server
require 'PHPMailer/src/SMTP.php'; // Phpmail package already on server
$mail = new PHPMailer(true);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "localhost"; // sets the SMTP server or use the server hostname
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "info@mydomain.com"; // SMTP account username
$mail->Password = "0TsvMU1b4el9"; // SMTP account password
$mail->SetFrom('info@mydomain.com', 'Firstname Lastname');
$mail->AddReplyTo("info@mydomain.com","John Doe");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->MsgHTML("<html><body>This is a sample message! <br></body></html>");
$mail->AddAddress("info@mydomain.com");
//$mail->AddAttachment(""); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
// header("Location: http://www.example.com/");
}
?>