我想尝试使用cake php发送邮件。我没有发送邮件的经验。所以,我不知道从哪里开始。是否需要制作邮件服务器?如果需要,如何制作邮件服务器以及如何发送邮件?请一步一步解释。我真的不知道从哪里开始。
我正在使用xampp,现在我在localhost上测试我的网站。
我测试了以下链接:
http://book.cakephp.org/view/1286/Sending-a-basic-message
但发生错误无法直接访问。
然后我从以下链接添加代码:
http://book.cakephp.org/view/1290/Sending-A-Message-Using-SMTP
所以,我的代码如下:
function _sendMail(){
$this->Email->to = 'user@gmail.com';
$this->Email->bcc = array('secret@example.coom');
$this->Email->subject = 'Welcome to our really cool things';
$this->Email->replyTo = 'support@example.com';
$this->Email->from = 'Online Application <app@example.coom>';
$this->Email->template = 'simple_message';
$this->Email->sendAs = 'both';
$this->Email->smtpOptions = array(
'port' =>'25',
'timeout' => '30',
'host' => 'ssl://smtp.gmail.com',
'username' => 'my_mail@gmail.com',
'password' =>'aaa',
);
$this->Email->delivery = 'smtp';
$this->Email->send();
}
但仍然出现错误。但是,我没有制作任何邮件服务器。那可以吗?
答案 0 :(得分:1)
使用前导下划线命名控制器功能是Cake的向后兼容方式,指定该功能应受保护,即该功能不应作为普通控制器操作访问。这意味着您无法使用网址FooController::_sendMail()
或任何其他网址访问/foo/_sendMail
。您应该看到这一点,IMO是一个非常好的错误消息:
UsersController中的私有方法
错误:无法直接访问
FooController::_sendMail()
。
删除前导下划线,就是这样。此问题与发送电子邮件无关。
答案 1 :(得分:1)
我觉得这与您的XAMPP配置有关:
尝试打开“php.ini”,它应该位于服务器文件中的某个位置。
在php.ini文件中搜索名为“SMTP”的属性。通常,您可以找到“SMTP = localhost”行。将localhost更改为ISP的smtp服务器名称。并且,还有一个名为“smtp_port”的属性应该设置为25.我已经在php.ini文件中设置了以下值。
SMTP = smtp.wlink.com.np
smtp_port = 25
重新启动apache服务器,以便重新加载PHP模块和属性。
尝试使用mail()函数发送邮件:
mail(“you@yourdomain.com”,”test subject”,”test body”);
如果收到以下警告:
Warning: mail() [function.mail]: “sendmail_from” not set in php.ini or custom “From:” header missing in C:\Program Files\xampp\htdocs\testmail.php on line 1
指定以下标题并尝试再次发送邮件:
$headers = ‘MIME-Version: 1.0′ . “\r\n”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1′ . “\r\n”;
$headers .= ‘From: sender@sender.com’ . “\r\n”;
mail(“you@yourdomain.com”,”test subject”,”test body”,$headers);
来源:http://roshanbh.com.np/2007/12/sending-e-mail-from-localhost-in-php-in-windows-environment.html
答案 2 :(得分:0)
尝试:
//load mail component in controller var $components = array('Mail'); //then do $this->Email->sendAs="html"; $this->Email->from="some@domain.com"; $this->Email->to="someone@domain.com"; $this->Email->subject="Your subject; $this->Email->send("Your message);
//检查cakephp手册以获取更多参考:http://book.cakephp.org/
答案 3 :(得分:0)
以下是使用CakePHP的电子邮件组件发送HTML电子邮件的示例代码
防爆控制器:EmailController.php
<?php
class EmailController extends AppController{
public $components=array('Email');
function send(){
//create an array of values to be replaced in email html template//
$emailValues=array('name'=>'MyName','phone'=>'MyPhone');
$this->set('emailValues',$emailValues);//pass to template //
$this->Email->to = 'to@address.com'; //receiver email id
$this->Email->subject = 'Subject Line';
$this->Email->replyTo = 'reply@address.com'; //reply to email//
$this->Email->from = 'SenderName<sender@address.com>'; //sender
$this->Email->template = 'sample';//email template //
$this->Email->sendAs = 'html';
if($this->Email->send()){
//mail send //
}
}
?>
现在在文件夹/ Views / Email / html /中创建电子邮件模板 即模板路径应该是 /Views/Email/html/sample.ctp
sample.ctp
<?php
Hi <?php echo $emailValues['name'];?> <br>
Thanks for sharing your phone number '<?php echo $emailValues['phone'];?>' .
<br>
?>