How would you write a bash script to take a users informations from the php webform and email the user saved on the server using postfix when they submit the form? The users saved on the server should be able to login and recieve mail upon submitting the webform.

Here is my webform script.

<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Update MailList Database</title>
<h1>Update MailList Database</h1>
<form method="post" action="EnterContacts.php"> 
Name: <input type="text" name="name" /><br/>
Email: <input type="text" name="email"/><br/>
<input type="submit" name="Enter" value="Enter" />
if (isset($_POST['Enter']))
$db_hostname = 'localhost';
$db_database = 'mailList';
$db_username = 'root';
$db_password = 'password1';

$db_server = mysqli_connect ($db_hostname, $db_username, $db_password,$db_database);
if (!$db_server) die("Unable to connect to MYSQL or select database: ". mysql_error ());

$query = "INSERT INTO UserNamesMail (UName,email) VALUES ('$name','$email')";

$result = mysql_query($db_server,$query);
if (!result) die ("Database access failed: ". mysql_error());

$numRows = mysqli_num_rows($result);
echo $numRows;

// Database is disconnected when page script has completed.

If you have a form in PHP, why not use PHP to send the mail? Is there any reason to use a third language to accomplish this? Simply configure the sendmail_path in php.ini and use PHP's mail() function.

Summary of the instructions

Start by configuring PHP to use sendmail, by modifying sendmail_path in your php.ini file. Open /etc/php5/apache2/php.ini in a text editor. Update the line already containing sendmail_path. If no such line exist, simply add it on a new line. It should look something like this: sendmail_path = "/usr/sbin/sendmail -t -i"

Now you can use a simple PHP script to send mail instead of saving it to a database first. There might still be good reasons for saving the mail to a database, such as logging and traceability, but there is nothing preventing you from combining the two methods.

Replace your PHP code with the following (Note, this is untested code, so make sure you understand it and test it before putting it into production)

if (isset($_POST['Enter'])) {
    $to      = $_POST['name'] . ' <'$_POST['email'] . '>';
    $subject = 'Hello ' . $_POST['name'];
    $message = 'Welcome to the mailing list';
    $headers = 'From: webmaster@example.com' . "\r\n" .
        'Reply-To: webmaster@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

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

The additional headers can be ommitted, but you should probably keep at least the "From" header.

| improve this answer | |
  • 1
    Please include the commands and/or directions to do the task, within your post, and link the blog or other links and reference. The goal of the community overall is to ensure the information stays available even if other sites go down. – Dave Dec 30 '15 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.