mail

(PHP 4, PHP 5, PHP 7, PHP 8)

mailEnvia e-mail

Descrição

mail(
    string $to,
    string $subject,
    string $message,
    array|string $additional_headers = [],
    string $additional_params = ""
): bool

Envia um e-mail.

Parâmetros

to

Destinatário, ou destinatários do e-mail.

O formato desta string precisa estar de acordo com a » RFC 2822. Alguns exemplos:

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

subject

Assunto do e-mail a ser enviado.

Cuidado

Assunto deve satisfazer a » RFC 2047.

message

Mensagem a ser enviada.

Cada linha deve ser separada com um CRLF (\r\n). Linhas não devem ser maiores que 70 caracteres.

Cuidado

(Somente Windows) Quando o PHP está se comunicando com o servidor SMTP diretamente, e um ponto final é encontrado no início de uma linha, ela é removida. Para se defender disto, substitua estas ocorrência por dois pontos seguidos.

<?php
$text
= str_replace("\n.", "\n..", $text);
?>

additional_headers (opcional)

String ou array a ser inserido no final do cabeçalho do e-mail.

Isto é tipicamente usado para adicionar cabeçalhos extras (From, Cc, e Bcc). Múltiplos cabeçalhos extras devem ser separados com um CRLF (\r\n). Se dados externos forem usados para compor este cabeçalho, os dados devem ser sanitizados para que nenhum cabeçalho indesejado possa ser injetado.

Se um array for passado, suas chaves são os nomes dos cabeçalhos e seus valores são os respectivos valores dos cabeçalhos.

Nota:

Ao enviar e-mail, ele precisa conter um cabeçalho From. Este pode ser definido com o parâmetro additional_headers, ou um padrão pode ser definido no php.ini.

Se isto não foi feito, ocorrerá uma mensagem de erro similar a Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. O cabeçalho From define também o Return-Path ao enviar diretamente via SMTP (Windows somente).

Nota:

Se mensagens não estão sendo recebidas, tente usar somente um LF (\n). Alguns agentes Unix de transferência de e-mail (mais notavelmente » qmail) substituem LF por CRLF automaticamente (que leva à duplicação de CR se CRLF for usado). Este deve ser um último recurso, porque não está conforme a » RFC 2822.

additional_params (opcional)

O parâmetro additional_params pode ser usado para passar um parâmetro adicional para o programa configurado para usa quando enviando e-mail usando a configuração sendmail_path. Por exemplo, isto pode ser usado para definir o endereço do envelope remetente quando usando sendmail com a opção do sendmail -f.

Este parâmetro é escapado por escapeshellcmd() internamente para prevenir a execução do comando. escapeshellcmd() impede a execução de comandos, mas permite adicionar parâmetros adicionais. Por razões de segurança, é recomendado que o usuário higienize este parâmetro para evitar adicionar parâmetros indesejados ao comando shell.

Uma vez que escapeshellcmd() é aplicada automaticamente, alguns caracteres que são permitidos como endereços de e-mail por RFCs da Internet não podem ser usados. mail() não pode permitir tais caracteres, portanto, em programas onde o uso de tais caracteres é necessário, meios alternativos de envio de e-mails (como o uso de um framework ou biblioteca) são recomendados.

O usuário com o qual o servidor web roda deve ser adicionado como um usuário confiável à configuração do sendmail para evitar que um cabeçalho 'X-Warning' seja adicionado à mensagem quando o remetente do envelope (-f) é definido usando este método. Para usuários do sendmail, este arquivo é /etc/mail/trusted-users.

Valor Retornado

Retorna true se o e-mail foi aceito com sucesso para entrega, false caso contrário.

É importante notar que somente pelo fato do e-mail ter sido aceito para entrega, NÃO significa que o e-mail realmente alcancará o destino esperado.

Registro de Alterações

Versão Descrição
7.2.0 O parâmetro additional_headers agora aceita um array.

Exemplos

Exemplo #1 Enviando e-mail.

Usando mail() para enviar um simples e-mail:

<?php
// A mensagem
$message = "Line 1\r\nLine 2\r\nLine 3";

// Se as linhas forem maiores que 70 caracteres, deve-se usar wordwrap()
$message = wordwrap($message, 70, "\r\n");

