search
HomeBackend DevelopmentPHP TutorialSolve several problems often encountered when using the mail() function

在我们平常使用PHP发送邮件时,不一定每次都需要使用像phpmailer这样强大的工具类,在网上找到一个不错的PHP mail封装函数,该函数能够解决以下使用mail()函数经常碰到的几个问题 

1.怎样发送HTML格式邮件。

2.邮件主题填上中文是乱码。

3.收件人中除了填上收件人的邮件,还要填上名字,而且还是中文的。 

4.发件人要填上网站的名字,让人收到邮件的时候不是光秃秃的显示邮件地址。// 对邮件地址进行中文的UTF-8编码转化 

function format_mail_address($address){  
   if(preg_match("||", $address, $matches)){  
     $name = mb_substr($address, 0, strpos($address, '  
//函数的使用方法:  
html_mail(  
     "电商",  
     array(  
         "用户A",  
         "用户B"),  
     "这是一封测试邮件",  
”

需要说明的一点是,收件人为数组时,其中一个收件人查看邮件会看到所有其他收件人的邮件地址。如果要分开来发送(互相都看不见各自的邮件地址),可以使用循环一个个发送。使用加密抄送的方式可以实现隐藏加密抄送的邮件地址。

<?php  
 $to = $mailtoname . " <" . $mailtomail . "-->" ;  
 $mailsubject="邮件测试";  
 $mailheader = "X-Priority: 5n";  
 $mailheader.= "From: " . "Sales Teamn";  
 $mailheader.= "X-Sender: " . "support@ec-shalom.comn";  
 $mailheader.= "Return-Path: " . "support@ec-shalom.comn";  
 //普通抄送邮件  
$mailheader.= "Cc: " . $mailcc ."n";  
 //加密抄送  
$mailheader.= "Bcc: " . $mailbcc ."n";  
 $mailbody="电商-mail()函数发送邮件";  
 $result = mail ($to, $mailsubject, $mailbody, $mailheader);  
 echo "  
 
Mail sent to “. “$to”. ” “; echo $mailsubject. ” “; echo $mailbody. ” “; echo $mailheader. ” “; if ($result) { echo “  
 
<strong></strong><strong>Email sent successfully!</strong>  
   
 <strong>"; }else{ echo "</strong>  
   
 <strong></strong><strong>Email could not be sent. </strong>  
   
 <strong>"; } ?></strong>

注意了php mail函数是需要服务器的支持,此函数配置如下:

Windows XP和2000本身就拥有构件SMTP服务器的功能,只是一般还没有安装。选择“控制面板→添加/删除程序→添加/删除Windows组件”,弹出“Windows组件向导”对话框,在其中双击“Internet信息服务(IIS)”项,就会打开详细选择项,选中“SMTP Service”,按“确定”,插入Windows XP安装盘进行安装 

安装好SMTP服务器后,选择“控制面板→性能和维护→管理工具→Internet信息服务”打开Internet信息服务设置窗口,在窗口左侧点击本地计算机名,展开本地计算机目录,可以看到有两个分支“Wed站点”和“默认SMTP虚拟服务器”。在“默认SMTP虚拟服务器”上点击鼠标右键选择“属性”,打开“默认SMTP虚拟服务器属性”窗口。

“常规”选项卡主要设置IP地址,单击IP地址下拉项选择“127.0.0.1”,表示指向本地计算机IP地址,其他项使用默认即可。如果你是局域网接入,拥有固定IP地址,那么IP地址就应该选择相应的地址

“访问”选项卡中设置访问权限。单击“身份验证”,选择“匿名访问”,表示任何用户都可以发送,其他两项不用选择;单击“连接控制”中的“连接”和“中段限制”中的“中断”,选中“仅以下列表除外”,表示可以许接入所有用户的访问。 

“邮件”选项卡中设置邮件传输条件及限制,“限制邮件大小为”等四个选项可以使用默认值,无须更改; 

“将未传递报告的副本发送到”可将发送不成功的邮件返回发件人,并且写明不成功的原因;“死信目录”设置没有发送成功的邮件被存放的位置。 

“传输”选项中设置邮件传递时间,这里不用修改,使用默认值;“LDAP路由”选项用来指定服务器使用的目录服务器标识和属性,这里也不用启动它。

安全”选项中设置使用发送服务器的有权用户,默认用户是“Administrators”,你可以单击“添加”添加使用用户。 

一切设置好后,你就拥护了自己的邮件发送服务器了!

SMTP装好以后 你应该是在WINDOW2K下,你找到PHP。INI文件 在C:WINNT下

 打开找到下面这些行

[mail function]  

 ; For Win32 only.  

 SMTP = 10.0.0.9 -------》替换成你的IP,10.0.0.9是偶的IP了 :)  

; For Win32 only.  

 sendmail_from = test@test.com    ---》发件人信息 

然后重新启动APACHE.

The above is the detailed content of Solve several problems often encountered when using the mail() function. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Apr 17, 2025 am 12:22 AM

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software