//Gmail发送邮件
public
function send() {
//Initialize needed variables
$your_name
= 'Mario Awad';
$your_email
= 'your_email@your_domain.com';
//Or your_email@gmail.com for Gmail
$your_password
= 'your_password';
$send_to_name
= 'My Friend';
$send_to_email
= 'myfriend@tempinbox.com';
//SMTP server configuration
$smtpHost
= 'smtp.gmail.com';
$smtpConf
= array(
'auth'
=> 'login',
'ssl'
=> 'ssl',
'port'
=> '465',
'username'
=> $your_email,
'password'
=> $your_password
);
$transport
= new
Zend_Mail_Transport_Smtp($smtpHost,
$smtpConf);
//Create email
$mail
= new
Zend_Mail();
$mail->setFrom($your_email,
$your_name);
$mail->addTo($send_to_email,
$send_to_name);
$mail->setSubject('Hello World');
$mail->setBodyText('This is the body text of the email.');
//Send
$sent
= true;
try
{
$mail->send($transport);
echo "Send ok";
}
catch
(Exception $e) {
$sent
= false;
echo $e->getMessage();
}
//Return boolean indicating success or failure
return
$sent;
}

本文介绍了一种使用PHP和Gmail SMTP服务器发送电子邮件的方法。通过示例代码展示了如何配置SMTP服务器、创建邮件内容及实现邮件的发送,并处理可能发生的异常。
806

被折叠的 条评论
为什么被折叠?



