sendmail 설치 및 구성

sendmail 설치 및 구성

인증 없이 Linux 시스템에서 사용자 이메일 주소로 파일을 보낼 수 있는 방법이 있습니까?

SMTP 서버 없이 사용자의 컴퓨터에서 다른 사용자의 이메일 주소로 파일을 보내고 싶습니다. 그렇다면 Gmail에서 보낼 수 있으며 스크립트를 통해 자동으로 수행되어야 하며 사용자는 수동 작업을 수행할 필요가 없습니다.

답변1

SMTP는 필수 사항입니다(Simple Mail Transfer Protocol). 구성하는 방법이 있습니다Gmail SMTP 릴레이를 사용하여 이메일 보내기.

sendmail 설치 및 구성

Debian 시스템의 루트 사용자로.

apt-get install sendmail mailutils sendmail-bin
mkdir -m 700 /etc/mail/authinfo
cd /etc/mail/authinfo
#hash your gmail password info
makemap hash gmail-auth <<<'AuthInfo: "U:root" "I:YOUR GMAIL EMAIL ADDRESS" "P:YOUR PASSWORD"'
#don't save your bash history because of password info
unset HISTFILE

sendmail.mc 구성 파일의 첫 번째 "MAILER" 정의 줄 바로 위에 다음 줄을 배치합니다.

define(`SMART_HOST',`[smtp.gmail.com]')dnl
define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl
define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl
define(`confAUTH_OPTIONS', `A p')dnl
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
FEATURE(`authinfo',`hash -o /etc/mail/authinfo/gmail-auth.db')dnl

sendmail 구성을 다시 작성하십시오.

make -C /etc/mail

보내는 이메일을 다시 로드하세요.

/etc/init.d/sendmail reload

이메일 보내기 테스트

echo "A simple message" | mail -s "Some subject" [email protected]

답변2

네, smtp 서버 없이 파일을 첨부하여 자동 이메일을 보내고 싶습니다.

이 경우에는 Python을 사용합니다(과거에는 첨부 파일이 없었지만 이 작업을 수행했습니다). Python을 사용하여 이메일을 보내는 데는 몇 분 밖에 걸리지 않습니다 import.

다음은 Gmail 주소를 사용하여 빠르게 구성한 예입니다.

#!/usr/bin/env python3

import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# Your login credentials
sender = "[email protected]"
emailPasswd = "yourpassword"

# Who are we sending to
receiver = "[email protected]"

# The path to the file we want to attach
fileToAttach = "att.txt"

msg = MIMEMultipart()
msg['Subject'] = "Here's an e-mail with attachment"
msg['From'] = sender
msg['To'] = receiver
body = "Mail with attachment"
bodyText = MIMEText(body, "plain")

# Now we try to add the attachment
try:
    att = open(fileToAttach)
    attachment = MIMEText(att.read())
    attachment.add_header('Content-Disposition', 'attachment', filename=fileToAttach)
except IOError:
    print("Could not add attachment {}".format(fileToAttach))
    exit(1)

# "Attach" both the attachment and body to 'msg'
msg.attach(bodyText)
msg.attach(attachment)

# Connect and send e-mail
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(sender, emailPasswd)
server.sendmail(sender, receiver, msg.as_string())
server.quit()

이 방법은 작동하지만 완료할 때까지는 작동하지 않습니다.이것. "보안 수준이 낮은 응용프로그램이 [Gmail] 계정에 액세스하는 것을 허용하지 않으면"아니요스크립트를 사용하여 로그인하는 기능. 대신 SMTPAuthenticationError(오류 코드 ) 가 표시됩니다 534. 바라보다여기좋은 참고자료입니다.

이제 지적할 필요는 없을지 모르지만 어쨌든 위의 작은 코드 조각은 txt첨부 파일에 작동합니다. 예를 들어 이미지를 첨부하려면 해당 모듈을 가져와야 합니다.from email.mime.image import MIMEImage

또는 첨부 파일을 "하드코드"하지 않으려면 간단히 매개변수로 전달할 수 있습니다. 스크립트를 호출하는 경우 ./pySmtp.py다음과 같이 호출하세요.

./pySmtp.py att.txt

그렇다면 코드를 다음과 같이 변경하세요.

#!/usr/bin/env python3

import sys
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# Your login credentials
sender = "[email protected]"
emailPasswd = "yourpassword"

# Who are we sending to
receiver = "[email protected]"

# The path to the file we want to attach
fileToAttach = sys.argv[1]

[rest of code stays the same]

"자동" 부분은 필요에 따라 직접 선택해야 합니다.

관련 정보