// Envia
mail('caffeinated@example.com', 'Meu Assunto', $message);
?>

Exemplo #2 Enviando e-mail com cabeçalhos extras.

A adição de cabeçalhos básicos, informando à MUA os endereços From e Reply-To:

<?php
$to
= 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

Exemplo #3 Enviando e-mail com cabeçalhos extras no formato array

Este exemplo envia o mesmo e-mail que o exemplo imediatamente acima, mas passa os cabeçalhos adicionais como array (disponível a partir do PHP 7.2.0).

<?php
$to
= 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = array(
'From' => 'webmaster@example.com',
'Reply-To' => 'webmaster@example.com',
'X-Mailer' => 'PHP/' . phpversion()
);

mail($to, $subject, $message, $headers);
?>

Exemplo #4 Enviando e-mail com um parâmetro de linha de comando adicional.

O parâmetro additional_params pode ser usado para passar um parâmetro adicional para o programa configurado para usar ao enviar e-mail usando o sendmail_path.

<?php
mail
('nobody@example.com', 'the subject', 'the message', null,
'-fwebmaster@example.com');
?>

Exemplo #5 Enviando e-mail HTML

É também possível enviar e-mail HTML com mail().

<?php
// Múltiplos recipientes
$to = 'joao@example.com, susana@example.com'; // observe a vírgula

// Assunto
$subject = 'Lembrete de Aniversários para Novembro';

// Message
$message = '
<html>
<head>
<title>Lembrete de Aniversários para Novembro</title>
</head>
<body>
<p>Aqui estão os próximos aniversariantes de novembro!</p>
<table>
<tr>
<th>Pessoa</th><th>Dia</th><th>Mês</th><th>Ano</th>
</tr>
<tr>
<td>João</td><td>10</td><td>Agosto</td><td>1990</td>
</tr>
<tr>
<td>Susana</td><td>17</td><td>Agosto</td><td>2003</td>
</tr>
</table>
</body>
</html>
'
;

// Para enviar e-mail HTML, o cabeçalho Content-type deve ser definido
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';

// Cabeçalhos adicionais
$headers[] = 'To: Maria <maria@example.com>, Rafael <rafael@example.com>';
$headers[] = 'From: Lembrete de Aniversário <niver@example.com>';
$headers[] = 'Cc: arquivo_niver@example.com';
$headers[] = 'Bcc: verifica_niver@example.com';

// Envia o e-mail
mail($to, $subject, $message, implode("\r\n", $headers));
?>

Nota:

Se a intenção é enviar e-mail HTML ou outro formato complexo, é recomendado usar o pacote PEAR » PEAR::Mail_Mime.

Notas

Nota:

A implentação SMTP (somente Windows) de mail() difere bastante da implentação do sendmail. Primeiro, ele não usa um binário local para compor mensagens mas apenas opera com sockets diretos, o que significa que uma MTA é necessária monitorando um socket de rede (que pode ser ou o localhost ou uma máquina remota).

Segundo, cabeçalhos customizados como From:, Cc:, Bcc: e Date: não são interpretados pela MTA, mas são analisados pelo PHP.

Desta forma, o parâmetro to não deve ser um endereço na forma de "Algo <alguem@example.com>". O comando de e-mail poderia não analisar isto adequadamente enquanto se comunica com o MTA.

Nota:

É válido notar que a função mail() não é apropriada para um grande volume de e-mail em um loop. Esta função abre e fecha um SMTP socket para cada e-mail, o que não é muito eficiente.

Para enviar uma grande quantidade de e-mail, veja os pacotes » PEAR::Mail, e » PEAR::Mail_Queue.

Nota:

O seguintes RFCs podem ser úteis: » RFC 1896, » RFC 2045, » RFC 2046, » RFC 2047, » RFC 2048, » RFC 2049 e » RFC 2822.

Veja Também

add a note add a note

User Contributed Notes 39 notes

up
21
Anonymous
4 years ago
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:

<?php
$headers
= 'Content-Type: text/plain; charset=utf-8' . "\r\n";
?>

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:

<?php
$headers
.= 'Content-Transfer-Encoding: base64' . "\r\n";
?>

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

