-1

I try to send email with a SMTP server. Is it possible to send message with mail() function? Or must i to use PHPMailer or Pear?

$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";
mail($to,$subject,$txt,$headers);

Is it possible like:

$headers .= "Host: myhost.example.com\r\n";
$headers .= "User: myusername\r\n";
$headers .= "Password: mypassword\r\n";

? I'm looking for possible simple versions.

user2301515
  • 4,903
  • 6
  • 30
  • 46

3 Answers3

0

On Windows you can specify a server to use for SMTP in php.ini, but not on *nix where sendmail or something sendmail-compliant is expected to be found

What you can do on a *nix system is set up postfix, Exim, or some other smtpd to use as a authenticated relay to send mail via some other SMTP server.

But then this is rapidly heading towards unix.stackexchange.com or perhaps superuser or serverfault

ivanivan
  • 2,155
  • 2
  • 10
  • 11
0

Are you trying to sent emails through a SMTP server that requires authentication ? If so, you can't use the native mail() function :
php.ini & SMTP= - how do you pass username & password

0

Plenty of working examples in the PHP Documentation

e.g.

<?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);
?>

To improve deliverability you may wish to set the 'Envelope From' address by passing the -f parameter to sendmail as follows:

mail($to, $subject, $message, $headers, '-f webmaster@example.com');

You should also check that an SPF record is setup on your domain allowing the web server IP address to send mail on behalf of the domain.

Chris Wheeler
  • 1,623
  • 1
  • 11
  • 18