百科狗-知识改变命运!
--

mail() - php 邮件函数mail

乐乐1年前 (2023-11-21)阅读数 15#技术干货
文章标签句号

mail()

(PHP 4, PHP 5, PHP 7)

发送邮件

说明

mail(string $to,string $subject,string $message[,string $additional_headers[,string $additional_parameters]]): bool

发送一封电子邮件。

参数

$to

电子邮件收件人,或收件人列表。

本字符串的格式必须符合» RFC 2822。例如:

  • user@example.com
  • user@example.com, anotheruser@example.com
  • User
  • User , Another User
$subject

电子邮件的主题。

Caution

本项不能包含任何换行符,否则邮件可能无法正确发送。

$message

所要发送的消息。

行之间必须以一个 LF(n)分隔。每行不能超过 70 个字符。

Caution

(Windows 下)当 PHP 直接连接到 SMTP 服务器时,如果在一行开头发现一个句号,则会被删掉。要避免此问题,将单个句号替换成两个句号。

$additional_headers(可选项)

String to be inserted at the end of the email header.

This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (rn).

Note:

When sending mail, the mailmustcontain aFromheader. This can be set with the$additional_headersparameter, or a default can be set inphp.ini.

Failing to do this will result in an error message similar toWarning: mail():"sendmail_from" not set in php.ini or custom "From:" header missing.Note:

If messages are not received, try using a LF (n) only. Some poor quality Unix mail transfer agents replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with» RFC 2822.$additional_parameters(optional)

The$additional_parametersparameter can be used to pass an additional parameter to the program configured to use when sending mail using thesendmail_pathconfiguration setting. For example, this can be used to set the envelope sender address when using sendmail with the-fsendmail option.

The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a 'X-Warning' header from being added to the message when the envelope sender (-f) is set using this method. For sendmail users, this file is/etc/mail/trusted-users.

返回值

ReturnsTRUEif the mail was successfully accepted for delivery,FALSEotherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

更新日志

