How to add attachment to e-mail from form

I have a form on my website with an attachment option. However, the attachment is not added in the e-mail.

What is wrong with my code?

if (isset($_POST[“submit”])) {
$sql = “SELECT * FROM username WHERE USER = '” . $_POST[“gebruikersnaam”] . “’”;
$result = mysqli_query($conn, $sql);
$found = “”;
while ($row = mysqli_fetch_array($result)) {
$found = $row[“USER”];
}
if ($found != “” && $_POST[""] == “”) { //deze
//login
//echo “Vraag verstuurd”;
$to = "@hotmail.com";
$subject = "VRAAG FCA: " . $_POST[“categorie”];
$message = "gebruikersnaam: " . $_POST[“gebruikersnaam”] . “\r\n” . "Link wachtwoord: " . $_POST[“wachtwoord”] . “\r\n” . "Email van: " . $_POST[“Email”] . “\r\n” . $_POST[“Vraag”];
$headers = ‘From: @hotmail.com’ . “\r\n” . ‘Reply-To:’ .$_POST[“Email”]. ‘’ . “\r\n” . ‘X-Mailer: PHP/’ . phpversion();
mail($to, $subject, $message, $headers);
echo “”;
$_SESSION[“Email”] = $_POST[“Email”]; //deze
//header(“Location: nav.php”);
}
else {
//failed to login
echo “De gebruikersnaam is verkeerd”;

Form code:

">

">

Onderwerp eEntry Link CRM eSales eService Tablet Overige

Bijlage toevoegen

Thank you!

The simple answer is use a mailer library like swift or phpmailer

I implemented php mailer. If i visit the phpmailer.php part of the website i see this message: Message has been sent and i receive an e-mail with the input from the phpmailer.php code.

Beantwoorden|
Vandaag, 12:02
U;
[email protected];
[email protected]
This is the HTML message body in bold!

[php]<?php
require ‘phpmailertest/PHPMailer/PHPMailerAutoload.php’;

$mail = new PHPMailer;

//$mail->SMTPDebug = 3; // Enable verbose debug output

$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ‘mail.nl’; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ‘sendfrom’; // SMTP username
$mail->Password = ‘’; // SMTP password
$mail->SMTPSecure = ‘tls’; // Enable TLS encryption, ssl also accepted
$mail->Port = 587; // TCP port to connect to

$mail->setFrom(‘l’, ‘Mailer’);
$mail->addAddress(hotmail.com’, ‘Joe User’); // Add a recipient
$mail->addAddress(‘[email protected]’); // Name is optional
$mail->addReplyTo(‘hotmail.com’, ‘Information’);
$mail->addCC(‘[email protected]’);
$mail->addBCC(‘[email protected]’);

$mail->addAttachment(’/var/tmp/file.tar.gz’); // Add attachments
$mail->addAttachment(’/tmp/image.jpg’, ‘new.jpg’); // Optional name
$mail->isHTML(true); // Set email format to HTML

$mail->Subject = 'Vraag ';
$mail->Body = ‘This is the HTML message body in bold!’;
$mail->AltBody = ‘This is the body in plain text for non-HTML mail clients’;

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

[/php]

How can i load the form input of the other page. Code shared in earlier comment in this thread. Can i implement the PHP mailer code on the same page? Or can i add something to the code so it loads the input from the already present form.

Thank you

I hope I am understanding you correctly, but PHPMailer is just a way to send email instead of using PHP’s built-in mail function. In short you would just replace that goobly gook in the previous code with PHPMailer commands.

As a matter of fact once you get the hang of PHPMailer you can write a function that sends the message(s). That way you can reuse it in other projects or even for the same website.

Here’s an example of a method (another name for function OOP style) that I use ->

[php] protected function deliverMail() {

    $this->mail = new PHPMailer; // Set up a new instance of PHPMailer:

    $this->mail->Host = EMAIL_HOST; // You're email host (I'm using a constant using the define() function):
    if (filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_URL) == "localhost") {
        $this->mail->isSmtp(); // Local Host:
        $this->mail->Port = EMAIL_PORT; // Local Host Port: (Usually 587)
    } else {
        $this->mail->isSendMail(); // Remote Host:
    }
    $this->mail->SMTPAuth = true;
    $this->mail->Username = EMAIL_ADDRESS; // SMTP username:
    $this->mail->Password = EMAIL_PASSWORD; // SMTP password:
    $this->mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted:        

    $this->mail->From = $this->emailData->getEmail();
    $this->mail->FromName = $this->emailData->getName();
    $this->mail->addAddress(EMAIL_ADDRESS, EMAIL_USERNAME);
    $this->mail->WordWrap = 50;                                 // Set word wrap to 50 characters
    $this->mail->Subject = $this->emailData->getTopic();
    $this->mail->Body = '<img src="https://www.pepster.com/lib/images/img-logo-01.png"><p style="color:green;font-family:sans-serif;">' . $this->getMessage() . '</p>';
    $this->mail->AltBody = $this->getMessage();
    $this->mail->isHTML(true);

    if (!$this->mail->send()) {
        echo 'Mailer Error: ' . $this->mail->ErrorInfo;
        exit;
    } else {
        return TRUE;
    }
}[/php]

HTH John

I’m with John. You could even do a wrapper that would replace the default mail function:

[php]<?php

function mail($to, $subject, $body, Array $options = null)
{
require ‘phpmailertest/PHPMailer/PHPMailerAutoload.php’;

$mail = new PHPMailer;

//$mail->SMTPDebug = 3; // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'mail.nl';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'sendfrom';                 // SMTP username
$mail->Password = '';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('l', 'Mailer');
$mail->addAddress($to);     // Add a recipient
$mail->addAddress('[email protected]');               // Name is optional
$mail->addReplyTo('hotmail.com', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = $body;

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

}[/php]

More work is needed to incorporate the options parameters, but hopefully, you get the idea.

Sponsor our Newsletter | Privacy Policy | Terms of Service