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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

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.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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