版本说明
4.3.0 (Windows only)All custom headers (like From, Cc, Bcc and Date) are supported, and are not case-sensitive.(As custom headers are not interpreted by the MTA in the first place, but are parsed by PHP, PHP
4.2.3The$additional_parametersparameter is disabled insafe_modeand themail()function will expose a warning message and returnFALSEwhen used.
4.0.5The$additional_parametersparameter was added.

范例

Sending mail.

Usingmail()to send a simple email:

Sending mail with extra headers.

The addition of basic headers, telling the MUA the From and Reply-To addresses:

Sending mail with an additional command line parameter.

The$additional_parametersparameter can be used to pass an additional parameter to the program configured to use when sending mail using thesendmail_path.

Sending HTML email

mail() - php 邮件函数mail

It is also possible to send HTML email withmail().

Note:

If intending to send HTML or otherwise Complex mails, it is recommended to use the PEAR package» PEAR::Mail.

注释

Note:

The Windows implementation ofmail()differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means aMTAis needed listening on a network socket (which can either on the localhost or a remote machine).

Second, the custom headers likeFrom:,Cc:,Bcc:andDate:arenotinterpreted by theMTAin the first place, but are parsed by PHP.

As such,the$toparameter should not be an address in the form of "Something ". The mail command may not parse this properly while talking with the MTA.Note:

It is worth noting that themail()function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.

For the sending of large amounts of email, see the» PEAR::Mail,and» PEAR::Mail_Queuepackages.Note:

The following RFCs may be useful:» RFC 1896,» RFC 2045,» RFC 2046,» RFC 2047,» RFC 2048,» RFC 2049,and» RFC 2822.

参见

  • imap_mail()Send an email message
  • » PEAR::Mail
  • » PEAR::Mail_Mime
Security advice: Although it is not documented, for the parameters $to and $subject the mail() function changes at least \r and \n to space. So these parameters are safe against injection of additional headers. But you might want to check $to for commas as these separate multiple addresses and you might not want to send to more than one recipient.
The crucial part is the $additional_headers parameter. This parameter can't be cleaned by the mail() function. So it is up to you to prevent unwanted \r or \n to be inserted into the values you put in there. Otherwise you just created a potential spam distributor.
Often it's helpful to find the exact error message that is triggered by the mail() function. While the function doesn't provide an error directly, you can use error_get_last() when mail() returns false.

(Tested successfully on Windows which uses SMTP by default, but sendmail on Linux/OSX may not provide the same level of detail.)
Thanks to https://stackoverflow.com/a/20203870/195835
I migrated an application to a platform without a local transport agent (MTA). I did not want to configure an MTA, so I wrote this xxmail function to replace mail() with calls to a remote SMTP server. Hopefully it is of some use.
function xxmail($to, $subject, $body, $headers)
{
 $smtp = stream_socket_client('tcp://smtp.yourmail.com:25', $eno, $estr, 30);
 $B = 8192;
 $c = "\r\n";
 $s = 'myapp@someserver.com';
 fwrite($smtp, 'helo ' . $_ENV['HOSTNAME'] . $c);
 $junk = fgets($smtp, $B);
 // Envelope
 fwrite($smtp, 'mail from: ' . $s . $c);
 $junk = fgets($smtp, $B);
 fwrite($smtp, 'rcpt to: ' . $to . $c);
 $junk = fgets($smtp, $B);
 fwrite($smtp, 'data' . $c);
 $junk = fgets($smtp, $B);
 // Header 
 fwrite($smtp, 'To: ' . $to . $c); 
 if(strlen($subject)) fwrite($smtp, 'Subject: ' . $subject . $c); 
 if(strlen($headers)) fwrite($smtp, $headers); // Must be \r\n (delimited)
 fwrite($smtp, $headers . $c); 
 // Body
 if(strlen($body)) fwrite($smtp, $body . $c); 
 fwrite($smtp, $c . '.' . $c);
 $junk = fgets($smtp, $B);
 // Close
 fwrite($smtp, 'quit' . $c);
 $junk = fgets($smtp, $B);
 fclose($smtp);
}
Make sure you enclose \r\n in double quotes (not single quotes!) so that PHP can translate that into the correct linefeed code
The 'sendmail' executable which PHP uses on Linux/Mac (not Windows) expects "\n" as a line separator.
This executable is a standard, and emulated by other MTAs.
"\n" is confirmed required for qmail and postfix, probably also for sendmail and exim but I have not tested.
If you pass through using "\r\n" as a separator it may appear to work, but your email will be subtly corrupted and some middleware may break. It only works because some systems will clean up your mistake.
If you are implementing DKIM be very careful, as DKIM checks will fail (at least on popular validation tools) if you screw this up. DKIM must be calculated using "\r\n" but then you must switch it all to "\n" when using the PHP mail function.
On Windows, however, you should use "\r\n" because PHP is using SMTP in this situation, and hence the normal rules of the SMTP protocol (not the normal rules of Unix piping) apply.
Note that there is a big difference between the behavior of this function on Windows systems vs. UNIX systems. On Windows it delivers directly to an SMTP server, while on a UNIX system it uses a local command to hand off to the system's own MTA.
The upshot of all this is that on a Windows system your message and headers must use the standard line endings \r\n as prescribed by the email specs. On a UNIX system the MTA's "sendmail" interface assumes that recieved data will use UNIX line endings and will turn any \n to \r\n, so you must supply only \n to mail() on a UNIX system to avoid the MTA hypercorrecting to \r\r\n.
If you use plain old \n on a Windows system, some MTAs will get a little upset. qmail in particular will refuse outright to accept any message that has a lonely \n without an accompanying \r.
Here's a small handy function I use to send email in UTF-8. 
If you notice wrong displayed characters in the email it's because you need to properly set the Content-Type and the Charset in the headers of the email:

Mostly, UTF-8 is your best choice.
You can set custom headers with the fourth parameter of the mail() function.
To make the whole thing waterproof, add the following header too:

Now you can use the combination of UTF-8 and Base64 to properly encode the subject line and the recipient name like this:

And don't forget to Base64 encode the email message too:

All references are taken from:
https://dev.to/lutvit/how-to-make-the-php-mail-function-awesome-3cii
I was having delivery issues from this function to Gmail, Yahoo, AOL, etc. I used the notes here to figure that you need to be setting your Return-Path to a valid email to catch bounces. There are two extra delivery gotchas on top of that:
1) The domain in the email used in the -f option in the php.ini sendmail parameter or in the mail() extra parameters field, needs to have a valid SPF record for the domain (in DNS as a "TXT" record type for sure and add an additional "SPF" type record if possible). Why? That's header field being used for spam checks.
2) You should also use a domain key or DKIM. The trick here is that the domain key/DKIM is case sensitive! I used Cpanel to create my domain key which automatically used all lowercase domain names in the key creation. I found when sending email and using a camel case "-f account@MyDomainHere.Com" option, my key was not accepted. However it was accepted when I used "-f account@mydomainhere.com".
There are many other factors that can contribute to mail not getting to inboxes, including your own multiple failed testing attempts, so I suggest you consult each site's guidelines and don't ask me for help. These are just the couple technical issues that helped my case.
I hope this saves someone some time and headaches...
I recently changed hosting companies and spent a day trying to see why an email script that had been working for years failed on the new server.
The answer was that the old hosting company's email server accepted multiple "CC:" lines in the additional headers string, and the new did not. Thus on the new server
...
$add_hdr .= "CC: " . $email1 . PHP_EOL;
$add_hdr .= "CC: " . $email2 . PHP_EOL;
...
did not work, but
...
$add_hdr .= "CC: " . $email1 . ", " . $email2 . PHP_EOL;
...
did work.
In both cases, PHP's mail() function returned no error, but until I placed both emails, comma-separated, in the same line I was getting the following error:
550 Messages should have one or no Cc headers, not 2.
Hope this helps someone.
It is worth noting that you can set up a fake sendmail program using the sendmail_path directive in php.ini.
Despite the comment in that file, sendmail_path also works for Window. From https://www.php.net/manual/en/mail.configuration.php#ini.sendmail-path:
This directive works also under Windows. If set, smtp, smtp_port and sendmail_from are ignored and the specified command is executed.
Be careful to not put extra spaces for the $headers variable.
For example, this didn't work on our servers:
$headers = "From: $from \r\n Bcc: $bcc \r\n";
But this did:
$headers = "From: $from\r\nBcc: $bcc\r\n";
Notice the removal of the spaces around the first \r\n.
Send mail with minimal requirements from email services. 
I have tried many online tutorials to get mail() function working in windows, until i stumbled upon this website
http://php.codeindepth.com/php-sending-mail/
It really boils down to changing few directives in php.ini and sendmail.ini
Changes required in sendmail.ini
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=your-gmail-id@gmail.com
auth_password=your-gmail-password
force_sender=your-gmail-id@gmail.com
Changes required in php.ini
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = your-gmail-id@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
;sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"
If you use mutt, do as below, 
/usr/bin/mutt -s '$subject' -f /dev/null -e 'set copy=no' -e 'set from = "{$GLOBALS[cfg][email_from]}"' -a '$attach_file_full_path' '{$GLOBALS[cfg][email_to]}' &1;
To define a mail sensitivity you have to put this line in the headers:
 
