1

My code is not working, how can I solve this problem? I want to receive mail on my Yahoo ID.

<?php
  // the message
  $msg = "First line of text\nSecond line of text";

  // use wordwrap() if lines are longer than 70 characters
  $msg = wordwrap($msg,70);

  // send email
  mail("faisalkhan00668@yahoo.com","My subject",$msg);
?>

The error:

Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in C:\xampp\htdocs\mail\mail.php on line 9

halfer
  • 19,824
  • 17
  • 99
  • 186
user3651840
  • 39
  • 2
  • 9

3 Answers3

5

In the fourth parameter you can add headers to the mail. Here you can add your from:

explained on the php site here http://php.net/manual/en/function.mail.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);

This will fix the from problem

Now in php.ini there is a line

;sendmail_from = postmaster@localhost

If you uncomment this by removing the ; you can set a default. in which case you won't have to add headers to the mail() call.

Meneer Venus
  • 1,039
  • 2
  • 12
  • 28
1

Are you using SMTP? try to insert those 2 lines at the beginning of your php code:

ini_set ("SMTP","localhost"); ini_set ("sendmail_from","xxxyourmailxxx@xxxxxx.xxx");

MarcinKwiatkowski
  • 247
  • 1
  • 3
  • 15
  • Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\mail\mail.php on line 11 still this error is occur – user3651840 May 28 '14 at 10:20
  • Try this: $headers="From: mittente@mail.it"; mail("faisalkhan00668@yahoo.com","My subject",$msg,$headers); – MarcinKwiatkowski May 28 '14 at 10:31
1

This can be done using the mail() function. Please remember that this will not work on a local server.

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

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

To create an SMTP server you need to do the following:

Create SMTPConfig.php

<?php
//These need to be changed to actual values
    $SmtpServer="127.0.0.1";
    $SmtpPort="25"; //default
    $SmtpUser="username";
    $SmtpPass="password";
?>

add the following code to the index.php file:

<?php
   include('SMTPconfig.php');
   include('SMTPClass.php');

   if($_SERVER["REQUEST_METHOD"] == "POST") {
       $to = $_POST['to'];
       $from = $_POST['from'];
       $subject = $_POST['sub'];
       $body = $_POST['message'];
       $SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from,                    $to, $subject, $body);
       $SMTPChat = $SMTPMail->SendMail();
   }
  ?>
  <form method="post" action="">
  To:<input type="text" name="to" />
  From :<input type='text' name="from" />
  Subject :<input type='text' name="sub" />
  Message :<textarea name="message"></textarea>
  <input type="submit" value=" Send " />
Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79