问题
I want to send email via PHP script using my free Gmail as the From
sender. How can this be done such that the SPF records will be valid (as if the mail was actually sent from Gmail)?
回答1:
There are many other libraries as well. Some of them are :
- http://phpmailer.worxware.com/
- http://pear.php.net/package/Mail (This is Pear package)
- https://github.com/denvertimothy/ThriveSMTP
- http://swiftmailer.org/
You can send the mail from PHP using SMTP using any of these libraries
An example of sending mail using your Gmail account with PHPMailer library will be :
//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!";
}
Hope this helps you
回答2:
As one of the solutions, you can use 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();
回答3:
You will have to send the message through Google's SMTP servers. If you need pure PHP, find a SMTP library (I only know of Net_SMTP from PEAR) and configure it for smtp.gmail.com
with the usual settings that Gmail gives you.
来源:https://stackoverflow.com/questions/16022238/send-from-free-gmail-account-via-php