Possible Options:
Sensitivity: Normal, Personal, Private and Company-Confidential
These will be recognised and handled in Outlook, Thunderbird and others.
Bare LFs in SMTP
Use This 
If you are using the sendmail app from an exim package or something you don't really need to change the normal parameters PHP gives it (-t -i) as other posts described.
I just added "-f myemail@example.com" and it worked.
One thing that got me stuck for a few hours was trying to figure out why the return-path was set as the user (user running php) and not what I was setting it with the -f option then I later found at that in order to forcefully set the return-path the user account running the command must be in exim's trusted users configuration! It helps to add trusted_groups as well then everything works fine :)
- Richard Sumilang
Correction to my earlier note:
'"\n" is confirmed required for qmail and postfix, probably also for sendmail and exim but I have not tested.'
It only affects qmail.
Currently my hosting service is on Godaddy. When attempting to use the mail function without the fifth parameter containing "-f", my message headers would not work.
Whenever your message headers do not work, simply try using the fifth parameter: 
For qmail users, I have written a function that talks directly to qmail-queue, rather than going through the sendmail wrapper used by mail(). Thus it allows more direct control over the message (for example, you can adapt the function to display "undisclosed recipients" in to the To: header). It also performs careful validation of the e-mail addresses passed to it, making it more difficult for spammers to exploit your scripts.
Please note that this function differs from the mail() function in that the from address must be passed as a _separate_ argument. It is automatically put into the message headers and _does not_ need to be included in $additional_headers.
$to can either be an array or a single address contained in a string.
$message should not contain any carriage return characters - only linefeeds.
No validation is performed on $additional_headers. This is mostly unnecessary because qmail will ignore any additional To: headers injected by a malicious user. However if you have some strange mail setup it might be a problem.
The function returns false if the message fails validation or is rejected by qmail-queue, and returns true on success. 
When dealing with mail headers "\n" and "\r\n" *sometimes* makes a big difference.
Once our CentOs servers got re-installed, all headers like:
 $headers = "MIME-Version: 1.0\r\n";
 $headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
 ...
