简体   繁体   English

尝试使用 Swiftmailer 发送电子邮件

[英]Trying to send an email using Swiftmailer

I've been struggling for four days to send a validation email to a new user through a PHP function.我一直在努力通过 PHP 函数向新用户发送验证电子邮件四天。 Then i tried to send a simple email!然后我尝试发送一封简单的电子邮件!

Just a few clarifications needed:只需要澄清几点:

  • Must we put the name of the user or that of the full address on the Gmail server?我们必须在 Gmail 服务器上输入用户名或完整地址吗? Several sounds of bells ...几声钟声……
  • who can explain to me in a few words the \\ in the line "$ message = (new \\ Swift_Message ('Email Through Swift Mailer'))"?谁能用几句话向我解释“$ message = (new \\ Swift_Message ('Email Through Swift Mailer'))”行中的\\?
  • define ('EMAIL', 'constant;: Does this function work well with PDO and PHP 7? It seems that using const EMAIL_USERNAME = 'constant'; works better. define ('EMAIL', 'constant;: 这个函数在 PDO 和 PHP 7 中工作得很好吗?似乎使用 const EMAIL_USERNAME = 'constant'; 效果更好。

Until then this script still doesn't work:直到那时这个脚本仍然不起作用:

<?php

require 'dev.php';

require 'C:wamp64/www/dayou_php/vendor/autoload.php';


echo 'Envoi de mail avec Swift Mailer';

$subject = 'Mon premier email avec Swift Mailer';

$fromEmail = 'sixxerxxre@gmail.com';

$fromUser = '思而惹';

$body = '<!DOCTYPE html>
<html>
<head>
<title>Mon premier mail</title>
</head>
<body>
<h5>Hello SwiftMailer</h5>
</body>
</html>';


$transport = (new Swift_SmtpTransport(EMAIL_HOST, EMAIL_PORT))

->setUsername(EMAIL_USERNAME)

->setPassword(EMAIL_PASSWORD)

->setEncryption(EMAIL_ENCRYPTION) //For Gmail

;


// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);


// Create a message

 $message = (new Swift_Message($subject))

->setFrom([$fromEmail => $fromUser])

->setTo([EMAIL_USERNAME])

->setBody($body, 'text/html')

;


 // Send the message

$result = $mailer->send($message);

Variables are in another script dev.php :变量在另一个脚本dev.php 中

const EMAIL_HOST = 'smtp.gmail.com';

// autre port possibles : 465 pour ssl

const EMAIL_PORT = 587;
const EMAIL_USERNAME = 'my_username_without_@gmail';

const EMAIL_PASSWORD = 'my_pass_word';

// autre possibilité : ssl ou null

const EMAIL_ENCRYPTION = 'tls';

The error I get is about the constant my_username_without_@gmail :我得到的错误是关于常量my_username_without_@gmail

Address in mailbox given [seeergefaure] does not comply with RFC 2822, 3.6.2. in C:\wamp64\www\dayou_php\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Mime\Headers\MailboxHeader.php on line 355

we must put the name of the user or that of the full address on the gmail server?我们必须将用户名或完整地址放在 gmail 服务器上吗? several sounds of bells ...几声钟声……

You need to put full name with @gmail.com您需要在@gmail.com 中输入全名

who can explain to me in a few words the \\ in the line "$ message = (new \\ Swift_Message ('Email Through Swift Mailer'))"?谁能用几句话向我解释“$ message = (new \\ Swift_Message ('Email Through Swift Mailer'))”行中的\\?

It's about name spaces.它是关于名称空间的。 It means, that you use global namespace (it will be used from your autoloader).这意味着,您使用全局命名空间(它将从您的自动加载器中使用)。

define ('EMAIL', 'constant;: Does this function work well with PDO and php 7? it seems that using const EMAIL_USERNAME = 'constant'; works better. define ('EMAIL', 'constant;: 这个函数在 PDO 和 php 7 中工作得好吗?似乎使用 const EMAIL_USERNAME = 'constant'; 效果更好。

The basic difference between define and const is that const defines constants at compile time, whereas define() defines them at run time. define 和 const 之间的基本区别在于 const 在编译时定义常量,而 define() 在运行时定义它们。

Your code looks good.你的代码看起来不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM