我编写了以下代码,使用Laravel控制器方法向指定的电子邮件地址发送电子邮件:
public function sendEmail(Request $request){
$question = $request->get('question');
$phone = $request->get('phone');
$from_email = $request->get('email');
$from_name = $request->get('name');
$to_email = 'XXX';
$to_name = 'XXX';
$data = array('name'=>"Sam Jose", "body" => $question . "\n" . $phone);
Mail::send(array(), $data, function($message) use ($to_name, $to_email, $from_email, $from_name) {
$message->to($to_email, $to_name)
->subject('Vraag | XXX');
$message->from($from_email, $from_name);
});
}
除此之外,我在我的.env文件中添加了以下内容:
MAIL_DRIVER=sendmail
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=XXX
MAIL_PASSWORD=XXX
MAIL_ENCRYPTION=ssl
如果我在本地主机上运行此代码,它会将邮件发送到指定的地址,但如果我在子域上运行它,则不会发送电子邮件,也不会显示任何错误消息。
MAIL_PORT=587
。 - kapitan