Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question