Cara Cara

untung99.homes: Sending Emails in PHP 2023 Guide with Examples


Untung99 menawarkan beragam permainan yang menarik, termasuk slot online, poker, roulette, blackjack, dan taruhan olahraga langsung. Dengan koleksi permainan yang lengkap dan terus diperbarui, pemain memiliki banyak pilihan untuk menjaga kegembiraan mereka. Selain itu, Untung99 juga menyediakan bonus dan promosi menarik yang meningkatkan peluang kemenangan dan memberikan nilai tambah kepada pemain.

Berikut adalah artikel atau berita tentang Harian untung99.homes dengan judul untung99.homes: Sending Emails in PHP 2023 Guide with Examples yang telah tayang di untung99.homes terimakasih telah menyimak. Bila ada masukan atau komplain mengenai artikel berikut silahkan hubungi email kami di koresponden@untung99.homes, Terimakasih.

Technologies advance – so does the world every day. But some things stay forever. Like sending emails with PHP. You might be skeptical and thinking even in 2023? Yes, even in 2023. W3Techs reports that, as of January 2023, “PHP is used by 77.5% of all the websites whose server-side programming language we know.” 

That said, let’s check out this tutorial that will help you send emails from PHP today.

Send mail from PHP: three important things that you need to know right away

One, there is a built-in PHP mail() function and it is quite simple.

Two, although PHP’s built-in mail function () is very simple, it provides limited functionality for sending emails. You won’t be able to add attachments to your email, and building a beautiful HTML template with embedded images will be a tricky task as well. 

Three, the PHP mail function () sends emails from your website, which may cause issues with deliverability due to security concerns such as suspicion of spam and blacklisting. PHP mail() also does not usually allow you to send mail using an SMTP server externally, and it does not support SMTP authentication.

What options do you have to send emails in PHP?

There are four popular ones.

  1. A built-in PHP mail() function;
  2. PHPMailer;
  3. Symfony Mailer;
  4. Third-party email sending services like Mailtrap, Sendgrid, Mailgun, etc.

We’ll go step-by-step through all of them.

PHP built-in mail function () 

The PHP function mail() is a built-in function for sending emails in PHP, but it has some limitations and potential drawbacks that make it less popular than other email sending options.

Here are some reasons why mail() is not commonly used for sending emails in PHP:

– Deliverability issues: The mail() function relies on the local mail server configuration, which can result in emails being flagged as spam or rejected by some email providers. This can cause deliverability issues and make it difficult to ensure that emails are being delivered to recipients.

– Lack of features: The mail() function does not support many advanced features such as SMTP authentication, email tracking, and attachments. This can limit the functionality of your email sending application.

– Security concerns: The mail() function can be vulnerable to email injection attacks, where an attacker can inject additional headers into the email message, potentially compromising the security of the email system.

While the mail() function can be a simple and lightweight option for sending emails in some cases, it is not recommended for production use and is generally considered less robust than other email sending options available in PHP.

Mail() is a wrapper on top of the sendmail utility, so sendmail has to be installed in the system first.

Here’s what you can do with PHP built-in mail function(): 

  • create simple HTML/text messages without attachments and images
  • send them to one or several recipients
  • include additional headers to the message
  • add additional parameters to the `sendmail` command

Keep in mind that when you send emails with mail(), you may come across some grave deliverability issues. The messages dispatched will not benefit from the SPF and DKIM setup on your domain, therefore, the messages will likely be treated as spam by the receiving MTA (Mail Transfer Agent). Thus, the overall deliverability of email messages sent via PHP mail () is not guaranteed. Moreover, you won’t receive bounce back messages if there is a delivery failure.

If you are still committed to the PHP built-in mail function() and are ready to accept the challenge, let’s take a look at the basic PHP script syntax and its main parameters. 

Syntax and parameters

The PHP mail syntax is pretty simple:

It uses the following parameters: 

  • “$to” = your message recipient(s). The email address format may be user@example.com or User . In general, it needs to comply with RFC 2822. It is mandatory.
  • “$subject” = your message’s subject. 
  • “$message” = the body of your message. Lines should be separated with a CRLF (\r\n). Each line should not exceed 70 characters.
  • “[$headers]” = the mandatory one is the “from” header: it must be specified, otherwise, you will receive an error message like Warning: mail(): “sendmail_from” not set in php.ini or custom “From:” header missing.

The additional headers indicate other recipients or copies of your message like CC or BCC. They can be an array where the key is a header name and the value is a header value. Or they can be a string. In this case, headers should be separated with a CRLF (\r\n).

  • [$parameters] = for specifying the additional parameters defined in the sendmail_path configuration setting.

For more details and additional parameters, refer to the PHP documentation. 

Sending HTML email using PHP mail() function

The body of the message can be written in HTML. However, as we’ve mentioned above, it should be simple. 

In the PHP mail function(), the HTML message part will look like this:

// Message
$message = '

  

Here are the cases requiring your review in December:

