ソースを参照

エラーがなくなったけど、まだメールが届かない

pull/16/head
コミット
7de4bc4e75
  1. 26
      app/Http/Controllers/Home/Contact.php
  2. 67
      app/Http/Controllers/MailerController.php
  3. 23
      app/Mail/ContactNotifyMail.php
  4. 2
      composer.json
  5. 10
      resources/views/emails/notify/contact.blade.php

26
app/Http/Controllers/Home/Contact.php

@ -7,6 +7,7 @@ use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\MailerController;
use App\Mail\ContactNotifyMail;
class Contact {
@ -50,6 +51,7 @@ class Contact {
$this->field['adr'] = $r->adr;
$this->field['kenmei'] = $r->kenmei;
$this->field['cat'] = $r->cat;
$this->field['catname'] = $r->cat;
$this->field['gpg'] = !isset($r->gpg) || is_null($r->gpg) || $r->gpg == '' ? null : file_get_contents($r->file('gpg'));
$this->field['bunsyo'] = $r->bunsyo;
$this->field['ruleapply'] = isset($r->ruleapply);
@ -60,7 +62,7 @@ class Contact {
// メールアドレス
if (!isset($this->field['adr']) || is_null($this->field['adr']) || $this->field['adr'] == '') $err[] = 'メールアドレスをご入力下さい。';
else if (!filter_var($this->field['adr'], FILTER_VALIDATE_EMAIL)) $err[] = 'メールアドレスを正しくご入力下さい。';
$filename = trim($this->field['adr']).'.key';
$this->field['filename'] = trim($this->field['adr']).'.key';
// 件名
if (!isset($this->field['kenmei']) || is_null($this->field['kenmei']) || $this->field['kenmei'] == '') $err[] = '件名をご入力下さい。';
@ -76,8 +78,8 @@ class Contact {
$gpg = new \gnupg();
$info = $gpg->import($this->field['gpg']);
$gpg->addencryptkey($info['fingerprint']);
Storage::disk('public')->put($filename, $this->field['gpg']);
$path = Storage::disk('public')->path($filename);
Storage::disk('public')->put($this->field['filename'], $this->field['gpg']);
$path = Storage::disk('public')->path($this->field['filename']);
$verifygpg = explode("\n", $this->run('gpg --dry-run --import '.$path))[0];
if (!str_contains($verifygpg, '処理数の合計: 1') && !str_contains($verifygpg, 'Total number processed: 1')) $err[] = $verifygpg;
}
@ -91,27 +93,35 @@ class Contact {
if (!$this->field['ruleapply']) $err[] = 'ルールを同意して下さい。';
if (!empty($err)) {
if (isset($this->field['gpg']) && !is_null($this->field['gpg']) && $this->field['gpg'] != '') Storage::disk('public')->delete($filename);
if (isset($this->field['gpg']) && !is_null($this->field['gpg']) && $this->field['gpg'] != '') Storage::disk('public')->delete($this->field['filename']);
return view('pages.site.contact', ['field' => $this->field, 'err' => $err]);
}
// カテゴリはDMCA報告、営業、税金、法律、又は無駄な話を選択したら、いつでも送信せず失敗します。
if (str_contains($this->field['cat'], 'scam')) {
Storage::disk('public')->delete($filename);
Storage::disk('public')->delete($this->field['filename']);
return view('pages.site.contact', ['field' => $this->field, 'err' => ['送信に失敗しました。数時間後もう一回送信してみて下さい。']]);
}
foreach ($this->field['cats'] as $i => $c) {
if ($this->field['cat'] == $i) $this->field['catname'] = $c;
}
// メールを送る
try {
Mail::to(config('mail.from.address'))->send(new ContactNotifyMail($this->field, $filename));
$mail = new MailerController();
if (!$mail->compose($this->field)) {
Storage::disk('public')->delete($this->field['filename']);
return view('pages.site.contact', ['field' => $this->field, 'err' => ['送信に失敗しました。数時間後もう一回送信してみて下さい。']]);
}
} catch (\Throwable $e) {
Storage::disk('public')->delete($filename);
Storage::disk('public')->delete($this->field['filename']);
$this->field['bunsyo'] = $r->bunsyo;
Log::critical($e);
return view('pages.site.contact', ['field' => $this->field, 'err' => ['送信に失敗しました。数時間後もう一回送信してみて下さい。']]);
}
Storage::disk('public')->delete($filename);
Storage::disk('public')->delete($this->field['filename']);
$this->field['kenmei'] = '';
$this->field['adr'] = '';
$this->field['cat'] = '';

67
app/Http/Controllers/MailerController.php

@ -0,0 +1,67 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Support\Facades\Log;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class MailerController extends Controller {
public function __construct() {}
public function compose ($val) {
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 1;
$mail->isSMTP();
$mail->Host = config('mail.mailers.smtp.host');
$mail->Port = config('mail.mailers.smtp.port');
$mail->SMTPSecure = config('mail.mailers.smtp.encryption');
$mail->SMTPAuth = true;
$mail->From = config('mail.owner.address');
$mail->Username = config('mail.mailers.smtp.username');
$mail->Password = config('mail.mailers.smtp.password');
$mail->AddAddress(config('mail.owner.address'));
$mail->addReplyTo(config('mail.owner.address'), config('mail.owner.name'));
$mail->SMTPOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
];
if (isset($val['attach'])) {
for ($i=0; $i < count($val['attach']); $i++) {
$mail->AddAttachment($val['attach'][$i], $val['attachname'][$i]);
}
}
if (isset($val['gpg'])) {
$mail->AddStringAttachment($val['gpg'], $val['gpgname']);
}
$mail->isHTML(false);
$mail->Subject = '【'.$val['catname'].'】'.$val['kenmei'];
$mail->Body = "メール:".$val['adr']."\n\n".$val['bunsyo'];
if (!$mail->send()) {
Log::critical('Mailer error: ' . $mail->ErrorInfo);
return false;
}
} catch (Exception $e) {
Log::critical($e);
return false;
}
return true;
}
}

23
app/Mail/ContactNotifyMail.php

@ -1,23 +0,0 @@
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class ContactNotifyMail extends Mailable {
use Queueable, SerializesModels;
private $form;
private $file;
public function __construct ($form, $file) {
$this->form = $form;
$this->file = $file;
}
public function build () {
return $this->from($this->form['adr'])->subject($this->form['kenmei'])->markdown('emails.notify.contact', ['form' => $this->form])->attachFromStorage('public/'.$this->file);
}
}

2
composer.json

@ -14,7 +14,7 @@
"guzzlehttp/guzzle": "^7.4.0",
"laravel/framework": "^8.74.0",
"laravel/tinker": "^2.6.2",
"phpmailer/phpmailer": "^6.5.3",
"phpmailer/phpmailer": "^6.5",
"predis/predis": "^1.1.9"
},
"require-dev": {

10
resources/views/emails/notify/contact.blade.php

@ -1,10 +0,0 @@
@component('mail::message')
# {{ $form['kenmei'] }}
{{ $form['adr'] }}<br />
{{ $form['cat'] }}<br /><br />
□□□□□□□□□□□□□□□□□□□□<br /><br />
{{ nl2br($form['bunsyo']) }}
@endcomponent
読み込み中…
キャンセル
保存