<?php
$subject
= '=?UTF-8?B?' . base64_encode('Test email with German Umlauts öäüß') . '?=';
$recipient = '=?UTF-8?B?' . base64_encode('Margret Müller') . '?= <recipient@domain.com>';
?>

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

<?php
$message
= base64_encode('This email contains German Umlauts öäüß.');
?>

All references are taken from:
https://dev.to/lutvit/how-to-make-the-php-mail-function-awesome-3cii
up
10
Mark Simon
4 years ago
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.
up
19
php at simoneast dot net
6 years ago
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.

<?php
$success
= mail('example@example.com', 'My Subject', $message);
if (!
$success) {
   
$errorMessage = error_get_last()['message'];
}
?>

(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
up
16
Anonymous
7 years ago
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.
up
10
chris at ocproducts dot com
6 years ago
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.
up
7
charles dot fisher at arconic dot com
6 years ago
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);
}
up
8
ABOMB
12 years ago
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...
up
5
Ben Cooke
18 years ago
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.
up
5
pavel.lint at vk.com
11 years ago
Here's a small handy function I use to send email in UTF-8.

<?php
function mail_utf8($to, $from_user, $from_email,
                                            
$subject = '(No subject)', $message = '')
   {
     
$from_user = "=?UTF-8?B?".base64_encode($from_user)."?=";
     
$subject = "=?UTF-8?B?".base64_encode($subject)."?=";

     
$headers = "From: $from_user <$from_email>\r\n".
              
"MIME-Version: 1.0" . "\r\n" .
              
"Content-type: text/html; charset=UTF-8" . "\r\n";

     return
mail($to, $subject, $message, $headers);
   }
?>
up
5
Porjo
13 years ago
Make sure you enclose \r\n in double quotes (not single quotes!) so that PHP can translate that into the correct linefeed code
up
1
atesin () gmial ! com
2 years ago
HOW PHP MAIL() WORKS INTERNALLY, more or less, according some tests i did

imagine php is running with uid 33 when reaches a mail() function like this

---- /my/htdocs/mailer-script.php line 69 ----
$was_sent = mail(
  'Alice <alice@wonder.land>, white-rabbit@endless.cave',
  'Invitation',
  "Hi, you are cordially invited to drink a cup of tea at my house\ntomorrow at sunset",
  "Some-Header: foo\r\nOther-Header: bar", // can also be an associative array of 'Header' => 'Value'
  'param1 param2');
----

then look for ini options like this (all optional, on any operating system)

---- ini options ----
; path to mail sender command + arguments, if omitted will defaults to NULL
sendmail_path = '/bin/sendmail-esque arg1 arg2'
; the From header, can be also set in sendmail but make sure it SENDS ONE
sendmail_from = 'Mad Hatter <hatter@house.forest>'
; recommended to not being blind, look at file permissions
mail.log = '/var/log/php/mail.log'
; adds an originating script header to send php mail internals (security sensitive)
mail.add_x_header = on
; outgoing mail server address, if omitted will defaults to "localhost"
;SMTP = 'localhost'
; server tcp port, if omitted will defaults to port 25
;smtp_port = 25
----

then craft a message like this (please avoid duplicated headers)

---- mail message ----
From: Mad Hatter <hatter@house.forest>
To: Alice <alice@wonder.land>, white-rabbit@endless.cave
Subject: Invitation
Some-Header: foo
Other-Header: bar
X-PHP-Originating-Script: 33:mailer-script.php

Hi, you are cordially invited to drink a cup of tea at my house
tomorrow at sunset
----

then APPEND this log line

---- /var/log/php/mail.log ----
[<date> <time> <zone>] mail() on [/my/htdocs/mailer-script.php:69]: <all headers joined...>
----

THEN IF (sendmail_path is not empty, AND file is found, AND has +rx permissions):

- craft and run this shell command

---- shell ----
/bin/sendmail-esque arg1 arg2 param1 param2
----

- send the crafted message to its standard input
- wait the program to exit
- pass last exit code to mail() return

OR ELSE (i.e. sendmail bin not run) (not sure about these):