Case titleCategoryStatusDue date
Case 1DevelopmentpendingDec-20
Case 1DevOpspendingDec-21
';

It’s important to remember that to send HTML mail, you need to set the Content-type header:
$headers[‘MIME-Version’] = ‘MIME-Version: 1.0’;
$headers[‘Content-type’] = text/html; charset=iso-8859-1′;

Simple Transmission Protocol (SMTP)

Where do I specify the SMTP settings? This is a fair question. Go to the PHP file installation folder and configure them in the php.ini file. But this will only work for localhost or XAMPP like solutions because as we have already mentioned, the PHP mail() function does not support SMTP authentication and doesn’t allow sending messages via external servers. 

To send mail without SMTP, you could also refer to a third party-service like Mailtrap, SendGrid, or Mailgun. 

Sending multiple emails

To send your message to multiple recipients, specify their email addresses in the “$to” =  parameter separating them with a comma(-s).  

It’s the only suitable method with a native mail() function. If you need to send a large volume of messages in a loop, try an external mailing package like Symfony Mailer. Or the third-party solution.

There are also a couple of ways to do this. And this topic needs a special tutorial. Whether you run a WordPress website or need a simple contact form, check out this encoding for a PHP contact form with Google reCaptcha. 

We explain how it works in detail, plus we go through many other ways to create contact forms in PHP in our special guide on PHP code to send email from a contact form. Tune in.

success) {
    $errors[] = 'Recaptcha failed';
  }
  if (empty($name)) {
    $errors[] = 'Name is empty';
  }
  if (empty($email)) {
    $errors[] = 'Email is empty';
  }  else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $errors[] = 'Email is invalid';
  }
  if (empty($message)) {
    $errors[] = 'Message is empty';
  }

  if (!empty($errors)) {
    $allErrors = join('
', $errors); $errorMessage = "

{$allErrors}

"; } else { $toEmail = 'mailtrap.club@gmail.com'; $emailSubject = 'New email from your contaсt form'; // Create a new PHPMailer instance $mail = new PHPMailer(true); try { // Configure the PHPMailer instance $mail->isSMTP(); $mail->Host = 'live.smtp.mailtrap.io'; $mail->SMTPAuth = true; $mail->Username = ''; $mail->Password = ''; $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $mail->Port = 587; // Set the sender, recipient, subject, and body of the message $mail->setFrom($email); $mail->addAddress($toEmail); $mail->Subject = $emailSubject; $mail->isHTML(true); $mail->Body = "

Name: {$name}

Email: {$email}

Message: {$message}

"; // Send the message $mail->send(); $successMessage = "

Thank you for contacting us :)

"; } catch (Exception $e) { $errorMessage = "

Oops, something went wrong. Please try again later

"; } } } function sanitizeInput($input) { $input = trim($input); $input = stripslashes($input); $input = htmlspecialchars($input, ENT_QUOTES, 'UTF-8'); return $input; } ?>

How to send emails with PHPMailer?

PHPMailer is the classic and the most popular email sending library for PHP. It deserves a separate article and a tutorial. To get a detailed overview of the PHP code in PHPMailer read our guide on how to send emails using PHPMailer.

What you can do with PHPMailer

  • create complex HTML/multipart templates
  • add attachments and embedded images
  • send email from authenticated SMTP. 

PHPMailer is protected against header injection attacks and automatically validates emails. 

Now let’s send, say, a hotel booking confirmation with PHPMailer. The code requires the Symfony Mailer library to be installed through Composer (follow the manual for installation):

isSMTP();
$mail->Host = 'live.smtp.mailtrap.io';
$mail->SMTPAuth = true;
$mail->Username = '1a2b3c4d5e6f7g';
$mail->Password = '1a2b3c4d5e6f7g';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 2525;

$mail->setFrom('confirmation@hotel.com', 'Your Hotel');
$mail->addAddress('me@gmail.com', 'Me');
$mail->Subject = 'Thanks for choosing Our Hotel!';
// Set HTML 
$mail->isHTML(TRUE);
$mail->Body = 'Hi there, we are happy to
confirm your booking. Please check the document in the attachment.'; $mail->AltBody = 'Hi there, we are happy to confirm your booking. Please check the document in the attachment.'; // add attachment // just add the '/path/to/file.pdf' $attachmentPath = './confirmations/yourbooking.pdf'; if (file_exists($attachmentPath)) { $mail->addAttachment($attachmentPath, 'yourbooking.pdf'); } // send the message if(!$mail->send()){ echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; }

Pay attention: for this email message, we use the following piece of code

$mail->isSMTP();
$mail->Host = 'live.smtp.mailtrap.io';
$mail->SMTPAuth = true;
$mail->Username = '1a2b3c4d5e6f7g';
$mail->Password = '1a2b3c4d5e6f7g';
$mail->SMTPSecure = 'tls';
$mail->Port = 528;

to set the mail server, SMTP port, and enable email authentication. 

PHP mailing packages

As we have already mentioned, the native PHP mail() function is not designed for creating email templates and sending a large volume of emails. Moreover, it causes some serious deliverability issues. So we would recommend using some external mailing packages instead. For 2022, Pear:: Mail and Swift Mailer are not a thing anymore (as they are outdated now), so the obvious choice would be Symfony Mailer. 

Symfony Mailer & Mime components establish a solid system for creating and sending emails – complete with support for multipart messages, Twig integration, CSS inlining, file attachments, etc. We have created a step-by-step guide for sending emails in Symfony: from installation to transport setup, and creating and sending messages. Feel free to use it to your advantage. Do not hesitate to turn to the current version documentation for specific details.

Mind that here we show a standalone usage of this package, and it’s already integrated into Symfony and into the Laravel framework. So using it is simpler while working with the frameworks altogether.

setUsername('155d3ee688e2b6')
                ->setPassword('1ca3b8a278cf32');

$mailer = new Mailer($transport); 

$email = (new Email())
            ->from('hello@example.com')
            ->to('you@example.com')
            ->subject('Time for Symfony Mailer!')
            ->text('Sending emails is fun again!')
            ->html('

See Twig integration for better HTML integration!

'); $mailer->send($email);

Sending emails in PHP with third-party email services 

In case your application requires automated email sending and email analytics, it is always better to go for third-party email services. Why? It is faster, it is less labor and resource-consuming, and let’s be honest: it gives you more control over email infrastructure.

There are many third-party email services you can integrate your app with, such as Sendgrid, Mailgun, etc. But, the Mailtrap Email Delivery Platform might arguably be the one you actually need.

Using Mailtrap to send emails with PHP

If you’re looking for a reliable and hassle-free email sending service with PHP, you should probably consider Mailtrap Email Sending. It’s an Email API/SMTP service that allows for up to 60 days of email logs for improved troubleshooting (saving your hard data during this time span). 

Dashboards with a clear snapshot of the state of your email infrastructure are always at hand for analytics. 

Another advantage of Mailtrap Email Sending is our timely email deliverability alerts that give you greater control over your email infrastructure and domain authority. This means that if anything goes wrong with your deliverability, you get an alert with a color-coded table plus insights on where to inspect a deliverability issue. Yet, Mailtrap sends you regular weekly reports on your deliverability performance to keep you up to date with your email sending and deliverability performance.

Mailtrap API integration for PHP

Go to the Sending Domains tab, choose API/SMTP Integration, go for the API one,  and select PHP from the library.
You’ll find the necessary credentials right there, just copy and paste them:

 'https://send.api.mailtrap.io/api/send',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>'{"from":{"email":"mailtrap@mailtrap.club","name":"Mailtrap Test"},"to":[{"email":"viktoriia.ivanenko@railsware.com"}],"subject":"You are awesome!","text":"Congrats for sending test email with Mailtrap!","category":"Integration Test"}',
    CURLOPT_HTTPHEADER => array(
        'Authorization: Bearer ',
        'Content-Type: application/json'
    ),
));

