/    Sign up×
Community /Pin to ProfileBookmark

Help me turn my PHPmailForm to use SMTP

Hello

I am back with my annoying mail form again ?
Suddenly my website cannot send mails using the ordinary PHPmailer and now I obviously must use SMTP instead. So I really really need some help updating my already existing mail form to be able to send it through SMTP.

I have found this code which is a working example SMTP

[code=php]<?php

if (‘POST’ == $_SERVER[‘REQUEST_METHOD’]) {

require_once(‘../phpmailer/class.phpmailer.php’);

$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = ‘ssl’; // sets the prefix to the servier
$mail->Host = ‘smtp.gmail.com’; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = ‘[email protected]’; // GMAIL username
$mail->Password = ‘MyPassword’; // GMAIL password

$to = $_POST[‘to’];
$to = filter_var($to, FILTER_VALIDATE_EMAIL); // ensure email valid
if (” == $to) exit(‘Invalid email address!’);

$body = $_POST[‘message’];
$body = eregi_replace(‘[]’,”,$body);

$from = $mail->Username;

// $mail->SetFrom($from, ‘Euro Rider’);
$mail->AddReplyTo($from,’[email protected]’);
$mail->Subject = ‘PHPMailer Test Subject via smtp (Gmail), basic’;
$mail->AltBody = ‘To view the message, please use an HTML compatible email viewer!’; // optional, comment out and test
$mail->MsgHTML($body);

$address = ‘[email protected]’;
$mail->AddAddress($to, ‘My Name’);

if(!$mail->Send())
exit(‘Mailer Error: ‘ . $mail->ErrorInfo);
else exit(‘Message sent!’);

}
?>

<form method=”post”>
<input type=”text” name=”to” value=”recepient@email”/><br/>
<textarea name=”message”>Message to send</textarea>
<input type=”submit” value=”Send”/>
</form>[/code]

And this is my original mail form

[code=php]<?php

foreach($_POST as $key => $value){
$_SESSION[‘form’][$key]=$value;
}

// —————— Configuration ——————————————– //
// ——– YOU CAN AND SHOULD TAKE 5 MINUTES TO CUSTOMIZE THIS —————- //

$receiver = “[email protected]”; // Your email.
$v_w = “blue”; // Write a word for the user to write, in order to unable bots to send emails.
// Examples: 2 + 2 = ?? then the v.w. shall be 4… or what color is the sky ??
// answer shall be blue, ect… 🙂
$yoursitename = “My Name”; // Write your site name.
$homepage = “http://MyPage.com”; // Your Website’s main Page.
$url_2_contactf = sefRelToAbs( ‘index.php?option=com_content&view=article&id=’.$menu->id.’&catid=’.$content->catid.’:events&Itemid=13′); // URL to contact form

$field_size = 30; // Chose the size you want the fields to be. Default 30.
$textarea_cols = 40; // Chose how many columns you want the text area to have. Default 30.
$textarea_row = 7; // Chose how many rows you want the text area to have. Default 10.

// —————————————————————————– //
// —————— DO NOT EDIT BELLOW THIS ———————————- //
// —————————————————————————– //

