Tuesday, October 11, 2011

Using PHPMailer and Gmail to send email

If you are developing applications using PHP and you need to send email you can use the PHPMailer() class in PHP. Using a publicly available SMTP server to send the email is much easier than trying to setup your own email server. The following code snippet shows the various settings for the mailer.
The code assumes that you have PHP 5.x version and you have class.phpmailer.php file in the include directory.
Google uses ssl for the smtp connection. In order for this example to work with google smtp server, you need to enable ssl in your php.ini file by adding a line that says extension=php_openssl.dll

If you are not sure of the exact location of the php.ini file and you are using xampp, you can find the location of the php.ini file by navigating to http://localhost/xampp/phpinfo.php on your browser and look for the text "Loaded Configuration File". Once you find the file, edit it and look for the text "extension=php_openssl.dll". If the text is not found in your file, add a new line at the end of the file with the above text.
IsSMTP();
$mail->SMTPDebug = 1; // 1 tells it to display SMTP errors and messages, 0 turns off all errors and messages, 2 prints messages only.

$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->Port = 465; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication

$mail->Username = 'user@gmail.com'; // replace this with your email acct
$mail->Password = 'userPassword'; // replace this with your password

$mail->From = 'jmeslie@gmail.com';
$mail->FromName = 'Jean Meslie';
$mail->AddAddress('receipient@yahoo.com', 'Receiver');
$mail->AddReplyTo('user@gmail.com'); // Adds a “Reply-to' address. Un-comment this to use it.
$mail->Subject = 'test message';
$mail->Body = 'message body goes here. This message was sent at '. time();