$response = curl_exec($curl);

if ($response === false) {
    echo 'cURL Error: ' . curl_error($curl);
}

curl_close($curl);
echo $response;

Now you can start sending.

Try Sending Emails in PHP with Mailtrap for Free

Server configuration

Here is how your SMTP server configuration to start sending with PHP would look like in Mailtrap (go to SMTP credentials at Mailtrap interface, then just copy and paste them):

isSMTP();
$mail->Host = 'live.smtp.mailtrap.io';
$mail->SMTPAuth = true;
$mail->Username = 'api';
$mail->Password = '********2ff0';
$mail->SMTPSecure = 'tls';
$mail->Port = 528;

Common issues with PHP mails

We’ll cover only the basic issues here. In case you have a specific problem not mentioned here, refer to the PHP documentation or the sending service providers’ support.

Wrongly displayed characters

Let’s say the email doesn’t render German characters (umlauts) properly.

Then, you need to set the Content-Type and the Charset in the headers of the email. The Content-Type specifies the type of content in the email, and the Charset defines the character encoding used in the email. In this case, the Charset is set to UTF-8, which supports a wide range of characters and is a popular choice for character encoding:

Mostly, UTF-8 is your best choice.

Then, add the Content-Transfer-Encoding header, which specifies the encoding mechanism used to transfer data over the internet. Base64 is a popular encoding mechanism for email messages because it ensures that the data is transmitted safely across different email systems and servers:

Now, you can use both UTF-8 and Base64 to properly encode the subject line or the recipient name if they require specific characters.

';
?>

And don’t forget to Base64 encode the email message too:

Anyway, troubleshooting and testing need some special attention and in-depth analysis. Here, you can find out more about how to test emails sent from PHP.

Final considerations

In this article, we have described the basic PHP email sending principles, syntax, and parameters. We’ve also reviewed the main ways of sending emails with PHP: its built-in mail function. 

However, in many situations, you might consider using a third-party mail service integration like Mailtrap to save time, money, and effort.