$to = "recipient@email.com"; $subject = "This is a subject."; $message = "This is the content of the email."; mail($to, $subject, $message);
require 'PHPMailer/PHPMailerAutoload.php'; $mail = new PHPMailer; $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; // Specify main SMTP server $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'your_email@gmail.com'; // SMTP username $mail->Password = 'YourPassword'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption $mail->Port = 587; // TCP port to connect to $mail->setFrom('your_email@gmail.com', 'Your Name'); $mail->addAddress('recipient@email.com', 'Recipient Name'); $mail->addAttachment('/path/to/file.pdf', 'filename.pdf'); $mail->Subject = 'Email subject'; $mail->Body = 'Email content'; if(!$mail->send()) { echo 'Email could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Email has been sent.'; }In the example above, we are using the PHPMailer library to send an email with an attachment to a specific recipient. We specify the SMTP server to use, the authentication details, and the recipient's email address. We then add any attachments to the email and set the subject and content of the email. Finally, we send the email and handle any errors using an if/else statement. Package library: PHPMailer.