R
R
Richard Kamsky2022-03-22 23:18:48
PHP
Richard Kamsky, 2022-03-22 23:18:48

How to specify sender name in phpmailer email?

Hello. I sent the form to the mail, but instead of the sender's name, it just says "I".
As I understood it, because my mail is specified in setFrom and addAddress. But if I conditionally write "[email protected]" in setFrom, the letter does not arrive.

<?php
  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;

  require 'PHPMailer/src/Exception.php';
  require 'PHPMailer/src/PHPMailer.php';

  $mail = new PHPMailer(true);
  $mail->CharSet = 'UTF-8';
  $mail->setLanguage('ru', 'PHPMailer/language/');
  $mail->isHTML(true);

  $mail->setFrom('моя почта', 'Test');
  $mail->addAddress('моя почта');
  $mail->Subject = 'Hello';

  $body = '<h1>This mail!</h1>';

  if (trim(!empty($_POST['name']))) {
    $body.='<p>Name: '.$_POST['name'].'</p>';
  }
  if (trim(!empty($_POST['email']))) {
    $body.='<p>Email: '.$_POST['email'].'</p>';
  }
  if (trim(!empty($_POST['url']))) {
    $body.='<p>Url: '.$_POST['url'].'</p>';
  }
  if (trim(!empty($_POST['comment']))) {
    $body.='<p>Comment: '.$_POST['comment'].'</p>';
  }

  if (!empty($_FILES['image']['tmp_name'])) {
    $filePath = __DIR__ . "/files/" . $_FILES['image']['name'];

    if (copy($_FILES['image']['tmp_name'], $filePath)) {
      $fileAttach = $filePath;
      $body.='<p>Image:</p>';
      $mail->addAttachment($fileAttach);
    }
  }

  $mail->Body = $body;
  
  if (!$mail->send()) {
    $message = 'Error';
  }

  $response = ['message' => $message];

  header('Content-type: application/json');
  echo json_encode($response);
?>

And in general, how to set up the design for the letter? At least the sender's mail instead of "I"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
ThunderCat, 2022-03-23
@ThunderCat

But if I conditionally write "[email protected]" in setFrom, the letter does not arrive.
So today it will not work practically anywhere, "buy security reason." At best, the email will end up in spam.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question