【问题标题】:Amazon SES RawMessage: Missing required header 'From'Amazon SES RawMessage:缺少必需的标头“发件人”
【发布时间】:2020-03-18 00:47:57
【问题描述】:

use PHPMailer\PHPMailer\PHPMailer;
use Aws\Ses\SesClient;
use Aws\Ses\Exception\SesException;
require 'vendor/autoload.php';

if(!function_exists("sendmailalexraw")){

function sendmailalexraw($email,$subject,$messages,$definesender)
{



// Replace sender@example.com with your "From" address. 
// This address must be verified with Amazon SES.
$sender = $definesender;           
$sendername = 'Alex';

// Replace recipient@example.com with a "To" address. If your account 
// is still in the sandbox, this address must be verified.
$recipient = $email;    

// Specify a configuration set.
$configset = 'ConfigSet';

// Replace us-west-2 with the AWS Region you're using for Amazon SES.
$region = 'eu-west-1'; 

$subject = $subject;

$htmlbody = <<<EOD
<html>
<head></head>
<body>
<h1>Hello!</h1>
<p>Please see the attached file for a list of customers to contact.</p>
</body>
</html>
EOD;

$textbody = <<<EOD
Hello,
Please see the attached file for a list of customers to contact.
EOD;

//// The full path to the file that will be attached to the email. 
$att = 'path/to/customers-to-contact.xlsx';

// Create an SesClient.
$client = SesClient::factory(array(
    'version'=> 'latest',     
    'region' => $region
));

// Create a new PHPMailer object.
$mail = new PHPMailer;

// Add components to the email.
$mail->setFrom($sender, $sendername);
$mail->addAddress($recipient);
$mail->Subject = $subject;
$mail->Body = $htmlbody;
$mail->AltBody = $textbody;
$mail->addAttachment($att);
$mail->addCustomHeader('X-SES-CONFIGURATION-SET', $configset);

// Attempt to assemble the above components into a MIME message.
if (!$mail->preSend()) {
    echo $mail->ErrorInfo;
} else {
    // Create a new variable that contains the MIME message.
    $message = $mail->getSentMIMEMessage();
}

// Try to send the message.
try {
    $result = $client->sendRawEmail([
        'RawMessage' => [
            'Data' => $messages
        ]
    ]);
    // If the message was sent, show the message ID.
    $messageId = $result->get('MessageId');
    echo("Email sent! Message ID: $messageId"."\n");
} catch (SesException $error) {
    // If the message was not sent, show a message explaining what went wrong.
    echo("The email was not sent. Error message: "
         .$error->getAwsErrorMessage()."\n");
}

}
}



$email='example@gmail.com';
$subject='abc';
$messages='xyz';
$definesender='info@verifieddomain.net';
sendmailalexraw($email,$subject,$messages,$definesender);


?>


我正在尝试使用 Amazon SES 发送 RawMessage,但我得到了:

电子邮件未发送。错误消息:缺少必需的标头“发件人”。

发件人我使用它已经过验证,我的 Amazon SES 它是活动的(在 sendbox 之外)。

我需要以原始消息的形式发送,以便为我正在发送的电子邮件创建取消订阅选项。正如我从文档中读到的,它必须是原始电子邮件才能添加此参数。谢谢!

【问题讨论】:

  • 在$message = $mail-&gt;getSentMIMEMessage(); 之后,打印$message 包含的内容并进行目视检查。它是否具有格式正确的 From: ... 标头?该错误表明它没有。

标签: php amazon-web-services amazon-ses


【解决方案1】:

您必须对消息进行 base64 编码:

$result = $client->sendRawEmail([
    'RawMessage' => [
        'Data' => base64_encode($messages)
    ]
]);

【讨论】:

    【解决方案2】:

    由于您使用的是 AWS Ses,因此您可以这样做:

        define('SENDER', 'Your name<a@a.com>');
        define('RECIPIENT', 'b@b.com');
        define('CCLIST', 'c@c.com');
    
        define('REGION','your region');
    
        define('SUBJECT','Your subject goes here');
        $bodytext = "<h2>Your body goes here.</h2>" . PHP_EOL;
        $bodytext .= "<p>Append as many rows as you want</p>";
    
        define('BODY',$bodytext);
    
    
        $client = SesClient::factory(array(
                'version'=> 'latest',
                'region' => 'your region'
        ));
    
        $request = array();
        $request['Source'] = SENDER;
        $request['Destination']['ToAddresses'] = array(RECIPIENT);
        $request['Destination']['CcAddresses'] = array(CCLIST);
        $request['Message']['Subject']['Data'] = SUBJECT;
        $request['Message']['Body']['Html']['Data'] = BODY;
    
        try {
            $result = $client->sendEmail($request);
            $messageId = $result->get('MessageId');
            echo("Email successfully sent! Message ID: $messageId"."\n");
    
        } catch (Exception $e) {
            echo("The email was not sent. Error message: ");
            echo($e->getMessage()."\n");
        }
    

    【讨论】:

      猜你喜欢
      • 2012-06-18
      • 1970-01-01
      • 2021-09-18
      • 2012-06-22
      • 2018-02-03
      • 1970-01-01
      • 2014-05-01
      • 2019-03-17
      • 1970-01-01
      相关资源
      最近更新 更多