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 = "***********"
wassim chaguetmi
  • 113
  • 2
  • 13

1 Answers1

0

Maybe have a look at this. It doesn't use django to send emails, instead it uses the python smptlib library and requires you to run your own local SMTP server similar to how php accomplishes it. They recommended using the open source Send Mail.

GTBebbo
  • 1,198
  • 1
  • 8
  • 17