我正在编写一个小型手机应用,需要将邮件从应用后端服务器发送到指定的电子邮件地址。
我看到有很多脚本可以在vb和c#等中实现这一点,但它们似乎都需要我不熟悉的SMTP服务器。
任何人都可以建议我如何从服务器脚本发送一个简单的电子邮件而没有昂贵的现成软件包?
答案 0 :(得分:1)
以下是使用python发送电子邮件的示例(这是来自检查电子邮件和发送电子邮件的程序)
import smtplib, imaplib
global sender
print "preparing to send message..."
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
send_from = 'myemail@gmail.com'
password = 'PASSWORD'
subject = ''
print "Sending: ", body
recipient = sender
print "Send to: ", recipient
headers = ["From: " + send_from,
"Subject: " + subject,
"To: " + recipient,
"MIME-Version: 1.0",
"Content-Type: text/plain"]
#"Content-Type: text/html"]
#to send html
headers = "\r\n".join(headers)
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo
session.login(send_from, password)
session.sendmail(send_from, recipient, headers + "\r\n\r\n" + body)
session.quit()
如您所见,它使用凭据从Gmail帐户发送电子邮件。我添加了评论,向您展示如何在电子邮件中发送html。如果您需要更多帮助,请告诉我。
使用php和Pear邮件包:
require_once "Mail.php";
$from = '<from.gmail.com>';
$to = '<to.yahoo.com>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'johndoe@gmail.com',
'password' => 'passwordxxx'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<p>Message successfully sent!</p>');
}
或者,如果你只想从php运行python脚本,你可以这样做:
<?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>