簡體   English   中英

任何人都可以建議如何使用send_mail發送電子郵件? 沒用

[英]Anyone can suggest how to send email with send_mail? It's not working

用戶注冊時,默認情況下is_active為False。 我希望用戶在管理員激活用戶時收到電子郵件通知。 但是send_mail沒有發送電子郵件。

我在views.py中創建了一個函數:

def send_mail(request):
    if user.is_active == True:
        send_mail(request, subject='subject',
          message='message',
          from_email='hekevintran_test@webfaction.com',
          recipient_list=['recipient@yahoo.com'],
          fail_silently=False)

另外,我在settings.py中寫下了這些內容:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'ualmaz'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 25
EMAIL_USE_TLS = False

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_FILE_PATH = os.path.join(BASE_DIR, 'apps', 'emails')

這是我的views.py:

from django.shortcuts import render, redirect
from django.contrib import messages
from django.core.mail import send_mail
from django.urls import reverse_lazy
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.views.generic import CreateView, DetailView, ListView, UpdateView, DeleteView
from .forms import UserCreationModelForm, UserUpdateForm, ProfileUpdateForm
from .models import User, Post, Profile

class UserRegistrationView(CreateView):
    form_class = UserCreationModelForm
    user = User
    success_url = reverse_lazy('login')
    template_name = 'users/registration.html'

def send_mail(request):
    user = User
    if user.is_active == True:
        send_mail(request, subject='subject',
          message='message',
          from_email='hekevintran_test@webfaction.com',
          recipient_list=['recipient@yahoo.com'],
          fail_silently=False)

這是我在models.py中的用戶模型:

class User(AbstractUser):
    first_name = models.CharField(verbose_name="First name", max_length=255)
    last_name = models.CharField(verbose_name="Last name", max_length=255)
    country = models.CharField(verbose_name="Country name", max_length=255)
    city = models.CharField(verbose_name="City name", max_length=255)
    email = models.EmailField(verbose_name="Email", max_length=255)
    access_challenge = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)

    def __str__(self):
        return self.username

有任何想法嗎?

這是怎么解決的。

from django.dispatch import receiver
from django.db.models.signals import pre_save, post_save
from django.conf import settings
from django.core.mail import send_mail


#signal used for is_active=False to is_active=True
@receiver(pre_save, sender=User, dispatch_uid='active')
def active(sender, instance, **kwargs):
    try:
        if instance.is_active and User.objects.filter(pk=instance.pk, is_active=False).exists():
            subject = 'Your account is activated'
            mesagge = '%s your account is now active' %(instance.first_name)
            from_email = settings.EMAIL_HOST_USER
            send_mail(subject, mesagge, from_email, [instance.email], fail_silently=False)
    except:
        print("Something went wrong, please try again.")


#signal to send an email to the admin when a user creates a new account
@receiver(post_save, sender=User, dispatch_uid='register')
def register(sender, instance, **kwargs):
    try:
        if kwargs.get('created', False):
            subject = "Verificatión of the %s 's account" %(instance.username)
            mesagge = '%s, %s just registered in locator' %(instance.first_name, instance.last_name)
            from_email = settings.EMAIL_HOST_USER
            send_mail(subject, mesagge, from_email, [from_email], fail_silently=False)
    except:
        print("Something went wrong, please try again.")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM