0

わかりました、以前に関連する質問をし、午前中ずっとこれに取り組んできたにもかかわらず、私はこれに真剣に取り組んでいます。

問題は基本的なものです。開始している新しいアプリから PHP を使用してメールを送信できません。メールホストは localhost で、認証は必要ありません。私が書いた、PHP メール機能も使用する以前のアプリをチェックアウトすることができ、それは機能します。php.ini ファイルはどちらの場合も同じであるため、どちらの場合も localhost を使用します。

動作中のアプリと新しいアプリの両方に、composer を使用してインストールされた swiftmailer がありますが、動作中の例とこの例の両方で、swiftmailer は使用されていません。

これが私が働きたい実際のコードです:

    // Determine headers
    $uid = md5(uniqid(time()));
    $headers = "From: " . $this->fromAddress . "  <" . $this->fromName . ">\r\n";
    $headers.= "Reply-To: " . $this->fromAddress . " <" . $this->fromName . ">\r\n";
    if ($this->cc != "") { $headers .= "CC: ".$this->cc."\r\n"; }
    if ($this->bcc != "") { $headers .= "BCC: ".$this->bcc."\r\n"; }
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
    $headers .= "This is a multi-part message in MIME format.\r\n";
    $headers .= "--" . $uid . "\r\n";
    $headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
    $headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $headers .= $this->body . "\r\n\r\n";

    // Optionally attach a file
    foreach ($this->attachments as $attachment) {
        $fileName = basename($attachment);
        $fileSize = filesize($attachment);
        $handle = fopen($attachment, "r");
        $content = fread($handle, $fileSize);
        fclose($handle);
        $content = chunk_split(base64_encode($content));

        $headers .= "--" . $uid . "\r\n";
        $headers .= "Content-Type: application/octet-stream; name=\"" . $fileName . "\"\r\n";
        $headers .= "Content-Transfer-Encoding: base64\r\n";
        $headers .= "Content-Disposition: attachment; filename=\"" . $fileName . "\"\r\n\r\n";
        $headers .= $content."\r\n\r\n";

        unlink($attachment);
    }

    // Conclude headers
    $headers .= "--".$uid."--";

    // Send the email
    $mail_sent = mail($this->toAddress,$this->subject,'',$headers);

    if (!$mail_sent) {
        throw new Exception('Email failed to send');
    }

このコードは、「E メールの送信に失敗しました」という例外をスローします。$this->toAddress が有効な電子メール アドレスであること、$this->subject が有効な件名であること、$this->fromAddress が有効な電子メール アドレスであること、および $this->body が有効な本文であることを確認できます。 、わずか数文字の長さです。

これを最も単純な例に要約しようとして、次のコードを試しました。

<?php
// The message
$message = "Line 1\r\nLine 2\r\nLine 3";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");

// Send
$result = mail('lowens@mycompany.com', 'My Subject', $message);

if (!$result) {
    error_log("fail");
}
?>

それは「失敗」を記録します。

localhost が機能することを確認するために、機能するコードを再チェックアウトしました。動作するコードは次のとおりです。

// Determine headers
$uid = md5(uniqid(time()));
$headers= "From: " . $login->getUser() . " <" . $login->getUserEmail() . ">\r\n";
$headers.= "Reply-To: " . $login->getUser() . " <" . $login->getUserEmail() . ">\r\n";
if ($bcc != "") { $headers .= "BCC: ".$bcc."\r\n"; }
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= $message."\r\n\r\n";

// Optionally attach a file
foreach ($attachedFilePaths as $attachedFilePath) {
    $fileName = basename($attachedFilePath);
    $fileSize = filesize($attachedFilePath);
    $handle = fopen($attachedFilePath, "r");
    $content = fread($handle, $fileSize);
    fclose($handle);
    $content = chunk_split(base64_encode($content));

    $headers .= "--".$uid."\r\n";
    $headers .= "Content-Type: application/octet-stream; name=\"".$fileName."\"\r\n"; // use different content types here
    $headers .= "Content-Transfer-Encoding: base64\r\n";
    $headers .= "Content-Disposition: attachment; filename=\"".$fileName."\"\r\n\r\n";
    $headers .= $content."\r\n\r\n";

    unlink($attachedFilePath);
}

$headers .= "--".$uid."--";

// Send the email
$mail_sent = @mail($toAddr,$subject,'',$headers);

// Save this email as a task
require_once('../classes/task.class.php');
$task = new Task();
$task->saveMailAsTask($CustomerId, $toAddr, $bcc, $subject, $message);

// This can be used to return a success or failure
if ($mail_sent) {
    redirect("http://$domainName/admin/index.php?task=account&event=viewdetails&id=$CustomerId&emailSentOK=true&d=emailResponse");
} else {
    redirect("http://$domainName/admin/index.php?task=account&event=viewdetails&id=$CustomerId&emailSentOK=false&d=emailResponse");
}

問題の原因であるメールホスト (localhost) と、PHP.ini ファイルを削除しました。私が推測する他の 2 つの問題の原因は、コード自体と、私が知らない未知の原因にあります。コードは私にはうまく見えます...

何を与える?そして、どうして mail() から適切なエラーメッセージが出ないのでしょうか?

4

2 に答える 2

0

問題は from フィールドでした。名前と住所の順番を逆にしてました。

正しくない:

$headers = "From: " . $this->fromAddress . "  <" . $this->fromName . ">\r\n";
$headers.= "Reply-To: " . $this->fromAddress . " <" . $this->fromName . ">\r\n";

正しい:

$headers = "From: " . $this->fromName . "  <" . $this->fromAddress . ">\r\n";
$headers.= "Reply-To: " . $this->fromName . " <" . $this->fromAddress . ">\r\n";
于 2013-05-06T19:21:54.567 に答える
0

確認事項:

php.ini で、sendmail_path が次の出力に設定されていることを確認します。

which sendmail

そうでない場合は、それに応じて設定します。

sendmail_path = '/usr/sbin/sendmail -t'

sendmail でメッセージを送信してみて、ブラックリストに登録されていないことを確認してください。

sendmail -v someone@someaddress.com "hello"

(CTR+D が送信され、受信サーバーからの応答が表示されます)

于 2013-05-06T16:57:27.867 に答える