Отправка писем в PHP
Sendmail в PHP возможен с помощью всего одной строки кода. PHP содержит встроенные почтовые функции для отправки почты.
PHP действительно экономит наше время благодаря своим встроенным функциям.
Простая текстовая почта с PHP mail()
<?php mail( string $recipient_email, string $subject, string $message, array|string $headers = [], string $additional_params = "" ) ?>
Sendmail в PHP для отправки открытого текста
<?php $to = 'recipient@email.com'; $subject = 'Mail sent from sendmail PHP script'; $message = 'Text content from sendmail code.'; // Sendmail in PHP using mail() if (mail($to, $subject, $message,)) { echo 'Mail sent successfully.'; } else { echo 'Unable to send mail. Please try again.'; } ?>
Код PHP Sendmail для отправки содержимого HTML
<?php $to = 'recipient@email.com'; $subject = 'Mail sent from sendmail PHP script'; $from = 'test@testmail.com'; $headers = "From: $from"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=UTF-8\r\n"; $message = '<p><strong>Sendmail in PHP with HTML content. </strong></p>'; if (mail($to, $subject, $message, $headers)) { echo 'Mail sent successfully.'; } else { echo 'Unable to send mail. Please try again.'; } ?>
Sendmail в PHP для прикрепленых файлов
<?php $file = "example.txt"; $to = 'recipient@email.com'; $subject = 'Mail sent from sendmail PHP script'; $content = file_get_contents($file); $encodedContent = chunk_split(base64_encode($content)); $divider = md5(time()); $headers = "From: TestSupport <example@email.com>\r\n"; $headers .= "Content-Type: multipart/mixed; boundary=\"" . $divider . "\"\r\n"; $headers .= "Content-Transfer-Encoding: 7bit\r\n"; // prepare mail body with attachment $message = "--" . $divider. "\r\n"; $message .= "Content-Type: application/octet-stream; name=\"" . $file . "\"\r\n"; $message .= "Content-Transfer-Encoding: base64\r\n"; $message .= "Content-Disposition: attachment\r\n"; $message .= $encodedContent. "\r\n"; $message .= "--" . $divider . "--"; //sendmail with attachment if (mail($to, $subject, $message, $headers)) { echo 'Mail sent successfully.'; } else { echo 'Unable to send mail. Please try again.'; } ?>
Sendmail при отправке формы
Код
body { font-family: Arial; width: 550px; } .response-ribbon { padding: 10px; background: #ccc; border: #bcbcbc 1px solid; margin-bottom: 15px; border-radius: 3px; } input, textarea { padding: 8px; border: 1px solid #ccc; border-radius: 5px; } #Submit-btn { background: #1363cc; color: #FFF; width: 150px; } #email-form { border: 1px solid #ccc; padding: 20px; } .response-ribbon { }
<?php if (isset($_POST["submit_btn"])) { $to = "recipient@email.com"; $subject = 'Mail sent from sendmail PHP script'; $from = $_POST["email"]; $message = $_POST["msg"]; $headers = "From: $from"; // Sendmail in PHP using mail() if (mail($to, $subject, $message, $headers)) { $responseText = 'Mail sent successfully.'; } else { $responseText = 'Unable to send mail. Please try again.'; } } ?> <?php if(!empty($responseText)) { ?> <div class="response-ribbon"><?php echo $responseText; ?></div> <?php } ?> <form id="email-form" name="email-form" method="post" action=""> <table width="100%" border="0" align="center" cellpadding="4" cellspacing="1"> <tr> <td> <div class="label">Name:</div> <div class="field"> <input name="name" type="text" id="name" required> </div> </td> </tr> <tr> <td><div class="label">E-mail:</div> <div class="field"> <input name="email" type="text" id="email" required> </div></td> </tr> <tr> <td><div class="label">Message:</div> <div class="field"> <textarea name="msg" cols="45" rows="5" id="msg" required></textarea> </div></td> </tr> <tr> <td> <div class="field"> <input name="submit_btn" type="submit" id="submit-btn" value="Send Mail"> </div> </td> </tr> </table> </form>
Результат
Отправка почты в PHP через SMTP
<?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; require_once __DIR__ . '/vendor/phpmailer/phpmailer/src/Exception.php'; require_once __DIR__ . '/vendor/phpmailer/phpmailer/src/PHPMailer.php'; require_once __DIR__ . '/vendor/phpmailer/phpmailer/src/SMTP.php'; $mail = new PHPMailer(true); $mail->SMTPDebug = 0; $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->Username = ""; $mail->Password = ""; $mail->SMTPSecure = "ssl"; $mail->Port = 465; $mail->From = "test@testmail.com"; $mail->FromName = "Full Name"; $mail->addAddress("recipient@email.com", "recipient name"); $mail->isHTML(true); $mail->Subject = "Mail sent from php send mail script."; $mail->Body = "<i>Text content from send mail.</i>"; $mail->AltBody = "This is the plain text version of the email content"; try { $mail->send(); echo "Message has been sent successfully"; } catch (Exception $e) { echo "Mailer Error: " . $mail->ErrorInfo; } ?>