- try to internally open an smtp connection (with fsockopen()?)
- send the message directly and wait for connection to close
- pass last socket status to mail() return
up
-1
eeeugeneee
6 years ago
Send mail with minimal requirements from email services.

<?php
    $encoding
= "utf-8";

   
// Preferences for Subject field
   
$subject_preferences = array(
       
"input-charset" => $encoding,
       
"output-charset" => $encoding,
       
"line-length" => 76,
       
"line-break-chars" => "\r\n"
   
);

   
// Mail header
   
$header = "Content-type: text/html; charset=".$encoding." \r\n";
   
$header .= "From: ".$from_name." <".$from_mail."> \r\n";
   
$header .= "MIME-Version: 1.0 \r\n";
   
$header .= "Content-Transfer-Encoding: 8bit \r\n";
   
$header .= "Date: ".date("r (T)")." \r\n";
   
$header .= iconv_mime_encode("Subject", $mail_subject, $subject_preferences);

   
// Send mail
   
mail($mail_to, $mail_subject, $mail_message, $header);
?>
up
-1
Max AT
12 years ago
To define a mail sensitivity you have to put this line in the headers:

<?php
        $headers
= "MIME-Version: 1.0\n" ;
       
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";

       
$headers .= "Sensitivity: Personal\n";

$status   = mail($to, $subject, $message,$headers);
?>

Possible Options:
Sensitivity: Normal, Personal, Private and Company-Confidential

These will be recognised and handled in Outlook, Thunderbird and others.
up
-1
pangz dot lab at gmail dot com
3 years ago
* Sending email with attachment

function sendMail(
    string $fileAttachment,
    string $mailMessage = MAIL_CONF["mailMessage"],
    string $subject     = MAIL_CONF["subject"],
    string $toAddress   = MAIL_CONF["toAddress"],
    string $fromMail    = MAIL_CONF["fromMail"]
): bool {
   
    $fileAttachment = trim($fileAttachment);
    $from           = $fromMail;
    $pathInfo       = pathinfo($fileAttachment);
    $attchmentName  = "attachment_".date("YmdHms").(
    (isset($pathInfo['extension']))? ".".$pathInfo['extension'] : ""
    );
   
    $attachment    = chunk_split(base64_encode(file_get_contents($fileAttachment)));
    $boundary      = "PHP-mixed-".md5(time());
    $boundWithPre  = "\n--".$boundary;
   
    $headers   = "From: $from";
    $headers  .= "\nReply-To: $from";
    $headers  .= "\nContent-Type: multipart/mixed; boundary=\"".$boundary."\"";
   
    $message   = $boundWithPre;
    $message  .= "\n Content-Type: text/plain; charset=UTF-8\n";
    $message  .= "\n $mailMessage";
   
    $message .= $boundWithPre;
    $message .= "\nContent-Type: application/octet-stream; name=\"".$attchmentName."\"";
    $message .= "\nContent-Transfer-Encoding: base64\n";
    $message .= "\nContent-Disposition: attachment\n";
    $message .= $attachment;
    $message .= $boundWithPre."--";
   
    return mail($toAddress, $subject, $message, $headers);
}

* Sending email in html

function sendHtmlMail(
    string $mailMessage = MAIL_CONF["mailMessage"],
    string $subject     = MAIL_CONF["subject"],
    array $toAddress    = MAIL_CONF["toAddress"],
    string $fromMail    = MAIL_CONF["fromMail"]
): bool {
   
    $to        = implode(",", $toAddress);
    $headers[] = 'MIME-Version: 1.0';
    $headers[] = 'Content-type: text/html; charset=iso-8859-1';   
    $headers[] = 'To: '.$to;
    $headers[] = 'From: '.$fromMail;   

    return mail($to, $subject, $mailMessage, implode("\r\n", $headers));
}
up
-2
rexlorenzo at gmail dot com
11 years ago
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.
up
-2
jim at jimbrooking dot net
8 years ago
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.
up
-2
chris at ocproducts dot com
6 years ago
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.
up
-4
debis at woh dot rr dot com
12 years ago
This is for Windows Server 2003, IIS 6.0 with SMTP virtual server.

The problem I had was not including init_set for the SMTP server, I thought the SMTP definition in the IIS SMTP virtual server configuration would work.  When I sent mail manually this was not an issue. 

