0

I'm having some issues in understanding how to send custom headers with the php mail() function.

What's wrong with this code?

<?php 
function send_email($name, $lastname, $from, $subject, $message){
    $to = 'to_adress@gmail.com';
    $headers = 'To: ' . $to . 
      '\r\nFrom: ' . $from .
      '\r\nSubject:' . $subject;

    mail($to, $subject, $message, $headers);
    print 'Email sent';

  }

  send_email('jhon', 'doe', 'from_adress@gmail.com', 'subject', 'message');

 ?>

It gives me no php error, and the email does not arrive.

gaggina
  • 5,369
  • 10
  • 31
  • 31

1 Answers1

0

'\r\n' is not the same as "\r\n". Try something like this instead:

$headers = 'To: ' . $to . "\r\n"
         . 'From: ' . $from . "\r\n"
         . 'Subject: ' . $subject;

However you don't need to repeat the To: and Subject: headers, so just using this should do:

$headers = 'From: ' . $from;
juanrpozo
  • 697
  • 4
  • 8