问题描述
我正在将我的投资组合从PHP转移到Python(django),并且其中有一个联系表单,当它使用PHP时,不需要任何凭据,当它发送电子邮件时,我收到电子邮件发件人:[email protected]正常,现在,当我尝试使用send_mail将其转换为Django时,发送电子邮件时,我收到了电子邮件发件人:[email protected](我).我想知道如何解决此问题,以发件人电子邮件的形式接收来自发件人的电子邮件,否则我将无法回复他们.我的问题不是重复的,因为我已经看到了其他问题及其答案,并且没有帮助.
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: [email protected] normally, now when I tried to convert it to Django, using send_mail, when an email is sent I receive an Email FROM: [email protected] (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.
这是PHP:
<?php
// Replace this with your own email address
$siteOwnersEmail = '[email protected]';
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
}
?>
这是我在django中的观点:
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, ['[email protected]'])
except BadHeaderError:
return redirect("profile:index")
return redirect("profile:index")
else:
return redirect("profile:index")
这是项目设置:
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = "[email protected]"
EMAIL_HOST_PASSWORD = "***********"
推荐答案
也许看看.它不使用django发送电子邮件,而是使用python smptlib
库,并要求您运行自己的本地SMTP服务器,类似于php的实现方式.他们建议使用开源发送邮件.
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.
这篇关于Django从表单发送电子邮件,填充电子邮件为FROM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!