In this post we are going to explain how to send emails using PHPMailer from a 1and1 server.
On the one hand, we have a config.php file where we configure the main parameters.
1 2 3 4 5 6 7 8 9 10 |
define('EMAIL_CHARSET', 'UTF-8'); define('SMTP_SERVER', 'smtp.1and1.com'); define('EMAIL_USERNAME', '<your 1and1 account>'); define('EMAIL_PASSWORD', '<your password>'); define('EMAIL_PORT', '25'); define('EMAIL_FROM', '<your from email>'); define('EMAIL_FROM_NAME', '<your from name email>'); define('EMAIL_REPLY', '<your reply email>'); define('EMAIL_REPLY_NAME', '<your reply name email>'); |
On the other hand, here you have a sample of a php class using PHPMailer for sending emails.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php require 'config.php'; require 'PHPMailer/PHPMailerAutoload.php'; class EMail { public static function sendMail($to, $subject, $message, $message_plain) { $mail = new PHPMailer; $mail->CharSet = EMAIL_CHARSET; $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = SMTP_SERVER; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = EMAIL_USERNAME; // SMTP username $mail->Password = EMAIL_PASSWORD; // SMTP password $mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted $mail->From = EMAIL_FROM; $mail->FromName = EMAIL_FROM_NAME; $mail->addAddress($to); // Add a recipient $mail->addReplyTo(EMAIL_REPLY, EMAIL_REPLY_NAME); // $mail->addCC('cc@example.com'); // $mail->addBCC('bcc@example.com'); $mail->WordWrap = 80; // Set word wrap to 80 characters // $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments // $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name $mail->isHTML(true); // Set email format to HTML $mail->Port = EMAIL_PORT; $mail->Subject = $subject; $mail->Body = $message; if (!empty($message_plain)) { $mail->AltBody = $message_plain; } if(!$mail->send()) { return false; } else { return true; } } } ?> |