Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 30079

Django send email from a form, with the filler's email as FROM

$
0
0

I'm moving my portfolio from PHP to Python (django), and I have a contact form in it, when it was in PHP, it didn't need any credentials, and when it sent the email,I receive an email FROM: user@mail.com normally, now when I tried to convert it to Django, using send_mail, when an email is sent I receive an Email FROM: myemail@gmai.com (me). i want to know how I could fix this, receive the email with the sender email as From, otherwise i wouldn't be able to reply to them. My question is not a duplicate, as i have seen the other questions and their anwers and it didn't help.

Here is the PHP:

<?php

// Replace this with your own email address
$siteOwnersEmail = 'wassim.chaguetmi@gmail.com';


if ($_POST) {

$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));

// Check Name
if (strlen($name) < 2) {
    $error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
    $error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
    $error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') {
    $subject = "Contact Form Submission";
}


// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";

// Set From: header
$from = $name . "<" . $email . ">";

// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


if (!$error) {

    ini_set("sendmail_from", $siteOwnersEmail); // for windows server
    $mail = mail($siteOwnersEmail, $subject, $message, $headers);

    if ($mail) {
        echo "OK";
    } else {
        echo "Something went wrong. Please try again.";
    }

} # end if - no validation error

else {

    $response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
    $response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
    $response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;

    echo $response;

} # end if - there was a validation error

}

?>

and here is my view in django:

class SendEmail(View):

def post(self, request):
    form = SendEmailForm(request.POST)
    if form.is_valid():
        name = form.cleaned_data['contact_name']
        email = form.cleaned_data['contact_email']
        subject = form.cleaned_data['contact_subject']
        message = form.data['contact_message']

        try:
            send_mail(subject, message, email, ['wassim.chaguetmi@gmail.com'])
        except BadHeaderError:
            return redirect("profile:index")
        return redirect("profile:index")
    else:
        return redirect("profile:index")

and here is the project settings:

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = "myemail@gmail.com"
EMAIL_HOST_PASSWORD = "***********"

Viewing all articles
Browse latest Browse all 30079

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>