简体   繁体   中英

PHP Mail to SMTP

I currently have a piece of PHP script that sends an email from a user inputted email. This was being identified by GMail as a spam email because it spoofed the email. I would like to convert my script so that it does exactly the same things but through an SMTP email.

   <?php
ob_start();
include('mplookup.php');
ob_end_clean();
$email = $_POST['emailfrom'];
$human = $_POST['human'];
$text = $_POST['text'];
$address = $_POST['address'];
$city = $_POST['city'];
$footer = '<br><em><strong>Disclaimer: &quot;Any views or opinions presented in this email are solely those of the author and do not necessarily represent those of EG4DEMUK.&nbsp; EG4DEMUK will not accept any liability in respect of defamatory or threatening communication. If you would like to raise a complaint about an email sent using our tool, please contact us at <a href="mailto:ercegypt.uk@ercegypt.org?subject=Email%20Complaint"></a>&quot;.</strong></em><p>-----------------------------------------------------------</p>
The ERC is an organisation that brings together Egyptian citizens and movements abroad, irrespective of their political or ideological affiliations. We share in common a belief in the principles of the January 25th Revolution and oppose all aspects of corruption and dictatorship in Egypt. We believe in constitutional legitimacy and work for the establishment of a civil state that reflects the will of the Egyptian people and their freedom in choosing their government.</p>';
$postcode = $_POST['postcode'];
$name = $_POST['flname'];
$message = $text.$name."<br />".$address."<br />".$city."<br />".$postcode."<br />".$footer;
$to = "";
$subject = 'Sisi\'s visit to the UK: Sent using the ERC\'s Tool ';
$headers = "From: ".$email."\r\n";
$headers .= "Reply-To: ".$email."\r\n";
$headers .= "Return-Path: ".$email."\r\n";
$headers .= "BCC: \r\n";
$headers .= "CC: $email\r\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

?>
<?php

if ($_POST['submit'] && $human == '4')
{
    if (mail ($to, $subject, $message, $headers)) {
        echo '<p>Your message has been sent! Thank you for participating in EG4DEMUK\'s campaign.</p>';
    }
    else
    {
        echo '<p>Something went wrong, please go back and try again!</p>';
    }
}

?>

I have no clue how to proceed in converting this to SMTP. Any help would be much appreciated.

You can use a pre-written PHP library for this.

https://github.com/PHPMailer/PHPMailer

The GitHub page includes a very good example script and you can simply copy your already written parameters to the corresponding variables of the library.

For example:

    $to = "xxx@outlook.com"; becomes $mail->addAddress('xxx@outlook.com');

    $mail->Body = $message;

etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM