tencent cloud

Simple Email Service

News and Announcements
Release Notes
Announcement on a Newly Added Independent IP Value-Added Service
Product Introduction
Overview
Features
Strengths
Use Cases
Specifications
Purchase Guide
Pricing
Getting Started
Email Configuration
Email Sending
Reputation Level
Console Guide
Configuration
Email Sending
Statistics
Delivery Response
API Documentation
History
Introduction
API Category
Making API Requests
Domain Verification APIs
Mail APIs
Template APIs
Statistics APIs
Sender Address APIs
Blocklist APIs
Sending Status Query APIs
Batch Task APIs
Custom Blocklist APIs
Data Types
Error Codes
SMTP Documentation
SMTP Email Sending Guide
SMTP Service Address
Sample Call for Java
Sample Call for Go
Sample Call for PHP
Sample Call for Python
Sample Call for C#
Sending Email with Attachment
Error Code
Webhook Documentation
Email Event Notification
Email Sending Restrictions
Attachment Types
FAQs
Getting Started
Billing
Sending Limits
Sending Feature
Domain
Template
Identity Verification and Configuration
Independent IP Related Questions
Delivery
API Use
Callback
Spam Folder
Security
Domain Acceleration Configuration
Important Notes
Others
Contact Us

Sample Call for PHP

PDF
Focus Mode
Font Size
Last updated: 2023-12-22 10:28:04

Notes

1. We recommend you use the PHPMailer package:
If your project is a new one and uses composer, then just add "phpmailer/phpmailer": "^6.5" to composer.json, or run composer require phpmailer/phpmailer and use the following code.
If your project is an old one and does not use composer, you need to manually import PHPMailer.
2. For the service address and port, see SMTP Service Address.
Below is the sample code:
<?php

use PHPMailer\\PHPMailer\\PHPMailer;
use PHPMailer\\PHPMailer\\SMTP;
use PHPMailer\\PHPMailer\\Exception;
require './PHPMailer/src/Exception.php';
require './PHPMailer/src/PHPMailer.php';
require './PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true);

try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->SMTPAuth = true; //Enable SMTP authentication
//$mail->AuthType = 'LOGIN';
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'sg-smtp.qcloudmail.com'; //Set the SMTP server to send through
$mail->Username = 'abc@qq.aa.com'; //SMTP username
$mail->Password = '123456'; //SMTP password

$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->CharSet = PHPMailer::CHARSET_UTF8;
$mail->CharSet = 'UTF-8';
$mail->ContentType = 'text/plain; charset=UTF-8';
$mail->Encoding = PHPMailer::ENCODING_BASE64;
//$mail->Encoding = '8bit';
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

//Recipients
$mail->setFrom('abc@qq.aa.com', 'fromName');
$mail->addAddress('test@test.com', 'toName'); //Add a recipient
//$mail->addAddress('ellen@example.com'); //Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

//Attachments
$mail->addAttachment('./tmp.txt'); //Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name

//Content
//$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}


Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback