問題描述
我正在編寫一個表單,當用戶輸入他們的電子郵件帳戶并點擊發送時,一封電子郵件將發送到他們的電子郵件帳戶.
I working on a form whereby when the user enter in their email account and click on send, an email will be sent to their email account.
我已經解決了所有問題.只是它不會將電子郵件發送到我的帳戶.有人有想法么?有沒有我遺漏的配置之類的?
I have everything worked out. Just that it doesnt send the email to my account. Anyone have any ideas? Is there a configuration that I left out or something?
這是來自我的控制器的示例:
This is the sample from my controller:
public function retrieveemailAction(){
$users = new Users();
$email = $_POST['email'];
$view = Zend_Registry::get('view');
if($users->checkEmail($_POST['email'])) {
// The Subject
$subject = "Email Test";
// The message
$message = "this is a test";
// Send email
// Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
// Use if command to display email message status
if(mail($email, $subject, $message, $headers)) {
$view->operation = 'true';
}
} else {
$view->operation = 'false';
}
$view->render('retrieve.tpl');
}
推薦答案
我建議您使用 Zend_Mail
而不是 mail()
.它可以自動處理很多東西,而且效果很好.
I recommend you use Zend_Mail
instead of mail()
. It handles a lot of stuff automatically and just works great.
你有 SMTP 服務器嗎?嘗試在沒有您自己的 SMTP 服務器的情況下發送郵件可能會導致郵件無法發送.
Do you have a SMTP server? Trying to send mail without your own SMTP server could be causing the mail to not be sent.
這是我使用 Zend_Mail
和 Gmail 發送郵件的方式:
This is what I use for sending mails using Zend_Mail
and Gmail:
在Bootstrap.php
中,我配置了一個默認的郵件傳輸:
In Bootstrap.php
, I configure a default mail transport:
protected function _initMail()
{
try {
$config = array(
'auth' => 'login',
'username' => 'username@gmail.com',
'password' => 'password',
'ssl' => 'tls',
'port' => 587
);
$mailTransport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
Zend_Mail::setDefaultTransport($mailTransport);
} catch (Zend_Exception $e){
//Do something with exception
}
}
然后我使用以下代碼發送電子郵件:
Then to send an email I use the following code:
//Prepare email
$mail = new Zend_Mail();
$mail->addTo($email);
$mail->setSubject($subject);
$mail->setBody($message);
$mail->setFrom('username@gmail.com', 'User Name');
//Send it!
$sent = true;
try {
$mail->send();
} catch (Exception $e){
$sent = false;
}
//Do stuff (display error message, log it, redirect user, etc)
if($sent){
//Mail was sent successfully.
} else {
//Mail failed to send.
}
這篇關于使用 Zend Framework 和 PHP 發送電子郵件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!