Home > Backend Development > PHP Tutorial > How to send email using SwiftMailer in php?

How to send email using SwiftMailer in php?

PHPz
Release: 2023-06-01 10:34:01
Original
1242 people have browsed it

PHP is a high-level programming language widely used in network development. Its powerful functions have been widely used in network communications and email notifications. SwiftMailer is a popular PHP email sender. It is simple and easy to use, can send emails quickly, and can also customize beautiful email templates to improve the visual effect of emails. This article will introduce how to use SwiftMailer to send emails, allowing you to easily complete the email sending function.

1. Installation of SwiftMailer

Before using SwiftMailer, you need to install it first. SwiftMailer can be installed through Composer or downloaded and imported manually.

1. Install through Composer

In the project directory, execute the following command:

composer require swiftmailer/swiftmailer
Copy after login

2. Manual download and installation

Download from the official website of SwiftMailer For the latest version, unzip the downloaded files into your project. In the PHP file, use require_once to import SwiftMailer’s autoload.php file.

require_once '/path/to/swift_required.php';
Copy after login

2. Sending emails

SwiftMailer provides several ways to send emails: using the SMTP protocol to send emails, using the php mail() function to send emails, and using the sendmail command to send emails. This article will introduce how to send emails using the SMTP protocol, which is a relatively stable and reliable method.

1. Prepare SMTP and email information

Before using the SMTP protocol to send emails, you need to prepare SMTP and email-related information. SMTP is the email server address and port number provided by the email sending service provider. Email information includes sender address, recipient address, email subject, email content, etc.

// SMTP信息
$smtp_server = 'smtp.example.com';
$smtp_port = 465;
$smtp_username = 'user@example.com';
$smtp_password = 'password';

// 邮件信息
$from_email = 'from@example.com';
$to_email = 'to@example.com';
$subject = '邮件主题';
$body = '邮件内容';
Copy after login

2. Create a SwiftMailer instance

After preparing the SMTP and email information, you need to create a SwiftMailer instance and call the method of sending emails through the instance.

// 创建SMTP实例
$transport = (new Swift_SmtpTransport($smtp_server,$smtp_port))
  ->setUsername($smtp_username)
  ->setPassword($smtp_password);

// 创建邮件实例
$message = (new Swift_Message($subject))
  ->setFrom($from_email)
  ->setTo($to_email)
  ->setBody($body);
Copy after login

3. Send email

Use the created SMTP instance and email instance and call the method of sending email to send the email.

// 创建Mailer实例
$mailer = new Swift_Mailer($transport);

// 发送邮件
$result = $mailer->send($message);

if($result){
  echo '发送成功';
}else{
  echo '发送失败';
}
Copy after login

The complete code is as follows:

// SMTP信息
$smtp_server = 'smtp.example.com';
$smtp_port = 465;
$smtp_username = 'user@example.com';
$smtp_password = 'password';

// 邮件信息
$from_email = 'from@example.com';
$to_email = 'to@example.com';
$subject = '邮件主题';
$body = '邮件内容';

// 创建SMTP实例
$transport = (new Swift_SmtpTransport($smtp_server,$smtp_port))
  ->setUsername($smtp_username)
  ->setPassword($smtp_password);

// 创建邮件实例
$message = (new Swift_Message($subject))
  ->setFrom($from_email)
  ->setTo($to_email)
  ->setBody($body);

// 创建Mailer实例
$mailer = new Swift_Mailer($transport);

// 发送邮件
$result = $mailer->send($message);

if($result){
  echo '发送成功';
}else{
  echo '发送失败';
}
Copy after login

3. Customized email template

When using SwiftMailer to send emails, in addition to setting the basic attributes of the email, you can also customize it Email templates improve the visual effect of emails. In the email template, you can add pictures, styles, tables and other elements to make the email more beautiful. This article will take HTML email template as an example to introduce how to customize email templates.

1. Prepare HTML template

When creating an email instance, you can set the content type of the email instance through the second parameter. To send HTML email, it needs to be set to 'text/html'. In fact, the email content can be any HTML format string, so you can prepare the HTML template first, and then dynamically generate the email content by inserting variables into the email content.

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>邮件模板</title>
  <style>
    * {margin: 0;padding: 0;}
    body {font-family: Arial, Helvetica, sans-serif;}
    h1 {text-align: center;margin-top: 50px;}
    p {line-height: 1.5;font-size: 16px;margin-top: 30px;text-indent: 2em;}
  </style>
</head>
<body>
  <h1>邮件标题</h1>
  <p>尊敬的<span style="color: red;">{{name}}</span>,您好!这是一封测试邮件。</p>
</body>
</html>
Copy after login

2. Set the email content

After preparing the HTML template, you need to replace the content that needs to be dynamically generated in the template with the placeholder {{name}}, through the replace() method Replace it with the actual value. The final generated email content is a complete HTML string.

// 邮件内容中需要动态生成的部分
$name = '张三';

// 读取HTML模板
$html_template = file_get_contents('/path/to/template.html');

// 替换占位符为实际的值
$html_body = str_replace("{{name}}", $name, $html_template);

// 创建邮件实例,设置邮件内容
$message = (new Swift_Message($subject))
  ->setFrom($from_email)
  ->setTo($to_email)
  ->setBody($html_body,'text/html');
Copy after login

The complete code is as follows:

// SMTP信息
$smtp_server = 'smtp.example.com';
$smtp_port = 465;
$smtp_username = 'user@example.com';
$smtp_password = 'password';

// 邮件信息
$from_email = 'from@example.com';
$to_email = 'to@example.com';
$subject = '邮件主题';

// 邮件内容中需要动态生成的部分
$name = '张三';

// 读取HTML模板
$html_template = file_get_contents('/path/to/template.html');

// 替换占位符为实际的值
$html_body = str_replace("{{name}}", $name, $html_template);

// 创建SMTP实例
$transport = (new Swift_SmtpTransport($smtp_server,$smtp_port))
  ->setUsername($smtp_username)
  ->setPassword($smtp_password);

// 创建邮件实例,设置邮件内容
$message = (new Swift_Message($subject))
  ->setFrom($from_email)
  ->setTo($to_email)
  ->setBody($html_body,'text/html');

// 创建Mailer实例
$mailer = new Swift_Mailer($transport);

// 发送邮件
$result = $mailer->send($message);

if($result){
  echo '发送成功';
}else{
  echo '发送失败';
}
Copy after login

Summary

SwiftMailer, as a powerful and easy-to-use PHP email sender, can easily realize the function of sending emails in PHP. When using SwiftMailer to send emails, you need to install and import the auto-loading file first. Before sending emails, you need to prepare SMTP and email-related information. You can choose to send emails in HTML format and customize email templates to improve the visual effect of the email. SwiftMailer's email sending functions are very rich, and you can set email priority, attachments, CC, and BCC functions. These functions can be used flexibly according to actual needs.

The above is the detailed content of How to send email using SwiftMailer in php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template