PHP email

To bolster the security of outbound emails, we strongly advise against utilizing the PHP mail() function. This function lacks authentication, posing a vulnerability easy to exploit by harmful scripts. Consequently, we have discontinued the mail() function in our system.

We recommend using SMTP (Simple Mail Transfer Protocol) instead, as it requires authentication and provides a more secure way to send emails. With PHP, you can easily implement SMTP using popular libraries like PHPMailer. A comprehensive tutorial on setting up PHPMailer can be found at http://stephaneey.developpez.com/tutoriel/php/phpmailer/.

Requirements

  • a Web Hosting service

  • an OnetSolutions account

Instructions

To configure SMTP, you will need the following information:

  • SMTP Username: Provided in your hosting activation email, often the cPanel username.

  • SMTP Password: Also provided in your hosting activation email, often the cPanel password.

  • Email: The email address you wish to use (you can create email accounts from your cPanel).

To send emails in PHP using SMTP, you can follow these steps:

  1. Make sure you have the PHPMailer library installed in your PHP project. If not, you can download it from the official PHPMailer GitHub repository (https://github.com/PHPMailer/PHPMailer).

  2. Include the PHPMailer library in your PHP script by adding the following line at the beginning of your code:

require 'path/to/PHPMailer/src/PHPMailer.php';
  1. Set up the SMTP configuration for your email provider. Here's an example of how to configure PHPMailer for popular email services like Gmail:

$mail = new PHPMailer\PHPMailer\PHPMailer();

// Enable SMTP debugging
$mail->SMTPDebug = 0; // Set to 2 for detailed debugging

// Set up SMTP server settings
$mail->isSMTP();
$mail->Host = '[HOST];
$mail->SMTPAuth = true;
$mail->Username = '[USERNAME]';
$mail->Password = 'your-password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

// Set up email content
$mail->setFrom('your-email@gmail.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Subject of your email';
$mail->Body = 'Content of your email';

// Send the email
if (!$mail->send()) {
    echo 'Error sending email: ' . $mail->ErrorInfo;
} else {
    echo 'Email sent successfully!';
}

Make sure to replace 'your-email@gmail.com' and 'your-password' with your actual email credentials. Adjust the SMTP server settings, port, and security method as per your email provider's requirements.

  1. Customize the $mail object to set the sender, recipient, subject, and body of your email.

  2. Finally, call the $mail->send() method to send the email. Check the return value to handle any errors that may occur during the sending process.

By following these steps, you can send emails using PHPMailer and SMTP authentication. Remember to test your code and ensure that the email provider allows SMTP access from your hosting server.

Last updated