became part of message body instead of headers
I was able to fixed this by replacing "\r\n" with just "\n"
Just a comment on some of the examples, and as a note for those who may be unaware. The SMTP RFC 822 is VERY explicit in stating that \r\n is the ONLY acceptable line break format in the headers, though is a little vague about the message body. While many MTAs will deal with just \n, I've run accross plenty of them that will exhibit "interesting" behaviours when this happens. Those MTAs that are strict in compliance will definitely break when header lines are terminated with only \n. They will also most likely break if the body of the message contains more than 1000 consecutive characters without a \r\n.*
Note that RFC 821 is a little more clear in defining:
"line
   A a sequence of ASCII characters ending with a ."
RFC 821 makes no distinction between header lines and message body lines, since both are actually transmitted during the DATA phase.
Bottom line, best practice is to be sure to convert any bare \n characters in the message to \r\n.
* "The maximum total length of a text line including the  is 1000 characters" (RFC 821)
***Encoding plain text as quoted-printable in MIME email***
If you don't want to install IMAP and use imap_8bit() to encode plain text or html message as quoted-printable
(friendly french special characters encoding :-) in MIME email, try this function.
I haven't fully tested it ( like with microtime with long mails). I send html message as 7-bit, so I didn't try yet with html.
If you have good html practise, you don't really need to encode html as quote-printable as it only uses 7-bit chars.
F.Touchard 
When using the PHP mail() function with IIS 6 on Windows Server 2003, check your "Relay" settings on the SMTP Virtual Server in IIS. If you grant access to 127.0.0.1 and set then set your php.ini SMTP to the same IP address (along with setting the same port 25), you should have success in sending mail. 
I'm using PHP 5.3 and have had success with this configuration and did not have to define the "sendmail_from" setting in our php.ini file.
My mime multipart/alternative messages were going ok, until I switched to qmail with php .. after years of painfull searching, I came across this on the Life With Qmail 'Gotchas' section:
G.11. Carriage Return/Linefeed (CRLF) line breaks don't work
qmail-inject and other local injection mechanisms like sendmail don't work right when messages are injected with DOS-style carriage return/linefeed (CRLF) line breaks. Unlike Sendmail, qmail requires locally-injected messages to use Unix newlines (LF only). This is a common problem with PHP scripts.
So now, I can go back to sending emails with text AND html components :)
When sending MIME email make sure you follow the documentation with the "70" characters per line...you may end up with missing characters...and that is really hard to track down...
If you want to send UTF-8 HTML letter you need to mention charset twice:
1) In message header:

2) In HTML header:

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)