PHP MAIL function with variables >>> HOW?

3.2k Views Asked by At

I'm creating a contact.php. My plan is to include in the e-maail stuff which the user entered before. (variables) In this case include the sender email, subject and message. I don't want to use PEAR. I want to use my SSMTP which is installed on my Raspberry Pi on Debian.

Code for form + input and textarea:

 <form method="POST" action="contact.php?page=log">
<input type="text" name="from_email" placeholder="Your E-Mail"/>
<input type="text" name="subject_email" placeholder="Subject"/>
<textarea rows="5" cols="50" name="message_email" style="width: 100%" placeholder="Message"></textarea>
<input type="submit" name="submite_email" value="Send E-Mail" />
</form>

Code for PHP Email:

<?php
if(isset($_POST['from_email']) and isset($_POST['subject_email']) and isset($_POST['message_email'])){
$to      = '[email protected]';
$subject = $_GET['subject_email'];
$message = $_GET['message_email'];
$headers = 'From: '$_GET['from_email'] . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
} ?>

SSMTP and php.ini are set. If I use the default form for this it sends me the email.

<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

There are 1 best solutions below

3
On BEST ANSWER

You ca use phpmailer class https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php

    require_once('../class.phpmailer.php');

    //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

    $mail             = new PHPMailer();

    $body             = file_get_contents('contents.html');
    $body             = eregi_replace("[\]",'',$body);

    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host       = "mail.yourdomain.com"; // SMTP server
    $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                               // 1 = errors and messages
                                               // 2 = messages only
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
    $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "yourname@yourdomain"; // SMTP account username
    $mail->Password   = "yourpassword";        // SMTP account password

    $mail->SetFrom('[email protected]', 'First Last');

    $mail->AddReplyTo("[email protected]","First Last");

    $mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";

    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

    $mail->MsgHTML($body);

    $address = "[email protected]";
    $mail->AddAddress($address, "John Doe");

    $mail->AddAttachment("images/phpmailer.gif");      // attachment
    $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

    if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
      echo "Message sent!";
    }