조회수 : 3116
Django를 통한 이메일 보내기
settings.py

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'youremail@gmail.com'
EMAIL_HOST_PASSWORD = 'yourpassword'
EMAIL_PORT = 587


views.py에 email 보내는 코드

from django.core.mail import send_mail
send_mail(
          'Subject here',
          'Here is the message.',
          'from@example.com',
          ['to@example.com'],
          fail_silently=False
          )

attach file(파일 첨부 하기)

from forms import WorkForm
from django.core.mail import send_mail, EmailMessage


def work(request):
    template = 'other/work.html'                           

    if request.method == 'POST':
        form = WorkForm(request.POST, request.FILES)
        if form.is_valid():
            name = form.cleaned_data['name']
            nick = form.cleaned_data['nick']
            email = form.cleaned_data['email']
            subject = 'Work'
            text = form.cleaned_data['text']
            image1 = request.FILES['image1']
            image2 = request.FILES['image2']
            image3 = request.FILES['image3']
            try:
                mail = EmailMessage(subject, text, ['EMAIL_ADDRESS'], [email])
                mail.attach(image1.name, image1.read(), image1.content_type)
                mail.attach(image2.name, image2.read(), image2.content_type)
                mail.attach(image3.name, image3.read(), image3.content_type)
                mail.send()
                template = 'other/mail_sent.html'
            except:
                return "Attachment error"
            return render_to_response(template, {'form':form},
                              context_instance=RequestContext(request)) 
    else:
        form = WorkForm()                             
    return render_to_response(template, {'form':form},
                  context_instance=RequestContext(request))

Or, if the CSV data is in a file, you can use:

email.attach_file('/full/path/to/file.csv')




상담을 원하시면 상담신청을 눌러 주세요

다른 포스팅 Tag Cloud