我正在编写一个小型手机应用程序,它要求从应用程序后端服务器将邮件发送到指定的电子邮件地址。
我看到在vb和c#等版本中有很多脚本可以实现这一点,但是它们似乎都需要SMTP服务器,这一点我并不熟悉。
有人能建议我如何从服务器脚本发送一封简单的电子邮件,而不需要昂贵的现成包?
发布于 2014-02-06 23:39:08
下面是一个使用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和梨邮件包:
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;
?>https://stackoverflow.com/questions/21612792
复制相似问题