phpmailerHow can I send a UTF-8 encoded email using PHPMailer?
Sending a UTF-8 encoded email using PHPMailer is easy. First, you need to create a new instance of the PHPMailer class:
$mail = new PHPMailer;
Then, you need to set the encoding to UTF-8:
$mail->CharSet = 'UTF-8';
You can then set the other parameters, such as the sender and recipient address, subject and body of the email:
$mail->setFrom('[email protected]', 'Sender Name');
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->Subject = 'UTF-8 encoded email';
$mail->Body = 'This is a UTF-8 encoded email.';
Finally, you can send the email:
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Output example
Message has been sent
Code parts explanation
$mail = new PHPMailer;
- creates a new instance of the PHPMailer class.$mail->CharSet = 'UTF-8';
- sets the encoding to UTF-8.$mail->setFrom('[email protected]', 'Sender Name');
- sets the sender address and name.$mail->addAddress('[email protected]', 'Recipient Name');
- sets the recipient address and name.$mail->Subject = 'UTF-8 encoded email';
- sets the email subject.$mail->Body = 'This is a UTF-8 encoded email.';
- sets the email body.if(!$mail->send()) {
- checks if the email was sent successfully.echo 'Message could not be sent.';
- prints an error message if the email was not sent.echo 'Mailer Error: ' . $mail->ErrorInfo;
- prints the error message.echo 'Message has been sent';
- prints a success message if the email was sent successfully.
Relevant links
More of Phpmailer
- How can I configure PHPMailer to support Polish characters?
- How can I set up PHPMailer to use Zimbra SMTP?
- How can I configure PHPMailer to work with GoDaddy?
- How do I use PHPMailer to attach a ZIP file?
- How can I use PHPMailer to send emails with a Yahoo account?
- How do I use PHPMailer with Yii2?
- How do I update PHPMailer?
- How do I use PHPMailer to send a file?
- How do I install PHPMailer using Composer?
- How can I use PHPMailer to send emails through Yandex?
See more codes...