1

I want to enter email and company name in a form and then have to send a random generated password to that email given.

Below is my code for generating random password and sending the email.

<?php

$company_name = "";
$email = "";
if (isset($_POST['submit'])) {
    $company_name = $_POST["company_name"];
    $email = $_POST["email_address"];
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*_";
    $password = substr(str_shuffle($chars), 0, 8);
    $to = $email;
    $subject = 'your registration is completed';
    $message = 'Welcome' . $company_name . ''
            . 'Your email and password is following :'
            . 'Email:' . $email . ''
            . 'Your new password : ' . $password . ''
            . 'Now you can login with this email and password';
    $headers = 'From :' . $email . "\r\n";
    mail($to, $subject, $message, $headers);
}
?>

but this shows the below error.

Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in "path"\password_generation.php on line 21

line 21 is mail($to, $subject, $message, $headers);

saku
  • 193
  • 3
  • 14
  • 1
    Check this : [link](http://stackoverflow.com/questions/28026932/php-warning-mail-sendmail-from-not-set-in-php-ini-or-custom-from-head) – Amine Soumiaa Apr 27 '16 at 09:58
  • @Amine Soumiaa it worked but this error is showing now `Warning: mail() [function.mail]: Failed to connect to mailserver at "127.0.0.1" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in "path"password_generation.php on line 23` – saku Apr 27 '16 at 10:12
  • check this for the stmp problem (1st solution) : [link](http://stackoverflow.com/questions/9660057/email-sending-from-local-server-in-php) – Amine Soumiaa Apr 27 '16 at 10:36

1 Answers1

0

Try this..

 <?php

    $company_name = "";
    $email = "";
    if (isset($_POST['submit'])) {
        $company_name = $_POST["company_name"];
        $email = $_POST["email_address"];
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*_";
        $password = substr(str_shuffle($chars), 0, 8);
        $to = $email;
        $subject = 'your registration is completed';
        $message = 'Welcome' . $company_name . ''
                . 'Your email and password is following :'
                . 'Email:' . $email . ''
                . 'Your new password : ' . $password . ''
                . 'Now you can login with this email and password';
        $headers = 'From: Your name <'.$email .'>' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

        mail($to, $subject, $message, $headers);
    }
    ?>
Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20
  • `Warning: mail() [function.mail]: Failed to connect to mailserver at "127.0.0.1" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in "path"password_generation.php on line 23` now this error is showing @Manjeet Barnala – saku Apr 27 '16 at 10:10
  • by default, PHP will try to send via a local SMTP server, thats why showing this warning, you need to do is edit your php.ini file, and find the SMTP option and change it with your smtp server – Manjeet Barnala Apr 27 '16 at 10:16
  • or try to upload this file to online server and test your email function, this will work online ... – Manjeet Barnala Apr 27 '16 at 10:18