phpmailerHow do I use PHPMailer to embed an image in an email using Base64 encoding?
To use PHPMailer to embed an image in an email using Base64 encoding, you need to use the addStringAttachment()
method. This method takes three parameters: the content of the attachment, the filename, and the encoding type. The content should be the Base64 encoded string of the image, the filename should be the desired name of the image, and the encoding type should be 'base64'
.
Example code
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Receiver');
$mail->Subject = 'Embedded Image';
$mail->Body = 'This is an embedded image:';
$imageString = base64_encode(file_get_contents('image.jpg'));
$mail->addStringAttachment($imageString, 'image.jpg', 'base64');
$mail->send();
echo 'Message has been sent';
?>
Output example
Message has been sent
Code explanation
require 'vendor/autoload.php';
- This line includes the PHPMailer library.$mail = new PHPMailer(true);
- This line creates a new instance of the PHPMailer class.$mail->isSMTP();
- This line sets the mailer to use SMTP.$mail->Host = 'smtp.gmail.com';
- This line sets the SMTP host.$mail->SMTPAuth = true;
- This line sets SMTP authentication to true.$mail->Username = '[email protected]';
- This line sets the SMTP username.$mail->Password = 'password';
- This line sets the SMTP password.$mail->SMTPSecure = 'tls';
- This line sets the SMTP security type.$mail->Port = 587;
- This line sets the SMTP port.$mail->setFrom('[email protected]', 'Mailer');
- This line sets the from address and name.$mail->addAddress('[email protected]', 'Receiver');
- This line adds the recipient address and name.$mail->Subject = 'Embedded Image';
- This line sets the email subject.$mail->Body = 'This is an embedded image:';
- This line sets the email body text.$imageString = base64_encode(file_get_contents('image.jpg'));
- This line encodes the image in Base64.$mail->addStringAttachment($imageString, 'image.jpg', 'base64');
- This line adds the image as an attachment to the email.$mail->send();
- This line sends the email.
Helpful links
More of Phpmailer
- How can I use PHPMailer in Yii 1?
- How can I use PHPMailer to send emails through Outlook?
- How do I install PHPMailer using Composer?
- How can I configure PHPMailer to support Polish characters?
- How can I use PHPMailer without SMTP secure?
- How do I determine which version of PHPMailer I'm using?
- How can I catch and handle a PHPMailer exception?
- How can I configure PHPMailer to work with GoDaddy?
- How can I add a new line to the body of an email using PHPMailer?
- How do I use PHPMailer to send an HTML email with UTF-8 encoding?
See more codes...