add smtp to php mail function using phpmailer

739 views Asked by At

i'm trying to add smtp authentication using phpmailer. i'm new to php and i'm struggling to accomplish this. i have this code:

<?php
if( isset($_POST['name']) )
{
    $to = $_POST['budget'].',[email protected]';
    $subject = 'Programare servicii'; 
    $message = 'Nume: ' . $_POST['name'] . "\n" .
                         'Prenume: ' . $_POST['company']. "\n" .
                         'E-mail: ' . $_POST['email']. "\n" .
                         'Telefon: ' . $_POST['phone']. "\n\n" .
                         'Doresc: ' . $_POST['interested']. "\n" .
                         'Magazin: ' . $_POST['budget']. "\n" .
                         'Incepand cu: ' . $_POST['start']. "\n" .
                         'Pana la: ' . $_POST['finish']. "\n" .
                         'Observatii: ' . $_POST['comment']. "\n\n\n";



  $num = md5(time());
  $headers = 'From:' . $_POST['name'] . "\r\n";
  $headers .= 'Reply-To:' . $_POST['email'] . "\r\n";
  $headers .= "MIME-Version: 1.0\r\n";
  $headers .= "Content-Type: multipart/mixed; ";
  $headers .= "boundary=$num\r\n";
  $headers .= "--$num\r\n";$headers .= "Content-Type: text/plain\r\n";
  $headers .= "Content-Transfer-Encoding:8bit\r\n\n";
  $headers .= "$message\r\n";
  $headers .= "--$num\r\n";

    if( isset($_FILES['file']['tmp_name']) )
    {

        $file = fopen($_FILES['file']['tmp_name'], 'r');
      $size = $_FILES['file']['size'];
      $content = fread($file, $size);
      $encoded_content = chunk_split(base64_encode($content));      

        $headers .= "Content-Type: ". $_FILES['file']['type'] ."; ";
        $headers .= 'name="' . $_FILES['file']['name'] . '"' . "\r\n";
        $headers .= "Content-Transfer-Encoding: base64\r\n";
        $headers .= "Content-Disposition: attachment; ";
        $headers .= 'filename="' . $_FILES['file']['name'] . '"' . "\r\n\n";
        $headers .= "$encoded_content\r\n";
        $headers .= "--$num--";
    }
    mail($to, $subject, '', $headers);
}
?>

how do id add authentication? any ideas? i read the phpmailer examples and i was able to send an authenticated email with phpmailer. the problem is that i can't get the code above to work with smtp authentication. i have a dropdown in my form and from there the user can select a store and the script will send email to that store. when i add $_POST['email'] to $mail->addAddress nothing happens. Thanks for your time,

Bogdan

update 1: i mange to do this:

<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();                               
$mail->Host = 'localhost';  
$mail->SMTPAuth = true;                          
$mail->Username = '[email protected]';           
$mail->Password = 'pass';     
$mail->SMTPSecure = 'tls';  
$mail->Port = 587;           
$mail->From = $_POST['email'];
$mail->FromName = $_POST['name'];
$mail->addAddress('need to get data from form', 'iohan test');   
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->isHTML(true);                                
$mail->Subject = 'test iohan';
$mail->Body    = 'bla bla bla';
$mail->AltBody = 'bla bla';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

the question is how to make it work. i have a drop-down with multiple emails and i need to send emails to the email the user selected.

1

There are 1 answers

7
Synchro On

There is a very simple problem here. You're not using PHPMailer; you're using PHP's mail() function, which is not the same thing at all, and does not suport authentication because it does not use SMTP. I'd really recommend you don't even try doing this yourself; let PHPMailer do it for you. The example in the readme file should be enough to get you going.

All that stuff you're doing with headers, creating the message body, encoding attachments, etc is not necessary. Your code is also vulnerable to header injection attacks and may have incorrectly encoded headers. You're also not handling file uploads safely - read the PHP docs on that, or look at the 'send file upload' example provided with PHPMailer.

Assuming your form looks like this (you should not get the destination email from the form directly because it opens it to abuse as a spam gateway):

<select name="to">
    <option value="0">[email protected]</option>
    <option value="1" selected>[email protected]</option>
    <option value="2">[email protected]</option>
</select>

You could process that like this:

$to = [
    0 => '[email protected]',
    1 => '[email protected]',
    2 => '[email protected]',
];
$mail->addAddress($to[$_POST['to']]);

though you should do some validation too.

In the code you posted you're setting $mail->From using data form the form - you should not do that as it will cause the message to fail SPF checks; use a fixed from address and stick the sender's address into a reply-to.