if ($mail->Send() == true) {
echo 'The message has been sent at '. time();
}
else {
echo 'The email message has NOT been sent for some reason. Please try again later.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
?>

24 comments:

Tom said...

That is very cool, idea! I love it! For me It's kind of hardunderstandig stuff but, you are doing well! If you need some help in future for the essay or homework, you can ask for help this guys! They really know how to do it! Just text to them paper writing services and everything gonna be cool! Enjoy!

Elizabeth Johanson said...

Assignment writing services are not just available in any specific region but it has now spread to every corner of the modern globe. It's reasonable because every student wants to purchase or rent an academic writing service & we are here with our Masters dissertation proposal services UK, to help them any way we can.

shopify said...

Meta faces billion pound class action case They choose our services because we deliver value for them and they have meaningful control of what information they share on Meta’s platforms and with

Agata Barnes said...

Hey guys. Looking for an artist to write my theme, I found the authors site, and I really liked it. Responsible guys know their job master papers reviews Tasks are completed on time. All at the highest level. I am very satisfied, and if necessary, always only to them. Cool, I recommend.

Unknown said...

How to Play Casino: Easy Guide to playing slots on
Casino games are played by 4 players, air jordan 18 retro from us the average time 해외 토토 사이트 넷마블 they take turns 넷마블 토토 넷마블 is around 14:20. The house is divided into jordan 18 white royal blue to us three distinct categories: the house air jordan 18 retro men red shop

Mia Wilson said...

I didn't have time to write a history essay. A college friend recommended these guys college essay service he responded very well. The application was accepted immediately, all wishes on the topic were taken into account. The work was done on time. The teacher accepted the first time. I am very pleased. I am satisfied, I recommend.

William Woodruff said...

I am developing an application using PHP and am stuck on a point. Can you guide me on whether I should use PHPMailer to send email in the contact form, or Gmail option will be best? https://www.theacademicpapers.co.uk/blog/2022/05/13/what-is-sample-study-in-research-purpose-and-techniques/

whome2249 said...

A lot of things in my life have already happened at school, I was lucky that then this site https://essaysrescue.com/bid4papers-review/ was recommended to me, on which I now often sit, and my grades have become much better, any written work will be written

Qasim Khan said...

I know that everyone wants to learn everything in the shortest time possible homework writing service. But I am sure that you don’t want to waste your time and money by doing assignments that you are not capable of doing. So, if you have the same problem, then you should use the most appropriate website for assignment writing services.

Shawn Abi said...

Track down the entirety of your coat, Battlestar Galactica Warrior Viper Pilot Jacket, and outerwear needs this season at Urban Outfitters. Shop different styles including denim coats. Find the selective Men's creator Jackets and Coats assortment by VERSACE. Purchase online the notorious VERSACE items, extravagance, and tastefulness models.

Anonymous said...

서울콜걸
울산콜걸
대구콜걸
대전콜걸
인천콜걸


세종콜걸
부산콜걸
인천콜걸
울산콜걸
대구콜걸

dry bulk trucking said...

We then dispatch the road base materials to your site’s location using our wide fleet of aggregate haulers specifically designedCEMENT TREATED BASE services in austin to retain the aggregate’s uniform quality and to protect it from contamination.

Loan providers near me said...

We are the best service provider Financial Instruments in Dubaiin UAE ! So do visit Tacb.

donaldriskell said...

Hello there, thank you for sharing this information. This one is the thing I need. I can now finish my website. Anyway, I want to return the favor, maybe by sharing these games ExoMiner and Dragonscapes Adventure. Click the links I attached here to download both games for free. After that, start your amazing adventure with these games. And if possible and if you like, invite your friends as well so they will know how awesome these games are.

Roomi alex said...
This comment has been removed by the author.
Roomi alex said...

That idea is awesome! I adore it. Although some of it is difficult for me to understand, you are doing great! You can ask for assistance from assignment writing help service saudi arabia if you ever need it for an essay or assignment! They are truly experts at it! Send them a text to ask about our paper writing services, and everything will be great! Enjoy!

Elite Blue said...

"Looking for the best in web design and development? Look no further than Elite Blue for web design and development. Our team of experts combines creative design with cutting-edge technology to create custom websites that meet your unique needs. From stunning graphics to intuitive user experiences, we deliver outstanding results that exceed your expectations. Trust us to bring your online vision to life and create a website that not only looks great but also drives business success."

envirosteam said...

Developing applications using PHP is an excellent choice, as it offers a versatile and powerful platform for web development. Its flexibility and wide range of libraries make it a great tool for creating dynamic and interactive websites. Mobile Car Detailing OttawaDelivering professional and convenient car detailing services directly to your location in Ottawa, just like PHP brings efficiency and convenience to web development.




Curbsidecar said...

Learn how to send emails effortlessly using PHPMailer and Gmail! And while you're mastering the art of communication, don't forget to give your car the attention it deserves with Curbside Ottawa Car Detailing. Achieve both digital and automotive excellence with these handy resources

Vava Store said...

It is very fantastic and informative information. thanks for sharing such informative information Everyone should know about it. thanks for sharing. share more information like this. want the best quality leather jacket like the Casino royale tuxedo with the latest style design and made from good quality material at a cheap price level.

hastencatering said...

Unlocking the power of PHPMailer and Gmail for email communication is a game-changer for web developers.catering midland tx, let our catering services add a layer of convenience to your tech endeavors, ensuring your meetings and coding sessions are fueled with culinary excellence. Just as PHPMailer streamlines email processes, our catering services streamline the dining experience at your events in Midland. Trust us to make every gathering a seamless blend of technology and gastronomy.





Hamood SRCA said...

The erp implementation service in pakistan
are pivotal for modernizing businesses, enhancing efficiency, and streamlining operations. With skilled professionals and tailored solutions, they empower organizations to adapt to evolving market demands and achieve sustainable growth.

Umar SRP said...

android app development company saudi arabia are experiencing significant growth, driven by the country's increasing focus on digital transformation and technology adoption. These companies offer a range of services including custom app development, UX/UI design, and software maintenance, catering to diverse industries such as finance, healthcare, and entertainment. With a strong emphasis on quality and innovation, many Saudi app developers are leveraging advanced technologies like AI and IoT to create sophisticated mobile solutions that meet both local and global market demands. The vibrant tech ecosystem and supportive government initiatives further bolster the sector's expansion, positioning Saudi Arabia as a burgeoning hub for mobile app development in the Middle East.

Future Style said...

The Floral Collection by Future Style By Nida Faysal showcases exquisite floral designs on luxurious fabrics. Each piece embodies a harmonious blend of tradition and modernity, offering a timeless elegance perfect for any sophisticated wardrobe.