// Lägg till varje ny rad som finns med i Mail Form, annars kommer den ej visas i mailet
if(isset($_POST[‘send’]))
{
$name = htmlspecialchars($_POST[‘name’]);
$email = htmlspecialchars($_POST[’email’]);
$adress = htmlspecialchars($_POST[‘adress’]);
$tel = htmlspecialchars($_POST[‘tel’]);
$msg = htmlspecialchars($_POST[‘msg’]);
$vword = htmlspecialchars($_POST[‘vword’]);
$ip = $_SERVER[‘REMOTE_ADDR’];

// CHECK IF EMAIL IS VALID ////
function CheckMail($email)
{
if(eregi(“^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*.[a-z]{2,4}$”, $email))
{
return true;
}
else
{
return false;
}
}

// CHECK IF THE FORM HAS BEEN FILLED ////

if(empty($email))
{ mosRedirect( $url_2_contactf,
$tag_b4_message.””.$tag_bitwin_er_msg_b4.””.$er_email_miss.””.$tag_bitwin_er_msg_af);
}
// CHECK IF EMAIL IS VALID ////
if(CheckMail($email) != true)
{ mosRedirect( $url_2_contactf,
$tag_b4_message.” “.$tag_bitwin_er_msg_b4.” “.$er_email.” “.$tag_bitwin_er_msg_af);
}
// IF THEY ARE NOT EMPTY
if(!empty($name) && !empty($adress) && !empty($tel) && !empty($vword) && CheckMail($email) == true)
{
if($vword == $v_w)
{
$headers .= “From: “.$email.””;
// WE STORE THE MESSAGE IN A VARIABLE
$messageproper =
sefRelToAbs( $url_2_contactf ) .”n”.
“Från: $name – $email nn”.
“————————- $yoursitename ————————-nn” .
“Namn: $namen” .
“Adress: $adressn” .
“Tel: $telnn” .
“Meddelande: $msgn” .
“n—————————————————————n”.
“Skickat från IP numret: $ip” ;

$sent_email = mail(“$receiver”, “=?UTF-8?B?”.base64_encode(“Förfrågan: $content->title”.$subject ).”?=”, $messageproper, “From: $name <$email>”);

if($sent_email)
{ mosRedirect(“$url_2_contactf”,”Tack!<br>Din förfrågan är skickad.<br> Vi kommer svara Er så snart som möjligt”);
echo ‘<blockquote> Tack <strong>’ . $name . ‘</strong>, Din förfrågan har skickats! <br /> Vi kommer svara Er så fort vi kan.<br>
And for security reasons, your IP was recorded <strong> IP: ‘ .$ip .'</strong><br />
Please wait…. redirecting… 🙂
<script language=javascript>
setTimeout(“location.href=”.$homepage.””, 3000);
</script>
</blockquote>’;
}
}
}
}
else
{

?>

<!– EDIT BELLOW –>
<p>Skicka en bokningsförfrågan genom formuläret nedan.</p>
<form name=”sobimail” class=”sobimailform” clickaider_track_me=”formmail” method=”post” action=”<?php $url_2_contactf ?>”><br />

<fieldset>
<legend> Kontaktuppgifter </legend>
<label for=”name”>Namn: *</label><br />
<input type=”text” name=”name” id=”name” size=”<?php echo $field_size; ?>”
value=”<?php echo @$_SESSION[‘form’][‘name’]; ?>” /><br />
<label for=”adress”>Adress: * <span>(gata,postnr, ort)</span></label><br />
<input type=”text” name=”adress” id=”adress” size=”<?php echo $field_size; ?>”
value=”<?php echo @$_SESSION[‘form’][‘adress’]; ?>”/><br />
<label for=”tel”>Telefon: *</label><br />
<input type=”text” name=”tel” id=”tel”
value=”<?php echo @$_SESSION[‘form’][‘tel’]; ?>” size=”<?php echo $field_size; ?>” /><br />
<label for=”email”>E-post: *</label><br />
<input type=”text” name=”email” id=”email” size=”<?php echo $field_size; ?>”
value=”<?php echo @$_SESSION[‘form’][’email’]; ?>” /><br />

</fieldset>

<fieldset>
<?php include ‘veri_text.txt’;?>
</fieldset>

<fieldset>

<label for=”msg”>Message: <br />
<textarea name=”msg” id=”msg” rows=”<?php echo $textarea_row; ?>” cols=”<?php echo $textarea_cols; ?>” ><?php echo @$_SESSION[‘form’][‘msg’];?></textarea><br />

</fieldset>
<fieldset>
<input class=”button” type=”submit” name=”send” value=”Skicka förfrågan” /> <br />
</fieldset>
</form>

<!– EDIT ABOVE –>
<?php
// Cleanses all data in the fields after revisit
unset($_SESSION[‘form’]);
} ?>
[/code]

And this is how far I could go, which of course is not working yet…
The changes I made is only editing:
[B]mail->XX[/B] to [B]sent_email->XX[/B]

[code=php]<?php

foreach($_POST as $key => $value){
$_SESSION[‘form’][$key]=$value;
}

// —————— Configuration ——————————————– //
// ——– YOU CAN AND SHOULD TAKE 5 MINUTES TO CUSTOMIZE THIS —————- //

require_once(‘../phpmailer/class.phpmailer.php’);

$sent_email = new PHPMailer();
$sent_email->IsSMTP(); // telling the class to use SMTP
$sent_email->SMTPAuth = true; // enable SMTP authentication
$sent_email->SMTPSecure = ‘ssl’; // sets the prefix to the servier
$sent_email->Host = ‘smtp.gmail.com’; // sets GMAIL as the SMTP server
$sent_email->Port = 465; // set the SMTP port for the GMAIL server
$sent_email->Username = ‘[email protected]’; // GMAIL username
$sent_email->Password = ‘MyPassword’; // GMAIL password

—- unchanged below —-
[/code]

Much greateful for any help I may recieve

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @Siddan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...