D
D
Denis8092020-05-03 13:15:40
PHP
Denis809, 2020-05-03 13:15:40

How to send a file to the E-mail entered in the form?

Hello, there is a form with one field and a send button, after entering the email field and pressing the Send button, an email with a file should be sent to the entered E-mail (.docx or .pdf extension file).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PQR, 2020-05-06
@PQR

To send a file to E-mail from PHP, you can use the standard mail sending functions, but it is more convenient to use a library, for example, Swfitmailer: https://swiftmailer.symfony.com/docs/introduction.html
The sending code can be something like this:

// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.gmail.com', 993))
  ->setUsername('[email protected]')
  ->setPassword('your password')
;

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

// Create a message
$message = (new Swift_Message('Тема сообщения'))
  ->setFrom(['[email protected]' => 'John Doe'])
  ->setTo($_POST['to']) // на форме должно быть поле <input name="to">
  ->attach(
    Swift_Attachment::fromPath('/path/to/file.pdf')
  );

// Send the message
$result = $mailer->send($message);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question