Last active
February 10, 2020 10:33
-
-
Save mazhar266/68af1f4ee2604f36a7645cf326dfd2f9 to your computer and use it in GitHub Desktop.
SMTP Mail Send Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require('vendor/autoload.php'); | |
// Import PHPMailer classes into the global namespace | |
// These must be at the top of your script, not inside a function | |
use PHPMailer\PHPMailer\PHPMailer; | |
use PHPMailer\PHPMailer\SMTP; | |
use PHPMailer\PHPMailer\Exception; | |
//PHPMailer Object | |
$mail = new PHPMailer(true); | |
// SMTP | |
//Enable SMTP debugging. | |
$mail->SMTPDebug = 3; | |
//Set PHPMailer to use SMTP. | |
$mail->isSMTP(); | |
//Set SMTP host name | |
$mail->Host = "-- server --"; | |
//Set this to true if SMTP host requires authentication to send email | |
$mail->SMTPAuth = true; | |
//Provide username and password | |
$mail->Username = "username"; | |
$mail->Password = "password"; | |
//If SMTP requires TLS encryption then set it | |
$mail->SMTPSecure = "tls"; | |
//Set TCP port to connect to | |
$mail->Port = 587; | |
// /SMTP | |
//From email address and name | |
$mail->From = "email"; | |
$mail->FromName = "name"; | |
//To address and name | |
$mail->addAddress("email", "name"); | |
$mail->addAddress("email", "name"); | |
//Address to which recipient will reply | |
$mail->addReplyTo("email", "name"); | |
//CC and BCC | |
$mail->addCC("email"); | |
$mail->addCC("email"); | |
$mail->addBCC("email"); | |
//Send HTML or Plain Text email | |
$mail->isHTML(true); | |
$mail->Subject = "CookHuts Mail Test"; | |
$mail->Body = "<i>Mail body in HTML</i>"; | |
$mail->AltBody = "This is the plain text version of the email content"; | |
if(!$mail->send()) { | |
echo "Mailer Error: " . $mail->ErrorInfo; | |
} else { | |
echo "Message has been sent successfully"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Email::Sender::Simple qw(sendmail); | |
use Email::Sender::Transport::SMTPS; | |
use Email::Simple (); | |
use Email::Simple::Creator (); | |
my $smtpserver = 'smtp.gmail.com'; | |
my $smtpport = 587; | |
my $smtpuser = '[email protected]'; | |
my $smtppassword = 'password'; | |
my $transport = Email::Sender::Transport::SMTPS->new({ | |
host => $smtpserver, | |
ssl => 'starttls', | |
port => $smtpport, | |
sasl_username => $smtpuser, | |
sasl_password => $smtppassword, | |
debug => 1, | |
}); | |
my $email = Email::Simple->create( | |
header => [ | |
To => '[email protected]', | |
From => '[email protected]', | |
Subject => 'Hi!', | |
], | |
body => "This is my message\n", | |
); | |
sendmail($email, { transport => $transport }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import smtplib, ssl | |
smtp_server = "smtp.gmail.com" | |
port = 587 # For starttls | |
sender_email = "[email protected]" | |
password = input("Type your password and press enter: ") | |
# Create a secure SSL context | |
context = ssl.create_default_context() | |
# Try to log in to server and send email | |
try: | |
server = smtplib.SMTP(smtp_server,port) | |
server.ehlo() # Can be omitted | |
server.starttls(context=context) # Secure the connection | |
server.ehlo() # Can be omitted | |
server.login(sender_email, password) | |
# TODO: Send email here | |
except Exception as e: | |
# Print any error messages to stdout | |
print(e) | |
finally: | |
server.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment