使用Zend Framework和PHP发送电子邮件 [英] Sending email using Zend Framework and PHP

查看:115
本文介绍了使用Zend Framework和PHP发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一份表单上工作,当用户输入他们的电子邮件帐户并点击发送时,电子邮件将发送到他们的电子邮件帐户。

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发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