【发布时间】:2013-04-07 23:37:02
【问题描述】:
我想使用我的免费 Gmail 作为From 发件人,通过 PHP 脚本发送电子邮件。怎样才能使 SPF 记录有效(就好像邮件实际上是从 Gmail 发送的一样)?
【问题讨论】:
我想使用我的免费 Gmail 作为From 发件人,通过 PHP 脚本发送电子邮件。怎样才能使 SPF 记录有效(就好像邮件实际上是从 Gmail 发送的一样)?
【问题讨论】:
还有许多其他库。其中一些是:
您可以使用这些库中的任何一个使用 SMTP 从 PHP 发送邮件
使用带有 PHPMailer 库的 Gmail 帐户发送邮件的示例如下:
//include the file
require_once('class.phpmailer.php');
$phpmailer = new PHPMailer();
$phpmailer->IsSMTP(); // telling the class to use SMTP
$phpmailer->Host = "ssl://smtp.gmail.com"; // SMTP server
$phpmailer->SMTPAuth = true; // enable SMTP authentication
$phpmailer->Port = 465; // set the SMTP port for the GMAIL server; 465 for ssl and 587 for tls
$phpmailer->Username = "yourname@yourdomain"; // Gmail account username
$phpmailer->Password = "yourpassword"; // Gmail account password
$phpmailer->SetFrom('name@yourdomain.com', 'First Last'); //set from name
$phpmailer->Subject = "Subject";
$phpmailer->MsgHTML($body);
$phpmailer->AddAddress($to, "To Name");
if(!$phpmailer->Send()) {
echo "Mailer Error: " . $phpmailer->ErrorInfo;
} else {
echo "Message sent!";
}
希望对你有帮助
【讨论】:
作为解决方案之一,可以使用 Zend Framework Zend_Mail class
$email_conf = array(
'auth' => 'login',
'username' => 'your_login',
'password' => 'your_password',
'ssl' => 'tls',
'port' => '587'
);
$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $email_conf);
Zend_Mail::setDefaultTransport($transport);
$mailer = new Zend_Mail('utf-8');
$mail->addTo($recipientEmail);
$mail->setSubject($subject);
$mailer->setBodyHtml($html);
$mail->send();
【讨论】:
您必须通过 Google 的 SMTP 服务器发送邮件。如果您需要纯 PHP,请找到一个 SMTP 库(我只知道 PEAR 中的 Net_SMTP)并使用 Gmail 为您提供的常用设置将其配置为 smtp.gmail.com。
【讨论】: