Php html email script
Here is a basic PHP script that sends an email using HTML content:
<?php
// Define the email settings
$to = '[email protected]';
$subject = 'Test Email';
$message = '<html>
<head>
<title>Test Email</title>
</head>
<body>
<h1>This is a test email</h1>
<p>This email was sent using PHP.</p>
</body>
</html>';
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers.= 'Content-type: text/html; charset=UTF-8'. "\r\n";
// Send the email
mail($to, $subject, $message, $headers);
?>
This script uses the mail()
function to send an email to the recipient. The $message
variable contains the HTML content of the email, which is sent as the body of the email. The $headers
variable defines the email headers, including the MIME type and character encoding.
You can customize this script to fit your needs by modifying the $to
, $subject
, and $message
variables. You can also add additional headers or attachments to the email as needed.
Here are some additional tips:
- Make sure to replace
[email protected]
with the actual email address of the recipient. - You can use a template engine like Twig or Smarty to render the HTML content of the email.
- You can use a library like PHPMailer to send emails with attachments and other advanced features.
- You should validate and sanitize any user input before sending the email to prevent security vulnerabilities.
Here is an example of how you can use PHPMailer to send an email with attachments:
<?php
require_once 'PHPMailer/PHPMailer.php';
require_once 'PHPMailer/SMTP.php';
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->Subject = 'Test Email';
$mail->Body = '<html>
<head>
<title>Test Email</title>
</head>
<body>
<h1>This is a test email</h1>
<p>This email was sent using PHPMailer.</p>
</body>
</html>';
$mail->addAttachment('path/to/file.pdf', 'application/pdf', 'base64', 'File Name');
if (!$mail->send()) {
echo 'Error sending email: '. $mail->ErrorInfo;
} else {
echo 'Email sent successfully!';
}
?>
This script uses the PHPMailer library to send an email with an attachment. You can customize the script to fit your needs by modifying the $mail
object and its properties.