Also, $mail_sent = @mail( $to, $subject, $message, $headers ); wouldn't work but $mail_sent = mail($to, $subject, $message, $headers); did.
Lack of date_default_timezone_set() only caused a warning because php guessed what it should be.

This worked:
<?php
$to     
= 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
   
'Reply-To: webmaster@example.com' . "\r\n" .
   
'X-Mailer: PHP/' . phpversion();
ini_set ( "SMTP", "smtp-server.example.com" );
date_default_timezone_set('America/New_York');

mail($to, $subject, $message, $headers);
?>

And just so you can troubleshoot, this worked when sending mail from the command line/manually. CLI worked even though the php code without the init_set function wouldn't work.

You will notice that the "rcpt to" and "to" fields appear redundant, but if both are not used, the delivered mail's "to" field will be blank/empty.

-------------------------------
telnet www.example.com 25
helo
mail from: webmaster@example.com
rcpt to: someone@example.com
data
to: someone@example.com
subject: test again to make sure

this is my message
.
quit
-----------------------------------
up
-8
jimmytrojan009 at gmail dot com
8 years ago
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"
up
-5
richard at richard-sumilang dot com
16 years ago
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
up
-5
bburch at bouncingpixel dot com
11 years ago
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.
up
-8
Edward
14 years ago
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:

<?php
mail
($to, $subject, $message, $headers, "-femail.address@example.com");
?>
up
-7
msheldon at desertraven dot com
18 years ago
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 <CRLF>."

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 <CRLF> is 1000 characters" (RFC 821)
up
-6
f dot touchard at laposte dot net
21 years ago
***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

<?php
function qp_encoding($Message) {
   
   
/* Build (most polpular) Extended ASCII Char/Hex MAP (characters >127 & <255) */
   
for ($i=0; $i<127; $i++) {
       
$CharList[$i] = "/".chr($i+128)."/";
       
$HexList[$i] = "=".strtoupper(bin2hex(chr($i+128)));
    }

   
/* Encode equal sign & 8-bit characters as equal signs followed by their hexadecimal values */
   
$Message = str_replace("=", "=3D", $Message);
   
$Message = preg_replace($CharList, $HexList, $Message);

   
/* Lines longer than 76 characters (size limit for quoted-printable Content-Transfer-Encoding)
        will be cut after character 75 and an equals sign is appended to these lines. */
   
$MessageLines = split("\n", $Message);
   
$Message_qp = "";
    while(list(,
$Line) = each($MessageLines)) {
        if (
strlen($Line) > 75) {
           
$Pointer = 0;       
            while (
$Pointer <= strlen($Line)) {
               
$Offset = 0;
                if (
preg_match("/^=(3D|([8-9A-F]{1}[0-9A-F]{1}))$/", substr($Line, ($Pointer+73), 3))) $Offset=-2;
                if (
preg_match("/^=(3D|([8-9A-F]{1}[0-9A-F]{1}))$/", substr($Line, ($Pointer+74), 3))) $Offset=-1;
               
$Message_qp.= substr($Line, $Pointer, (75+$Offset))."=\n";
                if ((
strlen($Line) - ($Pointer+75)) <= 75) {               
                   
$Message_qp.= substr($Line, ($Pointer+75+$Offset))."\n";
                    break
1;
                }
               
$Pointer+= 75+$Offset;
            }
        } else {
           
$Message_qp.= $Line."\n";
        }
    }       
    return
$Message_qp;
}
?>
up
-7
molotster on google mail com
15 years ago
Note, that single line should not contain more than 78 character, but is not allowed to contain more than 998 characters.

The possible consequences are:
Over 78 - clients are allowed to display the message in a "harder to read" way.
Over 998 - clients and servers are allowed to drop the message or cause any buffer-limit error.

See:
http://www.faqs.org/rfcs/rfc2822 part 2.1.1.
up
-6
ittasks at gmail dot com
11 years ago
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"
up
-8
Erich at gasboysnospam dot net
15 years ago
if your mail is failing (returns false) be aware that many servers are configured to kill mail going out with a bcc or cc header.

