|
|
LXXVI. Mail Functions
The mail() function allows you to send mail.
For the Mail functions to be available, PHP must have access to the
sendmail binary on your system during compile time.
If you use another mail program, such as qmail or postfix, be sure to
use the appropriate sendmail wrappers that come with them. PHP will
first look for sendmail in your PATH, and then in
the following:
/usr/bin:/usr/sbin:/usr/etc:/etc:/usr/ucblib:/usr/lib.
It's highly recommended to have sendmail available from your
PATH. Also, the user that compiled PHP must have
permission to access the sendmail binary.
Для использования этих функций не требуется проведение установки,
поскольку они являются частью ядра PHP.
Поведение этих функций зависит от установок в php.ini.
Таблица 1. Mail configuration options | Name | Default | Changeable | Changelog |
|---|
| SMTP | "localhost" | PHP_INI_ALL | | | smtp_port | "25" | PHP_INI_ALL | Available since PHP 4.3.0. | | sendmail_from | NULL | PHP_INI_ALL | | | sendmail_path | NULL | PHP_INI_SYSTEM | |
Для подробного описания констант
PHP_INI_*, обратитесь к документации функции ini_set().
Краткое разъяснение конфигурационных
директив.
SMTP
string
Used under Windows only: host name or IP address of the SMTP server PHP
should use for mail sent with the mail() function.
smtp_port
int
Used under Windows only: Number of the port to connect to the server
specified with the SMTP setting when sending mail
with mail(); defaults to 25. Only available since
PHP 4.3.0.
sendmail_from
string
Which "From:" mail address should be used in mail sent from
PHP under Windows.
This directive also sets the "Return-Path:" header.
sendmail_path
string
Where the sendmail program can be found,
usually /usr/sbin/sendmail or
/usr/lib/sendmail.
configure does an honest attempt of
locating this one for you and set a default, but if it fails,
you can set it here.
Systems not using sendmail should set this directive to the
sendmail wrapper/replacement their mail system offers, if any.
For example, Qmail
users can normally set it to
/var/qmail/bin/sendmail or
/var/qmail/bin/qmail-inject.
qmail-inject does not require any option to
process mail correctly.
This directive works also under Windows. If set, smtp,
smtp_port and sendmail_from are
ignored and the specified command is executed.
Данное расширение не определяет никакие типы ресурсов. Данное расширение не определяет никакие константы. - Содержание
- ezmlm_hash -- Calculate the hash value needed by EZMLM
- mail -- Send mail
webbieatvlaanderendashflandersdotcom
01-Nov-2007 04:19
I have the problem that, using EasyPHP 1.8, I constantly get the error 530, meaning I need to authenticate.
Yet, in my php.ini I have changed the [mail function] to my own ISP's SMTP server and sendmail_from to my own emailaddress.
yet when I reboot, in my php.ini file everything is as I left it, while phpinfo() still tells me my SMTP serve is set to localhost.
Has anyone got any idea where I might have gone wrong?
Joe
17-Oct-2007 03:20
SpamAssassin flags it as NO_REAL_NAME because it's looking for a real name with the e-mail address... Instead of passing user@host.com, use "My Name <user@host.com>". This will stop that particular problem.
Also, if you send messages all in HTML, get familiar with Mail/Mime in pear and strip_tags to send a text message identical to the HTML version. This takes care of some other SA problems, and makes it easier for people to read from non-gui mailreaders.
And on a final note, to keep Outlook users from automatically marking your messages as Junk Mail, send a message from Outlook to yourself, look at the headers, and make sure you send as many of the same ones as you can. If outlook thinks your message was sent from another outlook client, it'll score higher on the junk filter.
accius [ at] hotmail [dot ] com
16-Oct-2007 03:28
I found out the solution for my problem with spamassassin. The sender name and address should be separed with space :
$headers .= "From: ".$fromname." <".$fromaddress.">".$eol;
or
$from = $fromname." <".$fromaddress.">";
ini_set("sendmail_from",$from)
perryv at gmail dot com
13-Oct-2007 10:52
The ini_set('sendmail_from', $your_from_addr) solution only works when the PHP mailer script is running under Windows. To do a similar solution for *nix OS's, pass the string "-r $your_from_addr" to mail() as the fifth parameter.
accius [at] hotmail [dot] com
12-Oct-2007 02:08
One small error in nielsvan den berge's code:
ini_set(sendmail_from,$fromaddress) should be written ini_set("sendmail_from",$fromaddress) and ini_restore(sendmail_from) : ini_restore("sendmail_from")
Even with the "sendmail_from" set, SpamAssassin tags the message with NO_REAL_NAME, I didn't find out he solution yet
contact at lattery dot com
01-Oct-2007 03:44
Brian, in your script text simply add a "\n" to the end of each line of text you want separated. Add two "\n" for a space break between lines. For example:
$message .= "First Name: $firstname\n";
$message .= "Last name: $lastname\n";
will send each of these to your email on separate lines.
peekepichotmailcom
24-Sep-2007 01:47
Hi all, i recently update my server to another more powerful and since some pop3 servers like "gmail" display my emails like an HTML code :(
After one night, i've found the bug and some info on internet :
"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".
I replace my CRLF separator by LF onyl and all my users received a correct email. PeekEpic.
eflores at sterlingandbaxter dot com
18-Sep-2007 10:29
I need help. When I send the email the attachments and the multiparts are displayed as text. What am I doing wrong? I have tried the double eols and still have no clue. Here is my code.
<?php
$txt="The report contains locations on the ground for at least 16 weeks and no more than 17 weeks. A summary of last 3 weeks productivity and its average is included. Size".strlen($data);
$mime_boundary = "<<<--==+X[".md5(time())."]\r\n\r\n";
$message = "This is a multi-part message in MIME format.\r\n\r\n".
"--".$mime_boundary."\r\n".
"Content-Type: text/plain; charset=\"iso-8859-1\"\r\n".
"Content-Transfer-Encoding: 7bit\r\n".
$txt."\r\n\r\n".
"--".$mime_boundary."\r\n";
$data="Dummy Info";
$data = chunk_split(base64_encode($data));
$fileatt_type="application/vnd.ms-excel;";
$fileatt_name="startupLocations.xls";
$message .= "Content-Type: ".$fileatt_type."\r\n" .
" name=\"".$fileatt_name."\"\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-Disposition: attachment;\r\n" .
" filename=\"".$fileatt_name."\"\r\n\r\n" .
$data."\r\n" .
"--".$mime_boundary."\r\n";
$reportName='Comissions';
unset($sendTo);
$sql = "SELECT * FROM distributionlist left join autoemailtabs
on distributionlist.reporttabid=autoemailtabs.id
WHERE tabname='".$reportName."';";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$sendTo[] = $row['email'];
}
for($j=0;$j<sizeof($sendTo);$j++){
$headers = "From:eaflores@sterlingandbaxter.com\r\n".
"To:".$sendTo[$j]."\r\n".
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n".
"boundary=\"".$mime_boundary."\"\r\n";
mail($sendTo[$j],' STS Comissions ',$message,$headers);
}?>
nielsvandenberge at hotm dot dot dot dot dot com
04-Sep-2007 12:30
In the post I made about the send_mail() function at 28-Aug-2007 05:15 is a small problem.
the text:
<?php
$headers .= 'MIME-Version: 1.0'.$eol.$eol;
?>
should be replaced by
<?php
$headers .= 'MIME-Version: 1.0'.$eol;
?>
With a double "end of line" you close the header of a block so then the body should start. The Body also ends with two eol's
An e-mail is build up with some blocks, and those blocks can also contain blocks. Every block has a header where is defined what kind of block it is (content-Type). This content can be the texts and attachments for example.
The blocks are separated with a boundary, which is also defined in the header of a block (always starts with two dashes). After the header of a block you should put two "end of lines" (eol) and a boundary. The last block ends with a boundary plus two dashes (so it looks like "--BOUNDARY--") and 2 eol's
When a block contains other blocks (like we did with the HTML/Plain text part), the blocks should be separated by a different boundary then we used before.
So an email will look like:
Mail Header [define boundary "--123"]
2 eol
--123
header block 1 [define boundary "--456"]
2eol
--456
header block 1.1
2eol
content block 1.1
2eol
--456
header block 1.2
2eol
content block 1.2
2eol
--456--
2eol
--123
header block 2
2eol
content block 2
2eol
--123--
2eol
Have fun with it
webmaster at weethet dot nl
31-Aug-2007 07:35
As mentioned earlier, for Windows users there is a fake sendmail option. A bit more detailed description how to do this is:
If you have a test server in use running Windows and some kind of WAMP combo (XXAMP, WAMP Server, etc) then you'll notice that the PHP sendmail command (mail()) does not work. Windows simply does not provide the sendmail statement ...
There is a simple trick to get this to work though;
1) Download (or use the attached file) sendmail.zip from http://glob.com.au/sendmail/
2) Unzip this in a folder on your c: drive (preferably use a simple path, for example c:\wamp\sendmail -- long filenames could cause problems)
3) Edit your PHP.INI file (note: WAMP users should access their php.ini file from the WAMP menu). Go to the [mail function] section and modify it as such:
[mail function]
; For Win32 only.
;SMTP =
; For Win32 only.
;sendmail_from =
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "C:\wamp\sendmail\sendmail.exe -t"
; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_paramaters =
.. and save the changes.
4) Open the sendmail.ini and modify the settings to:
[sendmail]
; you must change mail.mydomain.com to your smtp server,
; or to IIS's "pickup" directory. (generally C:\Inetpub\mailroot\Pickup)
; emails delivered via IIS's pickup directory cause sendmail to
; run quicker, but you won't get error messages back to the calling
; application.
smtp_server=mail.yourdomain.com
; smtp port (normally 25)
smtp_port=25
; the default domain for this server will be read from the registry
; this will be appended to email addresses when one isn't provided
; if you want to override the value in the registry, uncomment and modify
default_domain=yourdomain.com
; log smtp errors to error.log (defaults to same directory as sendmail.exe)
; uncomment to enable logging
; error_logfile=sendmail_error.log
; create debug log as debug.log (defaults to same directory as sendmail.exe)
; uncomment to enable debugging
; debug_logfile=sendmail_debug.log
; if your smtp server requires authentication, modify the following two lines
;auth_username=
;auth_password=
; if your smtp server uses pop3 before smtp authentication, modify the
; following three lines
pop3_server=mail.yourdomain.com
pop3_username=you@yourdomain.com
pop3_password=mysecretpassword
; to force the sender to always be the following email address, uncomment and
; populate with a valid email address. this will only affect the "MAIL FROM"
; command, it won't modify the "From: " header of the message content
force_sender=you@yourdomain.com
; sendmail will use your hostname and your default_domain in the ehlo/helo
; smtp greeting. you can manually set the ehlo/helo name if required
hostname=
The optional error and debug logging is recommended when trying this the first time, so you have a clue what goes wrong in case it doesn't work.
Force_sender is also optional, but recommended to avoid confusion on the server end.
Obviously mail.yourdomain.com, you@yourdomain.com, and mysecretpassword should be the relevant info for your SMTP server.
Now restart the WAMP services (mainly Apache so PHP re-reads it's config).
Now you're good to go and use the PHP mail() statement as if you're a Unix user ...
nielsvan den berge at hotmail dot com
28-Aug-2007 08:15
I updated the function with help of some comments on this page and some research on the internet and this is the final result.
I tested it with MS Outlook, MS Outlook Express, Mozilla Thunderbird, Yahoo webmail, Hotmail, MS Exchange Webaccess and Horde (text-version) and they all gave the desired output with and without attachments.
Spamassassin also accepts this form of sending e-mails, when you use a clean e-mailaddress and body.
<?php
function send_mail($to, $body, $subject, $fromaddress, $fromname, $attachments=false)
{
$eol="\r\n";
$mime_boundary=md5(time());
$headers .= "From: ".$fromname."<".$fromaddress.">".$eol;
$headers .= "Reply-To: ".$fromname."<".$fromaddress.">".$eol;
$headers .= "Return-Path: ".$fromname."<".$fromaddress.">".$eol; $headers .= "Message-ID: <".time()."-".$fromaddress.">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol; $headers .= 'MIME-Version: 1.0'.$eol.$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"".$eol.$eol;
$msg = "--".$mime_boundary.$eol;
$htmlalt_mime_boundary = $mime_boundary."_htmlalt"; $msg .= "Content-Type: multipart/alternative; boundary=\"".$htmlalt_mime_boundary."\"".$eol.$eol;
$msg .= "--".$htmlalt_mime_boundary.$eol;
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$msg .= strip_tags(str_replace("<br>", "\n", substr($body, (strpos($body, "<body>")+6)))).$eol.$eol;
$msg .= "--".$htmlalt_mime_boundary.$eol;
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$msg .= $body.$eol.$eol;
$msg .= "--".$htmlalt_mime_boundary."--".$eol.$eol;
if ($attachments !== false)
{
for($i=0; $i < count($attachments); $i++)
{
if (is_file($attachments[$i]["file"]))
{
$file_name = substr($attachments[$i]["file"], (strrpos($attachments[$i]["file"], "/")+1));
$handle=fopen($attachments[$i]["file"], 'rb');
$f_contents=fread($handle, filesize($attachments[$i]["file"]));
$f_contents=chunk_split(base64_encode($f_contents)); $f_type=filetype($attachments[$i]["file"]);
fclose($handle);
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: ".$attachments[$i]["content_type"]."; name=\"".$file_name."\"".$eol; $msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Description: ".$file_name.$eol;
$msg .= "Content-Disposition: attachment; filename=\"".$file_name."\"".$eol.$eol; $msg .= $f_contents.$eol.$eol;
}
}
}
$msg .= "--".$mime_boundary."--".$eol.$eol; ini_set(sendmail_from,$fromaddress); $mail_sent = mail($to, $subject, $msg, $headers);
ini_restore(sendmail_from);
return $mail_sent;
}
?>
timo dot witte at kwick dot de
20-Aug-2007 05:57
if you wonder how to encode an subject in UTF-8 do it like this:
<?php
mail("timo.witte@kwick.de", "=?UTF-8?B?".base64_encode("also
|
|