Home Backend Development PHP Tutorial Share a php mail library-swiftmailer

Share a php mail library-swiftmailer

Jul 29, 2016 am 08:58 AM
attachment com gt message swift

I recently saw a good php mail library, which has the same function as phpmailer, but its performance is better than phpmailer, especially in terms of its ability to process attachments, and the probability of sending emails successfully is also high. Here is a usage:

<span> 1</span><span>require_once</span> ("lib/swift_required.php"<span>);
</span><span> 2</span><span> 3</span><span>//</span><span> 创建Transport对象,设置邮件服务器和端口号,并设置用户名和密码以供验证</span><span> 4</span><span>$transport</span> = Swift_SmtpTransport::newInstance('smtp.163.com', 25<span>)
</span><span> 5</span> ->setUsername('username@163.com'<span>)
</span><span> 6</span> ->setPassword('password'<span>);
</span><span> 7</span><span> 8</span><span>//</span><span> 创建mailer对象</span><span> 9</span><span>$mailer</span> = Swift_Mailer::newInstance(<span>$transport</span><span>);
</span><span>10</span><span>11</span><span>//</span><span> 创建message对象</span><span>12</span><span>$message</span> = Swift_Message::<span>newInstance();
</span><span>13</span><span>14</span><span>//</span><span> 设置邮件主题</span><span>15</span><span>$message</span>->setSubject('这是一份测试邮件'<span>)
</span><span>16</span><span>17</span><span>//</span><span> 设置邮件内容,可以省略content-type</span><span>18</span> -><span>setBody(
</span><span>19</span>     '<html>' .
<span>20</span>     ' <head></head>' .
<span>21</span>     ' <body>' .
<span>22</span>     ' Here is an image <img src="' . <span>//</span><span> 内嵌文件</span><span>23</span><span>$message</span>->embed(Swift_Image::fromPath('image.jpg')) .
<span>24</span>     '" alt="分享一个php邮件库——swiftmailer" />' .
<span>25</span>     ' Rest of message' .
<span>26</span>     '<a href="http://www.baidu.com">百度</a>'.
<span>27</span>     ' </body>' .
<span>28</span>     '</html>',
<span>29</span>     'text/html'
<span>30</span><span>);
</span><span>31</span><span>32</span><span>//</span><span> 创建attachment对象,content-type这个参数可以省略</span><span>33</span><span>$attachment</span> = Swift_Attachment::fromPath('image.jpg', 'image/jpeg'<span>)
</span><span>34</span> ->setFilename('cool.jpg'<span>);
</span><span>35</span><span>36</span><span>//</span><span> 添加附件</span><span>37</span><span>$message</span>->attach(<span>$attachment</span><span>);
</span><span>38</span><span>39</span><span>//</span><span> 用关联数组设置收件人地址,可以设置多个收件人</span><span>40</span><span>$message</span>->setTo(<span>array</span>('to@qq.com' => 'toName'<span>));
</span><span>41</span><span>42</span><span>//</span><span> 用关联数组设置发件人地址,可以设置多个发件人</span><span>43</span><span>$message</span>->setFrom(<span>array</span><span>(
</span><span>44</span>     'from@163.com' => 'fromName',
<span>45</span><span>));
</span><span>46</span><span>47</span><span>//</span><span> 添加抄送人</span><span>48</span><span>$message</span>->setCc(<span>array</span><span>(
</span><span>49</span>       'Cc@qq.com' => 'Cc'
<span>50</span><span> ));
</span><span>51</span><span>52</span><span>//</span><span> 添加密送人</span><span>53</span><span>$message</span>->setBcc(<span>array</span><span>(
</span><span>54</span>       'Bcc@qq.com' => 'Bcc'
<span>55</span><span>));
</span><span>56</span><span>57</span><span>//</span><span> 设置邮件回执</span><span>58</span><span>$message</span>->setReadReceiptTo('receipt@163.com'<span>);
</span><span>59</span><span>60</span><span>//</span><span> 发送邮件</span><span>61</span><span>$result</span> = <span>$mailer</span>->send(<span>$message</span>);
Copy after login

The above introduces the sharing of a PHP mail library - swiftmailer, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Hongmeng native application random poetry Hongmeng native application random poetry Feb 19, 2024 pm 01:36 PM

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

Apple releases open source Swift package for homomorphic encryption, deployed in iOS 18 Apple releases open source Swift package for homomorphic encryption, deployed in iOS 18 Jul 31, 2024 pm 01:10 PM

According to news on July 31, Apple issued a press release yesterday (July 30), announcing the launch of a new open source Swift package (swift-homomorphic-encryption) for enabling homomorphic encryption in the Swift programming language. Note: Homomorphic Encryption (HE) refers to an encryption algorithm that satisfies the homomorphic operation properties of ciphertext. That is, after the data is homomorphically encrypted, specific calculations are performed on the ciphertext, and the obtained ciphertext calculation results are processed at the same time. The plaintext after state decryption is equivalent to directly performing the same calculation on the plaintext data, achieving the "invisibility" of the data. Homomorphic encryption technology can calculate encrypted data without leaking the underlying unencrypted data to the operation process.

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Integration of Vue.js and Swift language, suggestions for development and testing of advanced iOS applications Integration of Vue.js and Swift language, suggestions for development and testing of advanced iOS applications Aug 01, 2023 am 09:53 AM

Vue.js is a popular JavaScript framework for building user interfaces. The Swift language is a programming language used for iOS and macOS application development. In this article, I will explore how to integrate Vue.js with the Swift language for advanced iOS application development and testing. Before we get started, we need to make sure you have the following software and tools installed: Xcode: an integrated development environment for developing and compiling iOS applications. Node.js: used for

How to use MySQL to implement data import and export functions in Swift How to use MySQL to implement data import and export functions in Swift Aug 01, 2023 pm 11:57 PM

How to implement data import and export functions in Swift using MySQL Importing and exporting data is one of the common functions in many applications. This article will show how to use MySQL database to import and export data in Swift language, and provide code examples. To use the MySQL database, you first need to introduce the corresponding library files into the Swift project. You can do this by adding the following dependencies in the Package.swift file: dependencies:[

What is the difference between comcn and com What is the difference between comcn and com May 12, 2023 pm 04:08 PM

The difference between comcn and com: 1. There are differences between comcn and com in terms of meaning, but there is no difference in access speed; 2. comcn is an international domain name and is a global top-level domain name for use by commercial institutions, while cn is a Chinese company domain name , domestic commercial institutions, domestic domain names, only enterprises can register; 3. The search priority is that cn will search for .cn first. After finding the .cn server, the .cn server will then search for .com; 4. cn is provided by cnnic China Internet Center Management, com's management organization is abroad.

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

See all articles