The ideal workaround is to use the smtp functions which servers allow because of its better audit trail. Alternatively call the mail function several times.

I've just spent about four hours trying to work out what I was doing wrong!!
up
-8
Alex Jaspersen
16 years ago
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.

<?php
function qmail_queue($to, $from, $subject, $message, $additional_headers = "")
{
   
// qmail-queue location and hostname used for Message-Id
   
$cmd = "/var/qmail/bin/qmail-queue";
   
$hostname = trim(file_get_contents("/var/qmail/control/me"));
   
   
// convert $to into an array
   
if(is_scalar($to))
       
$to = array($to);
   
   
// BEGIN VALIDATION
    // e-mail address validation
   
$e = "/^[-+\\.0-9=a-z_]+@([-0-9a-z]+\\.)+([0-9a-z]){2,4}$/i";
   
// from address
   
if(!preg_match($e, $from)) return false;
   
// to address(es)
   
foreach($to as $rcpt)
    {
        if(!
preg_match($e, $rcpt)) return false;
    }
   
   
// subject validation (only printable 7-bit ascii characters allowed)
    // needs to be adapted to allow for foreign languages with 8-bit characters
   
if(!preg_match("/^[\\040-\\176]+$/", $subject)) return false;
   
   
// END VALIDATION
   
    // open qmail-queue process
   
$dspec = array
    (
        array(
"pipe", "r"), // message descriptor
       
array("pipe", "r") // envelope descriptor
   
);
   
$pipes = array();
   
$proc = proc_open($cmd, $dspec, $pipes);
    if(!
is_resource($proc)) return false;
   
   
// write additional headers
   
if(!empty($additional_headers))
    {
       
fwrite($pipes[0], $additional_headers . "\n");
    }
   
   
// write to/from/subject/date/message-ID headers
   
fwrite($pipes[0], "To: " . $to[0]); // first recipient
   
for($i = 1; $i < sizeof($to); $i++) // additional recipients
   
{
       
fwrite($pipes[0], ", " . $to[$i]);
    }
   
fwrite($pipes[0], "\nSubject: " . $subject . "\n");
   
fwrite($pipes[0], "From: " . $from . "\n");
   
fwrite($pipes[0], "Message-Id: <" . md5(uniqid(microtime())) . "@" . $hostname . ">\n");
   
fwrite($pipes[0], "Date: " . date("r") . "\n\n");
   
fwrite($pipes[0], $message);
   
fwrite($pipes[0], "\n");
   
fclose($pipes[0]);
   
   
// write from address and recipients
   
fwrite($pipes[1], "F" . $from . "\0");
    foreach(
$to as $rcpt)
    {
       
fwrite($pipes[1], "T" . $rcpt . "\0");
    }
   
fwrite($pipes[1], "\0");
   
fclose($pipes[1]);
   
   
// return true on success.
   
return proc_close($proc) == 0;
}
?>
up
-7
Systemx
14 years ago
Bare LFs in SMTP

Use This

<?php
// Fix any bare linefeeds in the message to make it RFC821 Compliant.
$message = preg_replace("#(?<!\r)\n#si", "\r\n", $message);
   
// Make sure there are no bare linefeeds in the headers
$headers = preg_replace('#(?<!\r)\n#si', "\r\n", $headers);
?>
up
-6
Paul
20 years ago
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 :)
up
-8
martin dot farrow at versacloud dot com
11 years ago
I've noticed that on some versions of PHP occasionally mail() returns the empty string for success, rather than true or false. The empty string evaluates to false.

if you use constructs like

if ( mail( ... ) ){
  # do something here on success
}

this wont work consistently.

so you need code like

$ret=mail(....)

if ( $ret == '' || $ret ){
  # do something here
}

to get consistent results.
up
-2
John dot php at yaph dot org dot uk
3 years ago
I found I needed to use the additional parameter -f$envelope_sender to send subscribe messages to my email list manager (Enemies of Carlotta, YMMV). Simply setting From: email@address.tld in additional headers wasn't sufficient.

So to subscribe to mylist@example.com via a PHP script I call
mail("mylist-subscribe@example.com", $emailSubject, $body, '', "-f$email_to_subscribe");
up
-10
php dot net at schrecktech dot com
19 years ago
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...
up
-3
marcrbarker at NOSPAMgmail dot com
3 years ago
DKIM signature can break by trying to add <HTML tag> text effects where plain text is expected:

