OnetSolutions
EN
Ask or search…
K
Comment on page

PHP email

To enhance the security of outgoing emails, it is advisable to avoid using the PHP mail() function, which does not require authentication and can be exploited by malicious scripts. As a result, we have removed the mail() function from 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. 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. 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. 3.
    Set up the SMTP configuration for your email provider. Here's an example of how to configure PHPMailer for popular email services like Gmail:
1
$mail = new PHPMailer\PHPMailer\PHPMailer();
2
3
// Enable SMTP debugging
4
$mail->SMTPDebug = 0; // Set to 2 for detailed debugging
5
6
// Set up SMTP server settings
7
$mail->isSMTP();
8
$mail->Host = '[HOST];
9
$mail->SMTPAuth = true;
10
$mail->Username = '[USERNAME]';
11
$mail->Password = 'your-password';
12
$mail->SMTPSecure = 'tls';
13
$mail->Port = 587;
14
15
// Set up email content
16
$mail->setFrom('your-email@gmail.com', 'Your Name');
17
$mail->addAddress('recipient@example.com', 'Recipient Name');
18
$mail->Subject = 'Subject of your email';
19
$mail->Body = 'Content of your email';
20
21
// Send the email
22
if (!$mail->send()) {
23
echo 'Error sending email: ' . $mail->ErrorInfo;
24
} else {
25
echo 'Email sent successfully!';
26
}
Make sure to replace '[email protected]' 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. 4.
    Customize the $mail object to set the sender, recipient, subject, and body of your email.
  2. 5.
    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.