2013-05-03 3 views
0

첨부 파일이있는 전자 메일을 보내려고합니다. IOError : [Errno 2] 해당 파일이나 디렉토리가 없습니다. 하지만 그것이 말하는 URL은 존재하지 않습니다. 음, 실제로 존재합니다. 양식은 파일을 업로드하고 Signature = ... & 만료 = ... & AWSAccessKeyId = 끝에 추가 된 파일을 다른 창에서 불러올 때 파일과 함께 업로드하는 FileField.url을 업로드합니다.Amazon SES, Celery Tasks에서 Django의 첨부 파일로 전자 메일 보내기

내 Django 앱은 Amazon-SES를 사용합니다. 나는 send_mail()와 미세을 전송했지만, 첨부 파일을 지원하지 않는 래퍼, 그래서 난 내 tasks.py이 전환 :

from django.core.mail.message import EmailMessage 
from celery import task 
import logging 
from apps.profiles.models import Client 

@task(name='send-email') 
def send_published_article(sender, subject, body, attachment): 
    recipients = [] 
    for client in Client.objects.all(): 
     recipients.append(client.email) 
    email = EmailMessage(subject, body, sender, [recipients]) 
    email.attach_file(attachment) 
    email.send() 

그리고 (A form.save 내보기에이 전화)

다음
from story.tasks import send_published_article 
def add_article(request): 
    if request.method == 'POST': 
     form = ArticleForm(request.POST, request.FILES or None) 
     if form.is_valid(): 
      article = form.save(commit=False) 
      article.author = request.user 
      article.save() 
      if article.is_published: 
       subject = article.title 
       body = article.text 
       attachment = article.docfile.url 
       send_published_article.delay(request.user.email, 
              subject, 
              body, 
              attachment) 
      return redirect(article) 
    else: 
     form = ArticleForm() 
    return render_to_response('story/article_form.html', 
           { 'form': form }, 
           context_instance=RequestContext(request)) 

입니다 로그는 말 :

app/celeryd.1: File "/app/.heroku/python/lib/python2.7/site-packages/django/core/mail/message.py", line 268, in attach_file 
app/celeryd.1: content = open(path, 'rb').read() 
app/celeryd.1: IOError: [Errno 2] No such file or directory: 

모든

답변

1

편집 # 2 - .read() 기능을 사용하려면 파일 모드가 'r'이어야합니다.

왜냐하면 default_storage.open()을 사용하는 것을 잊었 기 때문에 이유는 "그러한 파일이나 디렉토리가 없습니다"라고 말합니다. 파일은 앱과 동일한 컴퓨터에 있지 않으며 정적 파일은 AWS S3에 저장됩니다.

from celery import task 
from django.core.mail.message import EmailMessage 
from django.core.files.storage import default_storage 
from apps.account.models import UserProfile 

@task(name='send-email') 
def send_published_article(sender, subject, body, attachment=None): 
    recipients = [] 
    for profile in UserProfile.objects.all(): 
     if profile.user_type == 'Client': 
      recipients.append(profile.user.email) 
    email = EmailMessage(subject, body, sender, recipients) 
    try: 
     docfile = default_storage.open(attachment.name, 'r') 
     if docfile: 
      email.attach(docfile.name, docfile.read()) 
     else: 
      pass 
    except: 
     pass 
    email.send() 
0

첨부가 될한다 파일을 찾은 다음 Django e-mail documentation에있는 attach_file을 검색하십시오.

전자 메일의 파일 (URL)에 링크하거나 파일을 다운로드하여 첨부 한 다음 나중에 로컬로 삭제할 수 있습니다.

+0

나는 이메일에 링크를 삽입하는 방법을 알고 있습니다. 파일을 다운로드하고 나중에 삭제해야하는 명령을 모르겠습니다. 어떤 제안? 나는 [이 SO 페이지] (http://stackoverflow.com/questions/908258/generating-file-to-download-with-django)를 읽었으며 cStringIO를 많이 보았습니다.이 유형의 작업을 처리 할 수 ​​있습니까? –

관련 문제