What is the best way to transfer emails from a MySQL database to Outlook using PHP?
To transfer emails from a MySQL database to Outlook using PHP, you can fetch the emails from the database and format them into an appropriate structure (such as HTML) that can be easily displayed in an email client like Outlook. You can then use PHP's mail function to send the emails to the desired recipients.
// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Fetch emails from database
$sql = "SELECT * FROM emails";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$email = $row['email'];
$subject = $row['subject'];
$message = $row['message'];
// Format email content
$email_content = "<html><body>";
$email_content .= "<h1>$subject</h1>";
$email_content .= "<p>$message</p>";
$email_content .= "</body></html>";
// Send email using PHP mail function
$to = $email;
$subject = $subject;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($to, $subject, $email_content, $headers);
}
} else {
echo "0 results";
}
// Close MySQL connection
$conn->close();