.
.
.
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-Type: text/html; utf-8';
$headers[] = 'Content-Transfer-Encoding: quoted-printable';

// *** This will break DKIM ***
$message = '<span style="color:green;font-size:24px;">ThisIsColored</span>'
// *** This had broke DKIM ***

mail($email, $subject, $message, implode("\r\n", $headers));
.
.
.
up
-12
jerold at pangzlab dot com
4 years ago
Sending mail with attachments

function sendMail(
    $mailTo,
    $message,
    $subject    = "Your Subject",
    $fromMail   = "your@emialaddress.com",
    $fromName   = "from sender",
    $replyTo    = "no-reply",
    $filePath   = "path"
)
{
    $LE  = "\r\n";
    $uid = md5(uniqid(time()));
    $withAttachment = ($filePath !== NULL && file_exists($filePath));

    if($withAttachment){
        $fileName   = basename($filePath);
        $fileSize   = filesize($filePath);
        $handle     = fopen($filePath, "r");
        $content    = fread($handle, $fileSize);
        fclose($handle);
        $content = chunk_split(base64_encode($content));
    }

    $header = "From: ".$fromName." <".$fromMail.">$LE";
    $header .= "Reply-To: ".$replyTo."$LE";
    $header .= "MIME-Version: 1.0$LE";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"$LE$LE";
    $header .= "This is a multi-part message in MIME format.$LE";
    $header .= "--".$uid."$LE";
    $header .= "Content-type:text/html; charset=UTF-8$LE";
    $header .= "Content-Transfer-Encoding: 7bit$LE$LE";
    $header .= $message."$LE$LE";

    if($withAttachment){
        $header .= "--".$uid."$LE";
        $header .= "Content-Type: application/octet-stream; name=\"".$fileName."\"$LE";
        $header .= "Content-Transfer-Encoding: base64$LE";
        $header .= "Content-Disposition: attachment; filename=\"".$fileName."\"$LE$LE";
        $header .= $content."$LE$LE";
        $header .= "--".$uid."--";
    }
    return mail($mailTo, $subject, "", $header);
}
up
-11
umangberi at gmail dot com
13 years ago
Outlook 2007 seemed to be a little finicky with me to have carriage returns in the headers. So any \r\n resulted in messages that had default apache messages sent over to me.

As soon as I removed \r from all of the headers, the script started working fine. Hope that helped.
up
-17
yarik dot bohatsky at gmail dot com
12 years ago
If you want to send UTF-8 HTML letter you need to mention charset twice:

1) In message header:
<?php
$headers
.= 'Content-type: text/html; charset=utf-8' . "\r\n";
?>

2) In HTML header:
<?php
$message
= '
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Fillon soutient à fond le retour d\'un Grand Prix de France</title>
</head>
<body>
   <p>Le Premier ministre François Fillon, passionné d\'automobile et pilote à ses heures, a apporté un soutien appuyé au retour d\'un Grand Prix de France au calendrier 2013 de la Formule 1, en faisant un passage-éclair vendredi sur le circuit Paul Ricard dans le Var.</p>
</body>
</html>
'
;

In this case Outlook will also "understand" that message is encoded using UTF-8.
up
-13
shuitest at gmail dot com
13 years ago
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]}' </dev/null 2>&1;
up
-21
shenyqwilliam
11 years ago
If you're sending a large attachment, you may encounter overflow problem.
AFAIK, two common limits could be responsible.

1. Postfix message size limit.
Edit /etc/postfix/main.cf . Change the value of "message_size_limit".

2. Apache memory size limit for scripts.
Edit /etc/php.ini . Change the value of "memory_limit".

//Then reload (or restart) Postfix and Apache.
//Empirically, sending 200MB attachment requires 500MB memory.

Be careful! Raising memory limits may cause unexpected consequences, and is hence deprecated.
Recommended alternatives include:
* Pack and split attachment into several emails.
* Only include a link to the file. The receiver can download it later.
* Use IMAP/POP3 server (e.g. Dovecot).
To Top