PHP

You can send emails in several ways

  • Natively
  • With a dedicated library

Natively

PHP has the function "Mail" natively to send email. The sent are then made through the Apache setting in the php.ini. For more information about editing the php.ini, you can read this article.

Here an code example

				$expediteur   = "from@example.com";
				$destinataire = "to@example.com";
				$message_html = "HTML message";
				$subject      = "your subject";

				mail($destinataire, $subject, $message_html,
					 "From: $expediteur\r\n".
						"Content-Type: text/html; charset=\"utf-8\"\r\n");
			

With a dedicated library

The use of libraries simplifies writing code for sending email. They are for the most free and open-source. In the case of PHP, we recommend SwiftMailer. This library is one of the most popular and mostly efficient.

  • Start by installing SwiftMailer. You can perform this operation with Composer or make clone from Github account
  • Include SwiftMailer in your development
  • Fill in the following example to your needs. For more information, please read the SwiftMailer documentation
					
					/**
					 * define your settings
					*/
					
					require __DIR__ . '/vendor/autoload.php'; // SwiftMailer library
					
					$username="your tipimail username";
					$password="your tipimail password";


					$transport = Swift_SmtpTransport::newInstance('smtp.tipimail.com', 25);
					/*
					 * if you want to use tls
					 * $transport = Swift_SmtpTransport::newInstance('smtp.tipimail.com', 587, "tls");
					*/

					//set the credential to send email
					$transport->setUsername($username);
					$transport->setPassword($password);
					$swift = Swift_Mailer::newInstance($transport);

					//set your email
					$message_html = "your html message";
					$message_text = "your text message";
					$subject = "your subject";
					$expediteur = array('from@example.com' =>'From');
					$destinataire = array(
						'to@example.com' => 'To'
					);

					$message->setSubject($subject)
							->setFrom($expediteur)
							->setTo($destinataire)
							->setBody($html, 'text/html')
							->addPart($text, 'text/plain');


					/*
					 * If you want to add headers
					 * $headers = $message->getHeaders();
					 * $headers->addTextHeader('X-TM-TRACKING', '{"html":{"open" : 1, "click": 0},"text":{"click": 1}}');
					*/

					if ($recipients = $swift->send($message, $failures)) {
						echo 'Message successfully sent!';
					} else {
						echo "There was an error:\n";
						print_r($failures);
					}